/
index.js
126 lines (113 loc) · 3.51 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { createAsyncThunk } from '@reduxjs/toolkit';
import { URL, TOKEN } from '../constants/constants';
const CHANGE_FILTER = 'CHANGE_FILTER';
const setCategory = filter => ({
type: CHANGE_FILTER,
payload: {
filter,
},
});
const initCreator = (verb, data = null) => {
let result;
if (data) {
result = {
method: verb,
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: new Headers({
'Content-Type': 'application/json',
Authorization: `Token token=${TOKEN}`,
}),
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data),
};
} else {
result = {
method: verb,
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: new Headers({
'Content-Type': 'application/json',
Authorization: `Token token=${TOKEN}`,
}),
redirect: 'follow',
referrerPolicy: 'no-referrer',
};
}
return result;
};
const createBook = createAsyncThunk('create', async ({
book, id,
}) => {
const data = { id, book };
const init = initCreator('POST', data);
const response = await fetch(`${URL}books`, init)
.then(data => data.json())
.catch(error => error.json());
return response;
});
const retrieveBooks = createAsyncThunk('retrieve', async data => {
const init = initCreator('POST', data);
const response = await fetch(`${URL}user-books`, init)
.then(data => data.json())
.catch(error => error);
return response;
});
const updateBook = createAsyncThunk('update-book', async data => {
const init = initCreator('PUT', { book: data.book });
const response = await fetch(`${URL}books/${data.id}`, init)
.then(data => data.json())
.catch(error => error.json());
return { reduxId: data.reduxId, response };
});
const removeBook = createAsyncThunk('remove-book', async data => {
const init = initCreator('DELETE');
const response = await fetch(`${URL}books/${data.id}`, init)
.then(data => data.json())
.catch(error => error.json());
return { reduxId: data.reduxId, response };
});
const retrieveComments = createAsyncThunk('retrieve-comments', async data => {
const init = initCreator('GET');
const response = await fetch(`${URL}comments/${data.id}`, init)
.then(data => data.json())
.catch(error => error);
return { reduxId: data.reduxId, response };
});
const createComment = createAsyncThunk('create-comment', async data => {
const init = initCreator('POST', data.item);
const response = await fetch(`${URL}comments`, init)
.then(data => data.json())
.catch(error => error.json());
return { reduxId: data.reduxId, response };
});
const updateComment = createAsyncThunk('update-comment', async data => {
const init = initCreator('PUT', { body: data.item.body });
const response = await fetch(`${URL}comments/${data.item.id}`, init)
.then(data => data.json())
.then(error => error.json());
return { reduxId: data.reduxId, reduxCommentId: data.reduxCommentId, response };
});
const destroyComment = createAsyncThunk('destroy-comment', async data => {
const init = initCreator('DELETE');
const response = await fetch(`${URL}comments/${data.id}`, init)
.then(data => data.json())
.catch(error => error.json());
return { reduxId: data.reduxId, reduxCommentId: data.reduxCommentId, response };
});
export {
setCategory,
CHANGE_FILTER,
initCreator,
retrieveBooks,
createBook,
updateBook,
removeBook,
retrieveComments,
createComment,
updateComment,
destroyComment,
};