Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export interface Loadable {
- isLoading: boolean;
- success: boolean;
- error: any | null;
- }
- export interface LoadableActionTypes {
- isLoadingAction: string;
- successAction: string;
- errorAction: string;
- clearAction: string;
- }
- export const INITIAL_LOADABLE_STATE: Loadable = {
- isLoading: false,
- success: false,
- error: null,
- };
- export const withLoadable = (actionTypes: LoadableActionTypes) =>
- reducer =>
- (state, action) => {
- switch (action.type) {
- case actionTypes.isLoadingAction: {
- return {
- ...state,
- isLoading: true,
- success: false,
- error: null,
- };
- }
- case actionTypes.errorAction: {
- return {
- ...state,
- isLoading: false,
- error: action.payload,
- success: false,
- };
- }
- case actionTypes.successAction: {
- return {
- ...state,
- ...action.payload,
- isLoading: false,
- success: true,
- error: null,
- };
- }
- case actionTypes.clearAction: {
- return {
- ...state,
- ...INITIAL_LOADABLE_STATE,
- };
- }
- default: {
- return reducer(state, action);
- }
- }
- };
Add Comment
Please, Sign In to add comment