-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketLayout.tsx
More file actions
53 lines (47 loc) · 1.08 KB
/
SocketLayout.tsx
File metadata and controls
53 lines (47 loc) · 1.08 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
import { gql, useQuery } from '@apollo/client'
import { Outlet, useNavigate } from 'react-router-dom'
import { useClearUser } from 'store/action'
import store from 'store/index'
import { GetMeQuery } from '__api__/types'
const GET_ME = gql`
query getMe {
getMe {
ok
user {
id
nickname
createdAt
updatedAt
role
}
error
}
}
`
const SocketLayout = () => {
const key = import.meta.env.VITE_AUTH_KEY
const token = localStorage.getItem(key) || ''
const { setUser, clear } = store((state) => ({
setUser: state.setUser,
clear: state.clear,
}))
const { data, loading, client } = useQuery<GetMeQuery>(GET_ME, {
context: {
headers: {
[key]: token,
},
},
onCompleted({ getMe: { ok, error, user } }) {
if (ok && user) {
setUser({ user, token })
} else {
clear()
navigate('/login')
}
},
fetchPolicy: 'no-cache',
})
const navigate = useNavigate()
return <div>{loading ? 'loading...' : <Outlet />}</div>
}
export default SocketLayout