mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
UI Actions
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user