File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -33,4 +33,3 @@ export const cache: InMemoryCache = new InMemoryCache({
33
33
export const isLoggedInVar =
34
34
cache . makeLocalVar < boolean > ( ! ! localStorage . getItem ( 'token' ) ) ;
35
35
export const cartItemsVar = cache . makeLocalVar < string [ ] > ( [ ] ) ;
36
-
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import styled from 'react-emotion' ;
3
+ import { useApolloClient } from '@apollo/client' ;
3
4
4
5
import { menuItemClassName } from '../components/menu-item' ;
5
6
import { isLoggedInVar } from '../cache' ;
6
7
import { ReactComponent as ExitIcon } from '../assets/icons/exit.svg' ;
7
8
8
9
const LogoutButton = ( ) => {
10
+ const { cache } = useApolloClient ( ) ;
9
11
return (
10
12
< StyledButton
11
13
data-testid = "logout-button"
12
14
onClick = { ( ) => {
15
+ // Since we're logging out, remove all traces of the current user
16
+ // from the cache. First use `cache.modify()` to remove the stored
17
+ // `me` reference that was added to the cache by the `GET_MY_TRIPS`
18
+ // query in `profile.tsx`. Then trigger garbage collection using
19
+ // `cache.gc()` to remove the cached `User` object that is no longer
20
+ // reachable.
21
+ cache . modify (
22
+ 'ROOT_QUERY' ,
23
+ {
24
+ me ( _ , { DELETE } ) {
25
+ return DELETE ;
26
+ }
27
+ }
28
+ ) ;
29
+ cache . gc ( ) ;
30
+
31
+ // Remove user details from localStorage.
13
32
localStorage . clear ( ) ;
33
+
34
+ // Let other parts of the application that are relying on logged in
35
+ // state know we're now logged out.
14
36
isLoggedInVar ( false ) ;
15
37
16
38
// TODO: This redirect is temporary. Eventually `makeLocalVar`
You can’t perform that action at this time.
0 commit comments