Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createActions, createReducer, resettableReducer } from 'reduxsauce';
- import { createBasicSelectors } from '../utils/selectors';
- export const Key = 'library';
- export const { Types, Creators } = createActions(
- {
- reset: null,
- getBooksRequest: null,
- getBooksSuccess: ['books'],
- getBooksFailure: ['error'],
- uploadBookRequest: ['file', 'name'],
- uploadBookSuccess: ['book'],
- uploadBookFailure: ['error'],
- deleteBookRequest: ['name'],
- deleteBookSuccess: ['book'],
- deleteBookFailure: ['error'],
- },
- {
- prefix: `${Key}_`.toUpperCase(),
- },
- );
- export const InitialState = {
- books: [],
- loading: false,
- loadingError: false,
- uploading: false,
- uploadingError: false,
- deleting: false,
- deletingError: false,
- };
- export const Selectors = (() => {
- const root = (state) => state[Key];
- return Object.assign(
- { root },
- createBasicSelectors(root, Object.keys(InitialState)),
- );
- })();
- export const Handlers = {
- [Types.GET_BOOKS_REQUEST]: (state) => ({
- ...state,
- loading: true,
- loadingError: false,
- }),
- [Types.GET_BOOKS_SUCCESS]: (state, { books }) => ({
- ...state,
- books,
- loading: false,
- loadingError: false,
- }),
- [Types.GET_BOOKS_FAILURE]: (state, { error }) => ({
- ...state,
- loading: false,
- loadingError: error,
- }),
- [Types.UPLOAD_BOOK_REQUEST]: (state, { name }) => ({
- ...state,
- uploading: name,
- uploadingError: false,
- }),
- [Types.UPLOAD_BOOK_SUCCESS]: (state, { book }) => ({
- ...state,
- books: [book, ...state.books],
- uploading: false,
- uploadingError: false,
- }),
- [Types.UPLOAD_BOOK_FAILURE]: (state, { error }) => ({
- ...state,
- uploading: false,
- uploadingError: error,
- }),
- [Types.DELETE_BOOK_REQUEST]: (state, { name }) => ({
- ...state,
- deleting: name,
- deletingError: false,
- }),
- [Types.DELETE_BOOK_SUCCESS]: (state, { book }) => ({
- ...state,
- books: state.books.filter(({ name }) => name !== book.name),
- deleting: false,
- deletingError: false,
- }),
- [Types.DELETE_BOOK_FAILURE]: (state, { error }) => ({
- ...state,
- deleting: false,
- deletingError: error,
- }),
- };
- export const Reducer = resettableReducer(
- Types.RESET,
- createReducer(InitialState, Handlers),
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement