|
1 | 1 | import React from 'react'; |
2 | | -import logo from './Shared/Logo/logo.svg'; |
3 | | -import './App.css'; |
4 | | -import CardList from './Cards/CardList' |
5 | | -import AddCard from './Cards/AddCard' |
6 | | -import Context from './context' |
7 | | -import Loader from './Shared/Loader' |
8 | | -import ModalCardEdit from './Cards/ModalCardEdit' |
9 | | -import ModalLogin from './Login/ModalLogin' |
10 | | -import DataService from './Services/DataService' |
11 | | -import Card, { checkCardsArr } from './Cards/cardType/Card' |
12 | | -//import useDebouncedEffect from './Shared/useDebouncedEffect.hook' |
13 | | - |
14 | | -const { loadData, postData, updDataServLogin } = DataService() |
15 | | - |
16 | | -const brandText = "Notes" |
17 | | - |
18 | | -var cardCount = 0 |
19 | | - |
20 | | -function calcCount(cards) { |
21 | | - let id = cardCount; |
22 | | - [...cards].forEach(element => { |
23 | | - if (Number(element.id) >= id) id = Number(element.id) |
24 | | - }); |
25 | | - return id |
26 | | -} |
27 | 2 |
|
28 | | -function useCardsArr(defaultValue) { |
29 | | - const [value, setValue] = React.useState(defaultValue) |
| 3 | +import './App.css'; |
30 | 4 |
|
31 | | - function trySetValue(cardsArr) { |
32 | | - if (checkCardsArr(cardsArr) || cardsArr === null) setValue(cardsArr) |
33 | | - else console.error('Массив cardsArr не прошел проверку \n', cardsArr) |
34 | | - } |
| 5 | +import Loader from './shared/Loader' |
35 | 6 |
|
36 | | - return [value, trySetValue] |
37 | | -} |
38 | | - |
39 | | -function useUpdater() { |
40 | | - const [updaterVal, setUpdaterVal] = React.useState(null) |
41 | | - const timer = React.useRef() |
42 | | - if (timer.current) clearTimeout(timer.current) |
43 | | - timer.current = setTimeout(() => { |
44 | | - console.log("Timed update") |
45 | | - setUpdaterVal(Date.now()) |
46 | | - }, 60 * 1000) // обновяем через минуту |
47 | | - return [updaterVal] |
48 | | -} |
| 7 | +import { BrowserRouter as Router } from 'react-router-dom' |
| 8 | +import { useRoutes } from './routes' |
| 9 | +import { useAuth } from './hooks/auth.hook' |
| 10 | +import { AuthContext } from './context/AuthContext' |
| 11 | +import { PageContext } from './context/PageContext' |
| 12 | +import Header from './pages/SharedComponents/Header' |
49 | 13 |
|
50 | 14 | function App() { |
51 | | - const [cardsArr, setCards] = useCardsArr(null) |
52 | | - const [editCardId, setEditCardId] = React.useState(null) |
53 | | - const [loading, setLoading] = React.useState({ state: false, res: false }) |
| 15 | + const { token, login, logout, userId, email, ready } = useAuth() |
| 16 | + const isAuthenticated = !!token |
| 17 | + const routes = useRoutes(isAuthenticated) |
54 | 18 |
|
55 | | - const [openLogin, setOpenLogin] = React.useState(false) |
56 | | - const [logged, setLogged] = React.useState(false) |
57 | | - const [userName, setUserName] = React.useState(undefined) |
| 19 | + const [nav, setNav] = React.useState() |
58 | 20 |
|
59 | | - const [updaterVal] = useUpdater() |
60 | | - |
61 | | - React.useEffect(loadDataFromServer, [logged, userName, updaterVal]) // eslint-disable-line react-hooks/exhaustive-deps |
62 | | - //useDebouncedEffect(loadDataToServer, [cardsArr], 300) // eslint-disable-line react-hooks/exhaustive-deps |
63 | | - React.useEffect(loadDataToServer, [cardsArr]) // eslint-disable-line react-hooks/exhaustive-deps |
64 | | - React.useEffect(clearOldData, [logged, userName]) // eslint-disable-line react-hooks/exhaustive-deps |
65 | | - |
66 | | - /////////// |
67 | | - function onLogin(login) { |
68 | | - //console.log("onLogin", login) |
69 | | - return new Promise((res, rej) => { |
70 | | - onLogout() |
71 | | - .then(() => { |
72 | | - //console.log("onLogin", login, "onLogout.then") |
73 | | - updDataServLogin(login) |
74 | | - .then(r => { |
75 | | - //console.log("onLogin", login, "onLogout.then", "updDataServLogin.then") |
76 | | - setLogged(Boolean(r)) |
77 | | - setUserName(login) |
78 | | - res(r) |
79 | | - }) |
80 | | - .catch(e => { |
81 | | - //console.log("onLogin", login, "onLogout.then", "updDataServLogin.catch", e) |
82 | | - rej(e) |
83 | | - }) |
84 | | - }) |
85 | | - .catch(e => console.log("logout catch in onLogin", e)) |
86 | | - }) |
87 | | - } |
88 | | - |
89 | | - function onLogout() { |
90 | | - //console.log("onLogout") |
91 | | - return new Promise((res) => { |
92 | | - updDataServLogin(null) |
93 | | - .finally(() => { |
94 | | - if (logged) { |
95 | | - //console.log("onLogout - was logged, dislogin") |
96 | | - setUserName(undefined) |
97 | | - setLogged(false) |
98 | | - } else { |
99 | | - //console.log("onLogout - also not logged") |
100 | | - clearOldData() |
101 | | - } |
102 | | - }) |
103 | | - .finally(res) |
104 | | - .catch(e => console.log("Data service dislogin failed", e)) |
105 | | - }) |
106 | | - } |
107 | | - |
108 | | - function clearOldData() { |
109 | | - //console.log("clearOldData, logged:", logged) |
110 | | - if (!logged) setCards(null) |
111 | | - } |
112 | | - /////////// |
113 | | - |
114 | | - /////////// |
115 | | - function loadDataToServer() { |
116 | | - try { |
117 | | - let startUsername = userName |
118 | | - if (logged && userName) postData(cardsArr) |
119 | | - .then(res => { |
120 | | - if (!cardsArr) console.log("empty post!") |
121 | | - console.log('[onPostData]', res) |
122 | | - if (startUsername !== userName) { |
123 | | - console.log("упс, несостыковочка с userName"); |
124 | | - loadDataToServer() |
125 | | - } |
126 | | - }) |
127 | | - .catch(e => console.log(`Data post request error. Response: ${e}`)) |
128 | | - } |
129 | | - catch (e) { |
130 | | - console.error(e) |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - function loadDataFromServer() { |
135 | | - try { |
136 | | - let startUsername = userName |
137 | | - if (logged && userName) { |
138 | | - setLoading({ state: true, res: loading.res }) |
139 | | - loadData() |
140 | | - .then(data => { |
141 | | - console.log('[onLoadData]', 'Данные с сервера загружены') |
142 | | - if (startUsername === userName) setLoadedCards(data) |
143 | | - else console.log("упс, несостыковочка с userName"); |
144 | | - setLoading({ state: false, res: true }) |
145 | | - }) |
146 | | - .catch(e => { |
147 | | - console.log(`Data load request error. Response:`, e) |
148 | | - setLoading({ state: false, res: false }) |
149 | | - }) |
150 | | - } |
151 | | - } |
152 | | - catch (e) { |
153 | | - setLoading({ state: false, res: false }) |
154 | | - console.error(e) |
155 | | - } |
156 | | - } |
157 | | - /////////// |
158 | | - |
159 | | - /////////// |
160 | | - function setLoadedCards(cards) { |
161 | | - setCards([...cards]) |
162 | | - cardCount = calcCount(cards) |
163 | | - } |
164 | | - |
165 | | - function removeCard(index) { |
166 | | - cardsArr.splice(index, 1) |
167 | | - setCards([...cardsArr]) |
168 | | - } |
169 | | - |
170 | | - function deleteAll() { |
171 | | - setCards([]) |
172 | | - } |
173 | | - |
174 | | - function addCard(cardData = {}) { |
175 | | - const newCard = new Card({ id: ++cardCount, name: cardData.name, color: cardData.color, text: cardData.text }) |
176 | | - setCards( |
177 | | - |
178 | | - (cardsArr != null) ? cardsArr.concat([newCard]) : [newCard] |
| 21 | + if (!ready) { |
| 22 | + return ( |
| 23 | + <div className="container display-4 text-center p-3" > |
| 24 | + <Loader /> |
| 25 | + </div> |
179 | 26 | ) |
180 | 27 | } |
181 | 28 |
|
182 | | - function changeCardColor(index, color) { |
183 | | - cardsArr[index].color = color |
184 | | - setCards([...cardsArr]) |
185 | | - } |
186 | | - |
187 | | - function editCardContent(index, name, text) { |
188 | | - if (cardsArr[index]) { |
189 | | - let card = new Card(cardsArr[index]) |
190 | | - card.name = name |
191 | | - card.text = text |
192 | | - cardsArr[index] = card |
193 | | - } |
194 | | - setCards([...cardsArr]) |
195 | | - } |
196 | | - /////////// |
197 | | - |
198 | | - /////////// |
199 | | - function getCardByIndex(index) { |
200 | | - return index !== null ? cardsArr[index] : null |
201 | | - } |
202 | | - function setEditCard(index) { |
203 | | - setEditCardId(index) |
204 | | - } |
205 | | - function unsetEditCard() { |
206 | | - setEditCardId(null) |
207 | | - } |
208 | | - /////////// |
209 | 29 |
|
210 | 30 | return ( |
211 | | - <Context.Provider value={{ removeCard, changeCardColor, setEditCard, unsetEditCard, editCardContent, editCardId }}> |
212 | | - <div className="App pb-3 mb-3"> |
213 | | - <header className="p-1"> |
214 | | - <nav className="d-flex container px-0 flex-wrap-reverse justify-content-around"> |
215 | | - <div className="text-center d-flex p-1 align-items-center justify-content-center flex-wrap"> |
216 | | - <img src={logo} className="App-logo" alt="logo" /> |
217 | | - <h1 className="h2 m-0 text-dark pr-3 brand">{brandText}</h1> |
218 | | - </div> |
219 | | - <div className="text-center d-flex p-0 align-items-center flex-wrap ml-auto"> |
220 | | - {logged && |
221 | | - <button className="btn btn-light m-1" onClick={loadDataFromServer}> |
222 | | - {loading.state ? <Loader className='px-1' /> : <i className="bi bi-arrow-clockwise px-1"></i>} |
223 | | - <span className='d-xl-inline d-none'>Update</span> |
224 | | - </button> |
225 | | - } |
226 | | - <button className="btn btn-light m-1" onClick={() => setOpenLogin(true)}> |
227 | | - {logged ? <span><i className="bi bi-person"></i> {userName}</span> : <span><i className="bi bi-arrow-right-circle"></i> LOG IN</span>} |
228 | | - </button> |
229 | | - </div> |
230 | | - </nav> |
231 | | - <ModalLogin onLogin={onLogin} onLogout={onLogout} logged={logged} userName={userName} isOpen={openLogin} setOpenState={setOpenLogin} /> |
232 | | - </header> |
233 | | - |
234 | | - <main className="p-1 pb-3 mb-3"> |
235 | | - <AddCard onCreate={addCard} onDeleteAll={deleteAll} /> |
236 | | - <ModalCardEdit card={getCardByIndex(editCardId)} index={editCardId} /> |
237 | | - |
238 | | - {cardsArr && cardsArr.length ? ( |
239 | | - <CardList cards={cardsArr} /> |
240 | | - ) : (loading.state || !loading.res) ? null : logged ? ( |
241 | | - <div className="container text-center"> |
242 | | - <p className="m-3 p-3 h5 text-muted">No cards. You can add a new one!</p> |
243 | | - </div> |
244 | | - ) : ( |
245 | | - <div className="container text-center"> |
246 | | - <p className="m-3 p-3 h5 text-muted">Unlogged</p> |
247 | | - </div> |
248 | | - )} |
249 | | - |
250 | | - {(logged && !loading.state && !loading.res) && ( |
251 | | - <div className="container text-center"> |
252 | | - <p className="m-3 p-3 h5 text-muted">Data not loaded</p> |
253 | | - </div> |
254 | | - )} |
255 | | - |
256 | | - {loading.state && |
257 | | - <div className="container display-4 text-center p-3" > |
258 | | - <Loader /> |
259 | | - </div> |
260 | | - } |
261 | | - </main> |
262 | | - </div> |
263 | | - </Context.Provider> |
264 | | - ); |
| 31 | + <AuthContext.Provider value={{ |
| 32 | + token, login, logout, userId, email, isAuthenticated |
| 33 | + }}> |
| 34 | + <PageContext.Provider value={{setNav}}> |
| 35 | + <Router> |
| 36 | + <Header>{nav}</Header> |
| 37 | + <div className="App pb-3 mb-3"> |
| 38 | + {routes} |
| 39 | + </div> |
| 40 | + </Router> |
| 41 | + </PageContext.Provider> |
| 42 | + |
| 43 | + </AuthContext.Provider> |
| 44 | + ) |
265 | 45 | } |
266 | 46 |
|
267 | 47 | export default App; |
0 commit comments