-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathasyncActions.js
executable file
·87 lines (67 loc) · 2.67 KB
/
asyncActions.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import BrowserPersistence from '../../../util/simplePersistence';
import { removeCart } from '../cart';
import { clearCheckoutDataFromStorage } from '../checkout';
import actions from './actions';
const storage = new BrowserPersistence();
export const signOut = (payload = {}) =>
async function thunk(dispatch, getState, { apolloClient }) {
const { revokeToken } = payload;
if (revokeToken) {
// Send mutation to revoke token.
try {
await revokeToken();
} catch (error) {
console.error('Error Revoking Token', error);
}
}
// Remove token from local storage and Redux.
await dispatch(clearToken());
await dispatch(actions.reset());
await clearCheckoutDataFromStorage();
await apolloClient.clearCacheData(apolloClient, 'cart');
await apolloClient.clearCacheData(apolloClient, 'customer');
// Now that we're signed out, forget the old (customer) cart.
// We don't need to create a new cart here because we're going to refresh
// the page immediately after.
await dispatch(removeCart());
};
export const getUserDetails = ({ fetchUserDetails }) =>
async function thunk(...args) {
const [dispatch, getState] = args;
const { user } = getState();
if (user.isSignedIn) {
dispatch(actions.getDetails.request());
try {
const { data } = await fetchUserDetails();
dispatch(actions.getDetails.receive(data.customer));
} catch (error) {
dispatch(actions.getDetails.receive(error));
}
}
};
export const resetPassword = ({ email }) =>
async function thunk(...args) {
const [dispatch] = args;
dispatch(actions.resetPassword.request());
// TODO: actually make the call to the API.
// For now, just return a resolved promise.
await Promise.resolve(email);
dispatch(actions.resetPassword.receive());
};
export const setToken = (token, customer_token_lifetime = 3600) =>
async function thunk(...args) {
const [dispatch] = args;
// Store token in local storage.
// TODO: Get correct token expire time from API
storage.setItem('signin_token', token, customer_token_lifetime);
// Persist in store
dispatch(actions.setToken(token));
};
export const clearToken = () =>
async function thunk(...args) {
const [dispatch] = args;
// Clear token from local storage
storage.removeItem('signin_token');
// Remove from store
dispatch(actions.clearToken());
};