Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions final/client/src/containers/logout-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ export default function LogoutButton() {
<StyledButton
data-testid="logout-button"
onClick={() => {
client.writeData({ data: { isLoggedIn: false } });
localStorage.clear();
client.writeData({
data: {
isLoggedIn: false,
cartItems: [],
},
});
localStorage.setItem('token', '');
}}
>
<ExitIcon />
Expand Down
6 changes: 4 additions & 2 deletions final/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ const client = new ApolloClient({
typeDefs,
});

const token = localStorage.getItem('token');
const cart = localStorage.getItem('cart');
cache.writeData({
data: {
isLoggedIn: !!localStorage.getItem('token'),
cartItems: [],
isLoggedIn: !!token,
cartItems: token && cart && JSON.parse(cart)[token] ? JSON.parse(cart)[token] : [],
},
});

Expand Down
8 changes: 7 additions & 1 deletion final/client/src/pages/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export default function Login() {
{
onCompleted({ login }) {
localStorage.setItem('token', login);
client.writeData({ data: { isLoggedIn: true } });
const cart = localStorage.getItem('cart');
client.writeData({
data: {
isLoggedIn: true,
cartItems: login && cart && JSON.parse(cart)[login] ? JSON.parse(cart)[login] : [],
},
});
}
}
);
Expand Down
3 changes: 3 additions & 0 deletions final/client/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const resolvers = {
: [...cartItems, id],
};
cache.writeQuery({ query: GET_CART_ITEMS, data });
const token = localStorage.getItem('token');
const cartObj = JSON.parse(localStorage.getItem('cart'));
localStorage.setItem('cart', JSON.stringify({...cartObj, [token]: data.cartItems}));
return data.cartItems;
},
},
Expand Down