Advertisement
Guest User

reduxsauce-example

a guest
Apr 9th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { createActions, createReducer, resettableReducer } from 'reduxsauce';
  2. import { createBasicSelectors } from '../utils/selectors';
  3.  
  4. export const Key = 'library';
  5.  
  6. export const { Types, Creators } = createActions(
  7.   {
  8.     reset: null,
  9.     getBooksRequest: null,
  10.     getBooksSuccess: ['books'],
  11.     getBooksFailure: ['error'],
  12.     uploadBookRequest: ['file', 'name'],
  13.     uploadBookSuccess: ['book'],
  14.     uploadBookFailure: ['error'],
  15.     deleteBookRequest: ['name'],
  16.     deleteBookSuccess: ['book'],
  17.     deleteBookFailure: ['error'],
  18.   },
  19.   {
  20.     prefix: `${Key}_`.toUpperCase(),
  21.   },
  22. );
  23.  
  24. export const InitialState = {
  25.   books: [],
  26.   loading: false,
  27.   loadingError: false,
  28.   uploading: false,
  29.   uploadingError: false,
  30.   deleting: false,
  31.   deletingError: false,
  32. };
  33.  
  34. export const Selectors = (() => {
  35.   const root = (state) => state[Key];
  36.   return Object.assign(
  37.     { root },
  38.     createBasicSelectors(root, Object.keys(InitialState)),
  39.   );
  40. })();
  41.  
  42. export const Handlers = {
  43.   [Types.GET_BOOKS_REQUEST]: (state) => ({
  44.     ...state,
  45.     loading: true,
  46.     loadingError: false,
  47.   }),
  48.   [Types.GET_BOOKS_SUCCESS]: (state, { books }) => ({
  49.     ...state,
  50.     books,
  51.     loading: false,
  52.     loadingError: false,
  53.   }),
  54.   [Types.GET_BOOKS_FAILURE]: (state, { error }) => ({
  55.     ...state,
  56.     loading: false,
  57.     loadingError: error,
  58.   }),
  59.   [Types.UPLOAD_BOOK_REQUEST]: (state, { name }) => ({
  60.     ...state,
  61.     uploading: name,
  62.     uploadingError: false,
  63.   }),
  64.   [Types.UPLOAD_BOOK_SUCCESS]: (state, { book }) => ({
  65.     ...state,
  66.     books: [book, ...state.books],
  67.     uploading: false,
  68.     uploadingError: false,
  69.   }),
  70.   [Types.UPLOAD_BOOK_FAILURE]: (state, { error }) => ({
  71.     ...state,
  72.     uploading: false,
  73.     uploadingError: error,
  74.   }),
  75.   [Types.DELETE_BOOK_REQUEST]: (state, { name }) => ({
  76.     ...state,
  77.     deleting: name,
  78.     deletingError: false,
  79.   }),
  80.   [Types.DELETE_BOOK_SUCCESS]: (state, { book }) => ({
  81.     ...state,
  82.     books: state.books.filter(({ name }) => name !== book.name),
  83.     deleting: false,
  84.     deletingError: false,
  85.   }),
  86.   [Types.DELETE_BOOK_FAILURE]: (state, { error }) => ({
  87.     ...state,
  88.     deleting: false,
  89.     deletingError: error,
  90.   }),
  91. };
  92.  
  93. export const Reducer = resettableReducer(
  94.   Types.RESET,
  95.   createReducer(InitialState, Handlers),
  96. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement