@@ -8,10 +8,11 @@ import Loader from '../shared/Loader'
88import ModalCardEdit from '../Cards/ModalCardEdit'
99
1010import Card , { checkCardsArr } from '../Cards/cardType/Card'
11- import Header from "./SharedComponents/Header"
11+
1212
1313import { NavLink } from 'react-router-dom'
1414import { AuthContext } from '../context/AuthContext'
15+ import { PageContext } from '../context/PageContext'
1516
1617import { useHttp } from '../hooks/http.hook'
1718
@@ -29,17 +30,22 @@ function useCardsArr(defaultValue) {
2930function useUpdater ( ) {
3031 const [ updaterVal , setUpdaterVal ] = React . useState ( null )
3132 const timer = React . useRef ( )
32- if ( timer . current ) clearTimeout ( timer . current )
33- timer . current = setTimeout ( ( ) => {
34- console . log ( "Timed update" )
35- setUpdaterVal ( Date . now ( ) )
36- } , 60 * 1000 ) // обновяем через минуту
33+ React . useEffect ( ( ) => {
34+ if ( timer . current ) clearTimeout ( timer . current )
35+ timer . current = setTimeout ( ( ) => {
36+ console . log ( "Timed update" )
37+ setUpdaterVal ( Date . now ( ) )
38+ } , 60 * 1000 ) // обновяем через минуту
39+ return ( ) => clearTimeout ( timer . current )
40+ } , [ ] ) // eslint-disable-line react-hooks/exhaustive-deps
3741 return [ updaterVal ]
3842}
3943
4044function CardsPage ( ) {
4145
4246 const auth = React . useContext ( AuthContext )
47+ const page = React . useContext ( PageContext )
48+
4349 const { loading, request, error, clearError } = useHttp ( )
4450 const fetchNotes = React . useCallback ( async ( url = "" , method = "GET" , body = null , resCallback = ( ) => { } ) => {
4551 try {
@@ -69,7 +75,14 @@ function CardsPage() {
6975
7076 const [ updaterVal ] = useUpdater ( )
7177
72- React . useEffect ( loadDataFromServer , [ auth . isAuthenticated , auth . email , updaterVal ] ) // eslint-disable-line react-hooks/exhaustive-deps
78+ const updatingEnable = React . useRef ( true )
79+
80+ React . useEffect ( ( ) => {
81+ updatingEnable . current = true
82+ loadDataFromServer ( )
83+ return ( ) => updatingEnable . current = false
84+ } , [ auth . isAuthenticated , auth . email , updaterVal ] ) // eslint-disable-line react-hooks/exhaustive-deps
85+
7386 React . useEffect ( clearOldData , [ auth . isAuthenticated ] ) // eslint-disable-line react-hooks/exhaustive-deps
7487
7588 ///////////
@@ -85,7 +98,7 @@ function CardsPage() {
8598 }
8699
87100 function setLoadedCards ( cards ) {
88- setCardsArr ( [ ...cards ] )
101+ if ( updatingEnable . current ) setCardsArr ( [ ...cards ] )
89102 }
90103 ///////////
91104
@@ -142,22 +155,25 @@ function CardsPage() {
142155 }
143156 ///////////
144157
158+ React . useEffect ( ( ) => {
159+ page . setNav (
160+ < React . Fragment >
161+ < button className = "btn btn-light m-1" onClick = { loadDataFromServer } >
162+ { loading ? < Loader className = 'px-1' /> : < i className = "bi bi-arrow-clockwise px-1" > </ i > }
163+ < span className = 'd-xl-inline d-none' > Update</ span >
164+ </ button >
165+
166+ < NavLink to = "/authpage" className = "btn btn-light m-1" >
167+ < span > < i className = "bi bi-person" > </ i > { auth . email } </ span >
168+ </ NavLink >
169+ </ React . Fragment >
170+ )
171+ // eslint-disable-next-line react-hooks/exhaustive-deps
172+ } , [ auth . email , auth . token ] )
173+
145174 return (
146175 < CardsContext . Provider value = { { addCard, removeCard, changeCardColor, setEditCard, unsetEditCard, editCardContent, editCardId } } >
147176 < div className = "" >
148- < Header >
149-
150- < button className = "btn btn-light m-1" onClick = { loadDataFromServer } >
151- { loading ? < Loader className = 'px-1' /> : < i className = "bi bi-arrow-clockwise px-1" > </ i > }
152- < span className = 'd-xl-inline d-none' > Update</ span >
153- </ button >
154-
155-
156- < NavLink to = "/authpage" className = "btn btn-light m-1" >
157- < span > < i className = "bi bi-person" > </ i > { auth . email } </ span >
158- </ NavLink >
159-
160- </ Header >
161177
162178 < main className = "p-1 pb-3 mb-3" >
163179 < AddCard />
0 commit comments