This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathapi.js
More file actions
97 lines (83 loc) · 2.46 KB
/
Copy pathapi.js
File metadata and controls
97 lines (83 loc) · 2.46 KB
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
90
91
92
93
94
95
96
97
import {
takeLatest, put, all, call,
} from 'redux-saga/effects'
import { api } from 'services/api'
import * as actionTypes from 'constants/actionTypes'
import { NETWORK_ERROR, UNKNOWN_ERROR } from 'constants/errorCodes'
import { updateApplication } from 'actions/ApplicationActions'
import { handleLoginSuccess, handleLoginError, logout as logoutAction } from 'actions/AuthActions'
export function* fetchForm() {
try {
const response = yield call(api.form)
yield put({ type: actionTypes.FETCH_FORM_SUCCESS, response })
} catch (error) {
yield put({ type: actionTypes.FETCH_FORM_ERROR, error })
}
}
export function* fetchStatus() {
try {
const response = yield call(api.status)
yield put({ type: actionTypes.FETCH_STATUS_SUCCESS, response })
} catch (error) {
yield put({ type: actionTypes.FETCH_STATUS_ERROR, error })
}
}
export function* login({ username, password }) {
try {
const response = yield call(api.login, username, password)
yield put(handleLoginSuccess(response))
} catch (error) {
// Expected error format:
// { errors: [{ message: "", code: "" }, { message: "", code: "" }] }
if (error.response) {
const { data, status } = error.response
if (data && data.errors) {
yield put(handleLoginError(data.errors))
} else {
yield put(handleLoginError([{
message: UNKNOWN_ERROR,
code: UNKNOWN_ERROR,
status,
}]))
}
} else {
// No response - API unreachable
yield put(handleLoginError([{ message: NETWORK_ERROR, code: NETWORK_ERROR }]))
}
}
}
export function* renewSession() {
try {
yield call(api.refresh)
yield put(updateApplication('Settings', 'lastRefresh', new Date().getTime()))
} catch (error) {
yield put(logoutAction(true))
}
}
export function* logout() {
yield call(api.logout)
}
export function* loginWatcher() {
yield takeLatest(actionTypes.LOGIN, login)
}
export function* fetchFormWatcher() {
yield takeLatest(actionTypes.FETCH_FORM, fetchForm)
}
export function* fetchStatusWatcher() {
yield takeLatest(actionTypes.FETCH_STATUS, fetchStatus)
}
export function* renewSessionWatcher() {
yield takeLatest(actionTypes.RENEW_SESSION, renewSession)
}
export function* logoutWatcher() {
yield takeLatest(actionTypes.LOGOUT, logout)
}
export function* apiWatcher() {
yield all([
fetchFormWatcher(),
fetchStatusWatcher(),
loginWatcher(),
renewSessionWatcher(),
logoutWatcher(),
])
}