Skip to content

Instantly share code, notes, and snippets.

@karptonite
Last active July 19, 2017 18:10
Show Gist options
  • Save karptonite/807150583a086ed8736f129d8094e88c to your computer and use it in GitHub Desktop.
Save karptonite/807150583a086ed8736f129d8094e88c to your computer and use it in GitHub Desktop.
// This function takes a reducer and a name, and returns a reducer that essentially filters on the name.
// Useful for when you have multiple reducers of the same form.
export function createNamedWrapperReducer<T>(
reducerFunction: ( ( state, action ) => T ), reducerName: string ): ( ( state, action ) => T ) {
return ( state, action ) => {
const { name } = action;
const isInitializationCall = state === undefined;
if ( name !== reducerName && !isInitializationCall ) {
return state;
}
return reducerFunction( state, action );
}
}
import { addToList } from '@geek/store/shared/type-sorted-list.functions';
import { TypeSortedList } from '@geek/store/shared/type-sorted-list.interface';
import { createNamedWrapperReducer } from '@geek/store/meta/meta-reducers.functions';
import { SubscriptionStatus } from '@geek/store/subscriptions/subscription-status.model';
import { SubscriptionType } from '@geek/store/subscriptions/subscription-type.type';
import * as subscriptionsActions from '@geek/store/subscriptions/subscriptions.actions';
import * as _ from 'lodash';
export const defaultStatus: SubscriptionStatus = {
exists: false,
loading: false,
firstLoad: false,
};
export interface State {
statuses: TypeSortedList<SubscriptionStatus>;
}
const initialState: State = {
statuses: {},
};
export function reducer( state = initialState, action: subscriptionsActions.Actions ): State {
let type, id;
let statuses;
let status;
switch ( action.type ) {
case subscriptionsActions.LOAD_STATUS_BY_ITEM:
({ type, id } = action.payload.parent);
status = _.get( state.statuses, [type, id], defaultStatus );
statuses = addToList( state.statuses, type, id, {...status, loading: true } );
return { statuses };
case subscriptionsActions.LOAD_STATUS_BY_ITEM_SUCCESS:
({ type, id } = action.payload.parent);
statuses = addToList(
state.statuses, type, id, { exists: action.payload.results, loading: false, firstLoad: true } );
return { statuses };
case subscriptionsActions.ADD:
({ type, id } = action.payload.parent);
statuses = addToList(
state.statuses, type, id, { exists: true, loading: true, firstLoad: false } );
return { statuses };
case subscriptionsActions.REMOVE:
({ type, id } = action.payload.parent);
statuses = addToList(
state.statuses, type, id, { exists: false, loading: true, firstLoad: false } );
return { statuses };
case subscriptionsActions.ADD_SUCCESS:
case subscriptionsActions.REMOVE_SUCCESS:
({ type, id } = action.payload.parent);
status = _.get( state.statuses, [type, id], defaultStatus );
statuses = addToList(
state.statuses, type, id, {...status, loading: false, firstLoad: true } );
return { statuses };
default:
return state;
}
}
export function createSubscriptionStatusesReducer( subscriptionType: SubscriptionType ) {
return createNamedWrapperReducer( reducer, subscriptionType );
}
export const getSubscriptionStatuses = ( state: State ) => state.statuses;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment