-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.js
45 lines (41 loc) · 1.15 KB
/
books.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
import { v4 as uuidv4 } from 'uuid';
import { getBooksAPI, addBookAPI, removeBookAPI } from '../../API/app';
const ADD_BOOK = 'NEW_BOOK_ADD';
const REMOVE_BOOK = 'BOOK_REMOVED';
const initialState = [];
export const getBooksFromServer = () => async (dispatch) => {
const data = await getBooksAPI();
const result = Object.entries(data).map((data) => {
let [, res] = data;
[res] = res;
[res.id] = data;
return (res);
});
dispatch(({ type: ADD_BOOK, payload: result }));
};
export const addBook = (title, author) => (dispatch) => {
const book = {
id: uuidv4(), title, author, category: 'not set',
};
addBookAPI(book).then(() => {
dispatch({ type: ADD_BOOK, payload: [book] });
});
};
export const removeBook = (id) => (dispatch) => {
removeBookAPI(id).then(() => {
dispatch({ type: REMOVE_BOOK, payload: id });
});
};
export default (state = initialState, action) => {
switch (action.type) {
case ADD_BOOK:
return [
...state,
...action.payload,
];
case REMOVE_BOOK:
return state.filter((book) => (book.id !== action.payload ? book : false));
default:
return state;
}
};