mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
BIG-126: async localization loaded data failed to be injected to application.
This commit is contained in:
@@ -1,27 +1,8 @@
|
||||
import { isEqual } from 'lodash';
|
||||
import React, { useRef, useEffect, useMemo } from 'react';
|
||||
import useAsync from './async';
|
||||
import { useRef, useEffect, useMemo } from 'react';
|
||||
import useAutofocus from './useAutofocus';
|
||||
import { useLocalStorage } from './utils/useLocalStorage';
|
||||
|
||||
// import use from 'async';
|
||||
|
||||
/**
|
||||
* A custom useEffect hook that only triggers on updates, not on initial mount
|
||||
* Idea stolen from: https://stackoverflow.com/a/55075818/1526448
|
||||
* @param {Function} effect
|
||||
* @param {Array<any>} dependencies
|
||||
*/
|
||||
export function useUpdateEffect(effect, dependencies = []) {
|
||||
const isInitialMount = useRef(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (isInitialMount.current) {
|
||||
isInitialMount.current = false;
|
||||
} else {
|
||||
effect();
|
||||
}
|
||||
}, dependencies);
|
||||
}
|
||||
export * from './utils';
|
||||
|
||||
export function useIsValuePassed(value, compatatorValue) {
|
||||
const cache = useRef([value]);
|
||||
@@ -62,41 +43,7 @@ export function useCellAutoFocus(ref, autoFocus, columnId, rowIndex) {
|
||||
return ref;
|
||||
}
|
||||
|
||||
export * from './useRequestPdf';
|
||||
export { useAsync, useAutofocus };
|
||||
|
||||
// Hook
|
||||
export function useLocalStorage(key, initialValue) {
|
||||
// State to store our value
|
||||
// Pass initial state function to useState so logic is only executed once
|
||||
const [storedValue, setStoredValue] = React.useState(() => {
|
||||
try {
|
||||
// Get from local storage by key
|
||||
const item = window.localStorage.getItem(key);
|
||||
// Parse stored json or if none return initialValue
|
||||
return item ? JSON.parse(item) : initialValue;
|
||||
} catch (error) {
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
// Return a wrapped version of useState's setter function that ...
|
||||
// ... persists the new value to localStorage.
|
||||
const setValue = (value) => {
|
||||
try {
|
||||
// Allow value to be a function so we have same API as useState
|
||||
const valueToStore =
|
||||
value instanceof Function ? value(storedValue) : value;
|
||||
// Save state
|
||||
setStoredValue(valueToStore);
|
||||
// Save to local storage
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||
} catch (error) {
|
||||
// A more advanced implementation would handle the error case
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
return [storedValue, setValue];
|
||||
}
|
||||
export { useAutofocus };
|
||||
|
||||
export function useMemorizedColumnsWidths(tableName) {
|
||||
const [get, save] = useLocalStorage(`${tableName}.columns_widths`, {});
|
||||
@@ -106,42 +53,3 @@ export function useMemorizedColumnsWidths(tableName) {
|
||||
};
|
||||
return [get, save, handleColumnResizing];
|
||||
}
|
||||
|
||||
// Hook
|
||||
function usePrevious(value) {
|
||||
// The ref object is a generic container whose current property is mutable ...
|
||||
// ... and can hold any value, similar to an instance property on a class
|
||||
const ref = useRef();
|
||||
// Store current value in ref
|
||||
useEffect(() => {
|
||||
ref.current = value;
|
||||
}, [value]); // Only re-run if value changes
|
||||
// Return previous value (happens before update in useEffect above)
|
||||
return ref.current;
|
||||
}
|
||||
|
||||
export function useWhen(condition, callback) {
|
||||
React.useEffect(() => {
|
||||
if (condition) {
|
||||
callback();
|
||||
}
|
||||
}, [condition, callback]);
|
||||
}
|
||||
|
||||
export function useWhenNot(condition, callback) {
|
||||
return useWhen(!condition, callback);
|
||||
}
|
||||
|
||||
export function useWatch(state, callback, props) {
|
||||
const config = { immediate: false, ...props };
|
||||
|
||||
const previosuState = usePrevious(state);
|
||||
const flag = React.useRef(false);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!isEqual(previosuState, state) || (config.immediate && !flag.current)) {
|
||||
flag.current = true;
|
||||
callback(state);
|
||||
}
|
||||
}, [previosuState, state, config.immediate, callback]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user