You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.2 KiB
68 lines
1.2 KiB
3 years ago
|
import { useReducer, useRef } from 'react';
|
||
|
|
||
|
|
||
|
const initialState = () => ({
|
||
|
appSettings: {
|
||
|
apiURL: 'https://pool.wowne.ro/api',
|
||
|
appLoaded: false,
|
||
|
coinDecimals: 11,
|
||
|
},
|
||
|
pool: {
|
||
|
config: null,
|
||
|
},
|
||
|
user: {
|
||
|
loggedIn: false,
|
||
|
},
|
||
|
messages: {},
|
||
|
});
|
||
|
|
||
|
let updatedState;
|
||
|
|
||
|
const reducer = (state, action) => {
|
||
|
let result = {};
|
||
|
const {
|
||
|
appLoaded,
|
||
|
config,
|
||
|
loggedIn,
|
||
|
} = action;
|
||
|
|
||
|
switch (action.type) {
|
||
|
case 'APP_LOADED':
|
||
|
result = {
|
||
|
...state,
|
||
|
appSettings: {
|
||
|
...state.appSettings,
|
||
|
appLoaded,
|
||
|
},
|
||
|
};
|
||
|
break;
|
||
|
case 'CLEAR_MESSAGES':
|
||
|
result = { ...state, messages: {} };
|
||
|
break;
|
||
|
case 'USER_LOGGED_IN':
|
||
|
result = { ...state, user: { loggedIn } };
|
||
|
break;
|
||
|
case 'UPDATE_POOL_CONFIG':
|
||
|
result = {
|
||
|
...state,
|
||
|
pool: {
|
||
|
...state.pool,
|
||
|
config,
|
||
|
}
|
||
|
};
|
||
|
break;
|
||
|
default:
|
||
|
throw new Error();
|
||
|
}
|
||
|
updatedState.current = result;
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
const useAppState = () => {
|
||
|
const state = initialState();
|
||
|
updatedState = useRef(state);
|
||
|
return [...useReducer(reducer, state), updatedState];
|
||
|
};
|
||
|
|
||
|
export default useAppState;
|