UNPKG

react-behave

Version:

A library of behaviors for React applications.

46 lines (43 loc) 929 B
var INITIAL_STATE = { value: null, error: null, pending: false }; export var START = 'START'; export var SUCCESS = 'SUCCESS'; export var ERROR = 'ERROR'; export function initializeStateWithValue(value) { return Object.assign({}, INITIAL_STATE, { value: value }); } export function asyncStateReducer(state, action) { switch (action.type) { case START: { return Object.assign({}, state, { pending: true }); } case SUCCESS: { return { value: action.value, error: null, pending: false }; } case ERROR: { // We don't want to loose a previous value in case of later error. return Object.assign({}, state, { error: action.error, pending: false }); } default: { throw new Error("Unknown action type ".concat(action.type)); } } }