-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathstore.jsx
More file actions
75 lines (68 loc) · 2.55 KB
/
store.jsx
File metadata and controls
75 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { createContext, useContext, useState, useReducer } from 'react';
const TodosContext = createContext([]);
const LabelsContext = createContext([]);
const UserContext = createContext(null);
const UiContext = createContext(null);
const reducer = (state = [], action = {}) => {
const mutatedItem = action.payload;
if (!mutatedItem) { return }
const mutatedIndex = state.findIndex((item) => item.id === mutatedItem.id);
switch (action.type) {
case "CREATED":
if (mutatedIndex < 0) {
state.push(mutatedItem);
}
break;
case "DELETED":
if (mutatedIndex >= 0) {
state.splice(mutatedIndex, 1);
}
break;
case "UPDATED":
state[mutatedIndex] = mutatedItem;
break;
default:
}
return [...state];
}
export function TodosProvider({ children, todos }) {
const [state, dispatch] = useReducer(reducer, todos);
return <TodosContext.Provider value={[state, dispatch]}>{children}</TodosContext.Provider>;
}
export function LabelsProvider({ children, labels }) {
const [state, dispatch] = useReducer(reducer, labels);
return <LabelsContext.Provider value={[state, dispatch]}>{children}</LabelsContext.Provider>;
}
export function UserProvider({ children, user }) {
const [isDarkMode, setDarkMode] = useState(user && user.darkMode);
const [isListView, setListView] = useState(user && user.listMode);
const userValue = [{
name: user && user.name,
email: user && user.email,
isDarkMode,
isListView
}, {
toggleDarkMode: () => setDarkMode(!isDarkMode),
toggleView: () => setListView(!isListView)
}];
return <UserContext.Provider value={userValue}>{children}</UserContext.Provider>;
}
export function UiProvider({ children }) {
const [isNavBarOpen, setNavBarOpen] = useState(true);
const [noteInEditMode, setNoteInEditMode] = useState("");
const [selectedLabelId, setSelectedLabelId] = useState("");
const uiValue = [{
isNavBarOpen,
noteInEditMode,
selectedLabelId
}, {
toggleNavBar: () => setNavBarOpen(!isNavBarOpen),
setNoteInEditMode,
setSelectedLabelId
}];
return <UiContext.Provider value={uiValue}>{children}</UiContext.Provider>;
}
export const useTodosStore = () => useContext(TodosContext);
export const useLabelsStore = () => useContext(LabelsContext);
export const useUserStore = () => useContext(UserContext);
export const useUiStore = () => useContext(UiContext);