Skip to content

Instantly share code, notes, and snippets.

View asherccohen's full-sized avatar

Asher Cohen asherccohen

View GitHub Profile
import uuid from 'uuid';
export default (state = {}, action) => {
const { type, payload } = action;
const matches = /(.*)_(REQUEST|FAILURE)/.exec(type);
// not a *_REQUEST / *_FAILURE actions, so we ignore them
if (!matches) return state;
const [, requestName, requestState] = matches;
return {
…state,
// Store errorMessage
@asherccohen
asherccohen / LoadingReducer.js
Created July 26, 2019 11:13
LoadingReducer
export default (state = {}, action) => {
const { type } = action;
const matches = /(.*)_(REQUEST|SUCCESS|FAILURE)/.exec(type);
// not a *_REQUEST / *_SUCCESS / *_FAILURE actions, so we ignore them
if (!matches) return state;
const [, requestName, requestState] = matches;
return {
…state,
// Store whether a request is happening at the moment or not
// e.g. will be true when receiving FETCH_TODOS_REQUEST
@asherccohen
asherccohen / reducerActionTypes.JS
Created July 26, 2019 11:11
reducerActionTypes
export const FETCH_TODOS_REQUEST = 'FETCH_TODOS_REQUEST';
export const FETCH_TODOS_SUCCESS = 'FETCH_TODOS_SUCCESS';
export const FETCH_TODOS_FAILURE = 'FETCH_TODOS_FAILURE';
// todo/actions.js
export const getEvents = (dispatch) => () => {
dispatch({ type: 'GET_EVENTS_REQUEST' });
return fetch('/api/v1/events')
.then((todos) => dispatch({ type: 'GET_EVENTS_SUCCESS', payload: todos })
.catch((error) => dispatch({ type: 'GET_EVENTS_FAILURE', payload: error, error: true });
};
// events/reducer.js
const initialState = { todos: [] };
export const eventsReducer = (state = initialState, action) => {
switch(action.type) {
case 'GET_EVENTS_REQUEST':
return { ...state, isFetching: true };
case 'GET_EVENTS_SUCCESS':
return { ...state, isFetching: false, todos: action.payload };
case 'GET_EVENTS_FAILURE':
return { ...state, isFetching: false, errorMessage: action.payload.message };
import { createStore } from 'redux';
// local import
import reducers from '../reducers';
const store =
reducers &&
createStore(reducers);
export default store;
const INITIAL_STATE = {
orderList: {},
isRetrievingOrder: false,
errorRetrievingOrder: false,
errorRetrievingOrderMessage: null,
isSavingOrder: false,
errorSavingOrder: false,
errorSavingOrderMessage: null,
isCancellingOrder: false,
errorCancellingOrder: false,
const rootReducer = (state, action) => {
// when a logout action is dispatched it will reset redux state
if (action.type === 'USER_LOGGED_OUT') {
const { users, comment } = state;
state = { users, comment };
}
if (action.type === 'USER_LOGGED_IN') {
import { combineReducers } from 'redux';
import AppReducer from './AppReducer';
import UsersReducer from './UsersReducer';
import OrderReducer from './OrderReducer';
import NotificationReducer from './NotificationReducer';
import CommentReducer from './CommentReducer';
const appReducer = combineReducers({
/* your app’s top-level reducers */
const rootReducer = (state, action) => {
if (action.type === USER_LOGGED_OUT) {
// for all keys defined in your persistConfig(s)
storage.removeItem('persist:root')
// storage.removeItem('persist:otherKey')
state = undefined;
}
return appReducer(state, action);
};