qualiteer/src/ctx/StoreContext.jsx

47 lines
995 B
React
Raw Normal View History

2022-05-05 12:35:47 +00:00
import React, { useReducer, createContext, useMemo } from "react";
const StoreContext = createContext();
const ACTIONS = {
UPDATE: "u",
};
2022-05-17 12:32:04 +00:00
const initialState = {
intervals: [],
failing: [],
regions: [],
focusJob: false,
simplifiedControls: false,
defaultRegion: "us", // Local Store
defaultPage: "failing", // Local Store
};
2022-05-05 12:35:47 +00:00
const reducer = (state, action) => {
2022-05-17 12:32:04 +00:00
const { store } = action;
2022-05-05 12:35:47 +00:00
// Actions
switch (action.type) {
case ACTIONS.UPDATE:
return { ...state, ...store };
default:
return state;
}
};
export const StoreProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
const context = {
state,
dispatch,
2022-05-17 12:32:04 +00:00
updateStore: (store) => dispatch({ type: ACTIONS.UPDATE, store }),
2022-05-05 12:35:47 +00:00
};
const contextValue = useMemo(() => context, [state, dispatch]);
return (
<StoreContext.Provider value={contextValue}>
{children}
</StoreContext.Provider>
);
};
export default StoreContext;