-
Notifications
You must be signed in to change notification settings - Fork 2
/
sagas.ts
89 lines (74 loc) · 1.95 KB
/
sagas.ts
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
88
89
import { call, put, takeEvery } from "redux-saga/effects";
import {
fetchLoginError,
fetchLoginSuccess,
fetchRegisterError,
fetchRegisterSuccess,
fetchAuthSuccess,
fetchAuthError
} from "./actions";
import { login, register, logout, getAuthStatus } from "./api";
import { AuthActionTypes } from "./types";
function* loginFetch(data: { email: string; password: string }) {
try {
const { profile, idToken } = yield call(login, data.email, data.password);
yield put(fetchLoginSuccess({ profile, idToken }));
} catch (error) {
yield put(fetchLoginError(error));
}
}
function* registerFetch(data: {
email: string;
password: string;
fullname: string;
}) {
try {
const { profile, idToken } = yield call(
register,
data.email,
data.password,
data.fullname
);
yield put(fetchRegisterSuccess({ profile, idToken }));
} catch (error) {
yield put(fetchRegisterError(error));
}
}
function* authFetch() {
try {
const isAuthenticated = yield call(getAuthStatus);
yield put(fetchAuthSuccess(isAuthenticated));
} catch (error) {
yield put(fetchAuthError(error));
}
}
function* logoutFetch() {
try {
yield call(logout);
return;
} catch (error) {}
}
export function* loginWatcher({ payload }: any) {
yield call(loginFetch, payload);
}
export function* registerWatcher({ payload }: any) {
yield call(registerFetch, payload);
}
export function* authWatcher() {
yield call(authFetch);
}
export function* logoutWatcher() {
yield call(logoutFetch);
}
export function* watchUserLogin() {
yield takeEvery(AuthActionTypes.LOGIN_REQUEST as any, loginWatcher);
}
export function* watchUserRegister() {
yield takeEvery(AuthActionTypes.REGISTER_REQUEST as any, registerFetch);
}
export function* watchUserAuth() {
yield takeEvery(AuthActionTypes.AUTH_REQUEST as any, authFetch);
}
export function* watchUserLogout() {
yield takeEvery(AuthActionTypes.LOGOUT as any, logoutWatcher);
}