UI Actions

This commit is contained in:
Belen Curcio
2020-10-01 09:26:22 -03:00
parent 2971b4f802
commit 7cf79cea91
4 changed files with 37 additions and 27 deletions

View File

@@ -9,6 +9,32 @@ export interface UIState {
dispatch: (string) => void;
}
export const UIContext = React.createContext<UIState>(initialState);
UIContext.displayName = "UIContext";
export const UIProvider: FunctionComponent = (props) => {
const [state, dispatch] = React.useReducer(uiReducer, initialState);
const openSidebar = () => dispatch("OPEN_SIDEBAR");
const closeSidebar = () => dispatch("CLOSE_SIDEBAR");
const value = {
...state,
openSidebar,
closeSidebar,
};
return <UIContext.Provider value={value} {...props} />;
};
export const useUI = () => {
const context = React.useContext(UIContext);
if (context === undefined) {
throw new Error(`useUI must be used within a UIProvider`);
}
return context;
};
function uiReducer(state, action) {
switch (action) {
case "OPEN_SIDEBAR": {
@@ -25,23 +51,3 @@ function uiReducer(state, action) {
}
}
}
export const UIContext = React.createContext<UIState>(initialState);
UIContext.displayName = "UIContext";
export const UIProvider: FunctionComponent = (props) => {
const [state, dispatch] = React.useReducer(uiReducer, initialState);
const value = {
...state,
dispatch,
};
return <UIContext.Provider value={value} {...props} />;
};
export const useUI = () => {
const context = React.useContext(UIContext);
if (context === undefined) {
throw new Error(`useUI must be used within a UIProvider`);
}
return context;
};