This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
/
crudResponse.js
109 lines (107 loc) · 3.89 KB
/
crudResponse.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { all, put, takeEvery } from 'redux-saga/effects';
import { push } from 'react-router-redux';
import { reset } from 'redux-form';
import {
CRUD_CREATE_FAILURE,
CRUD_CREATE_SUCCESS,
CRUD_DELETE_FAILURE,
CRUD_DELETE_SUCCESS,
CRUD_GET_LIST_FAILURE,
CRUD_GET_MANY_FAILURE,
CRUD_GET_MANY_REFERENCE_FAILURE,
CRUD_GET_ONE_FAILURE,
CRUD_UPDATE_FAILURE,
CRUD_UPDATE_SUCCESS,
} from '../../actions/dataActions';
import { showNotification } from '../../actions/notificationActions';
import resolveRedirectTo from '../../util/resolveRedirectTo';
/**
* Side effects for fetch responses
*
* Mostly redirects and notifications
*/
function* handleResponse({ type, requestPayload, error, payload }) {
switch (type) {
case CRUD_UPDATE_SUCCESS:
return requestPayload.redirectTo
? yield all([
put(showNotification('aor.notification.updated')),
put(
push(
resolveRedirectTo(
requestPayload.redirectTo,
requestPayload.basePath,
requestPayload.id
)
)
),
])
: yield [put(showNotification('aor.notification.updated'))];
case CRUD_CREATE_SUCCESS:
return requestPayload.redirectTo
? yield all([
put(showNotification('aor.notification.created')),
put(
push(
resolveRedirectTo(
requestPayload.redirectTo,
requestPayload.basePath,
payload.data.id
)
)
),
])
: yield all([
put(showNotification('aor.notification.created')),
put(reset('record-form')),
]);
case CRUD_DELETE_SUCCESS:
return requestPayload.redirectTo
? yield all([
put(showNotification('aor.notification.deleted')),
put(
push(
resolveRedirectTo(
requestPayload.redirectTo,
requestPayload.basePath,
requestPayload.id
)
)
),
])
: yield [put(showNotification('aor.notification.deleted'))];
case CRUD_GET_ONE_FAILURE:
return requestPayload.basePath
? yield all([
put(
showNotification(
'aor.notification.item_doesnt_exist',
'warning'
)
),
put(push(requestPayload.basePath)),
])
: yield all([]);
case CRUD_GET_LIST_FAILURE:
case CRUD_GET_MANY_FAILURE:
case CRUD_GET_MANY_REFERENCE_FAILURE:
case CRUD_CREATE_FAILURE:
case CRUD_UPDATE_FAILURE:
case CRUD_DELETE_FAILURE: {
console.error(error); // eslint-disable-line no-console
const errorMessage =
typeof error === 'string'
? error
: error.message || 'aor.notification.http_error';
return yield put(showNotification(errorMessage, 'warning'));
}
default:
return yield all([]);
}
}
export default function*() {
yield takeEvery(
action => action.meta && action.meta.fetchResponse,
handleResponse
);
}