-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuser.actions.js
69 lines (62 loc) · 1.73 KB
/
user.actions.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
import { Alert } from 'react-native';
import config from 'resources/config';
import ApiError from 'helpers/api/api.error';
import { setItem, removeItem } from 'helpers/storage';
import {
USER_SIGNED_IN,
USER_LOGGED_OUT,
} from './user.constants';
import * as api from './user.api';
export const signUp = userData => async () => {
try {
const payload = await api.signUp(userData);
return payload;
} catch (error) {
if (error.status === 400) {
throw new ApiError(error.data, error.status);
} else {
Alert.alert(
'SignUp Failed',
'Sorry, something went wrong.',
[{ text: 'OK', style: 'cancel' }],
);
throw new ApiError({ errors: [] }, error.status);
}
}
};
export const signIn = (email, password) => async (dispatch) => {
try {
const userInfo = await api.signIn(email, password);
config.token = userInfo.accessToken;
await setItem('token', userInfo.accessToken);
dispatch({ type: USER_SIGNED_IN, userInfo });
} catch (error) {
if (error.status === 400) {
throw new ApiError(error.data, error.status);
} else {
Alert.alert(
'SignIn Failed',
'Sorry, something went wrong.',
[{ text: 'OK', style: 'cancel' }],
);
throw new ApiError({ errors: [] }, error.status);
}
}
};
export const signOut = () => async (dispatch) => {
try {
await api.signOut();
config.token = null;
await removeItem('token');
dispatch({ type: USER_LOGGED_OUT });
} catch (error) {
Alert.alert(
'SignOut Failed',
'Sorry, something went wrong.',
[{ text: 'OK', style: 'cancel' }],
);
}
};
export const verifyEmailDev = _signupToken => async () => {
await api.verifyEmailDev(_signupToken);
};