-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
136 lines (127 loc) · 3.7 KB
/
App.tsx
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import './App.css';
import { ThemeContext } from './context/theme/theme';
import Home from './pages/home/home';
import Switch from 'react-switch';
import { FaSun, FaMoon } from 'react-icons/fa';
import { useEffect, useReducer, useState } from 'react';
import { StateContext, StateType } from './context/state/state';
import {
ADD_NOTE,
DELETE_NOTE,
INIT_NOTES,
SET_EDIT_MODE,
SET_NOTE_FOR_EDIT,
UPDATE_NOTE,
} from './actions';
import { getNotes } from './services/notes-service';
import DetailedNote from './pages/detailed-note/detailed-note';
import {
createBrowserRouter,
RouterProvider,
Route,
Link,
} from "react-router-dom";
const router = createBrowserRouter([
{
path: "/",
element: <Home></Home>,
},
{
path: "/:id",
element: <DetailedNote></DetailedNote>,
},
]);
function App() {
let defaultTheme;
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
defaultTheme = 'dark'
} else {
defaultTheme = 'light'
}
const [theme, setTheme] = useState(defaultTheme);
const [checked, setChecked] = useState(defaultTheme==='dark');
const [state, dispatch] = useReducer(
(state: StateType, action: { type: string; payload: any }) => {
switch (action.type) {
case INIT_NOTES:
return { ...state, notes: action.payload };
case SET_EDIT_MODE:
return { ...state, editMode: action.payload };
case SET_NOTE_FOR_EDIT:
return { ...state, noteToBeEdited: action.payload };
case ADD_NOTE:
return { ...state, notes: [action.payload, ...state.notes] };
case DELETE_NOTE:
const index = state.notes.findIndex(
(note) => note.id === action.payload
);
let editedNotes = [...state.notes];
editedNotes.splice(index, 1);
return { ...state, notes: editedNotes };
case UPDATE_NOTE:
const indexUpdated = state.notes.findIndex(
(note) => note.id === action.payload.id
);
let editedNotesUpdated = [...state.notes];
editedNotesUpdated.splice(indexUpdated, 1);
editedNotesUpdated.unshift(action.payload);
return { ...state, notes: editedNotesUpdated };
default:
return state;
}
},
{ notes: [], editMode: false, noteToBeEdited: null }
);
const checkForTheme =(check:boolean)=>{
if (check) {
setTheme('dark');
} else {
setTheme('light');
}
}
const changeHandler = (check: boolean) => {
setChecked(!checked);
checkForTheme(check);
};
useEffect(() => {
async function initializeNotes() {
const notes = await getNotes();
dispatch({ type: INIT_NOTES, payload: notes });
}
initializeNotes();
checkForTheme(checked);
}, []);
return (
<StateContext.Provider value={{ state, dispatch }}>
<ThemeContext.Provider value={theme}>
<div className={`App ${theme}`}>
<Switch
onChange={changeHandler}
checked={checked}
className="react-switch"
uncheckedIcon={
<FaMoon
size={20}
style={{ paddingTop: '4px', paddingRight: '4px', float: 'right' }}
color="white"
></FaMoon>
}
checkedIcon={
<FaSun
size={20}
style={{ paddingTop: '4px', paddingLeft: '4px' }}
color="yellow"
></FaSun>
}
onColor="#900"
offColor="#333"
onHandleColor="#000"
></Switch>
{/* here we want router */}
<RouterProvider router={router} />
</div>
</ThemeContext.Provider>
</StateContext.Provider>
);
}
export default App;