Skip to content

Commit 861fecb

Browse files
committed
header fix and anti ghost requests fix
1 parent 0cfef7a commit 861fecb

File tree

5 files changed

+66
-31
lines changed

5 files changed

+66
-31
lines changed

client/src/App.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import { BrowserRouter as Router } from 'react-router-dom'
88
import { useRoutes } from './routes'
99
import { useAuth } from './hooks/auth.hook'
1010
import { AuthContext } from './context/AuthContext'
11+
import { PageContext } from './context/PageContext'
12+
import Header from './pages/SharedComponents/Header'
1113

1214
function App() {
1315
const { token, login, logout, userId, email, ready } = useAuth()
1416
const isAuthenticated = !!token
1517
const routes = useRoutes(isAuthenticated)
1618

19+
const [nav, setNav] = React.useState()
20+
1721
if (!ready) {
1822
return (
1923
<div className="container display-4 text-center p-3" >
@@ -22,15 +26,20 @@ function App() {
2226
)
2327
}
2428

29+
2530
return (
2631
<AuthContext.Provider value={{
2732
token, login, logout, userId, email, isAuthenticated
2833
}}>
29-
<Router>
34+
<PageContext.Provider value={{setNav}}>
35+
<Router>
36+
<Header>{nav}</Header>
3037
<div className="App pb-3 mb-3">
3138
{routes}
3239
</div>
3340
</Router>
41+
</PageContext.Provider>
42+
3443
</AuthContext.Provider>
3544
)
3645
}

client/src/context/PageContext.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createContext } from 'react'
2+
3+
function noop() { }
4+
5+
export const PageContext = createContext({ setNav: noop })

client/src/pages/AuthPage.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import React, { useContext, useEffect, useState } from 'react'
22
import { useHttp } from '../hooks/http.hook'
33

44
import { AuthContext } from '../context/AuthContext'
5-
import Header from './SharedComponents/Header'
5+
import { PageContext } from '../context/PageContext'
6+
67
import { NavLink, useHistory } from 'react-router-dom'
78
import './AuthPage.css'
89

910
function AuthPage() {
1011
const auth = useContext(AuthContext)
12+
const page = useContext(PageContext)
1113
const history = useHistory()
1214

1315
const { loading, request, error, clearError } = useHttp()
@@ -45,15 +47,18 @@ function AuthPage() {
4547
auth.logout()
4648
}
4749

50+
React.useEffect(() => {
51+
page.setNav(auth.isAuthenticated &&
52+
<NavLink to="/notes" className="btn btn-light m-1">
53+
<span><i className="bi bi-x d-inline d-sm-none"></i> К заметкам</span>
54+
</NavLink>
55+
)
56+
// eslint-disable-next-line react-hooks/exhaustive-deps
57+
}, [auth.isAuthenticated, auth.token])
58+
4859
return (
4960
<div className="AuthPage">
50-
<Header >
51-
{auth.isAuthenticated &&
52-
<NavLink to="/notes" className="btn btn-light m-1">
53-
<span><i className="bi bi-x d-inline d-sm-none"></i> К заметкам</span>
54-
</NavLink>
55-
}
56-
</Header>
61+
5762

5863
<div className="p-1 pb-3 mb-3 container" >
5964
<div className=" form-body mx-auto ">

client/src/pages/CardsPage.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import Loader from '../shared/Loader'
88
import ModalCardEdit from '../Cards/ModalCardEdit'
99

1010
import Card, { checkCardsArr } from '../Cards/cardType/Card'
11-
import Header from "./SharedComponents/Header"
11+
1212

1313
import { NavLink } from 'react-router-dom'
1414
import { AuthContext } from '../context/AuthContext'
15+
import { PageContext } from '../context/PageContext'
1516

1617
import { useHttp } from '../hooks/http.hook'
1718

@@ -29,17 +30,22 @@ function useCardsArr(defaultValue) {
2930
function 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

4044
function 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 />

routes/notes.routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ router.post('/delete', auth, async (req, res) => {
6464
//console.log("EXITING");
6565

6666
existing.remove()
67-
console.log("fdsdf");
67+
6868
} else res.status(500).json({ message: 'уже удален' })
6969
}
7070
})

0 commit comments

Comments
 (0)