Skip to content

Instantly share code, notes, and snippets.

@oops-wrong
Created April 26, 2018 07:10
Show Gist options
  • Save oops-wrong/31b18d52dd744d3511e6cbc7d56e0037 to your computer and use it in GitHub Desktop.
Save oops-wrong/31b18d52dd744d3511e6cbc7d56e0037 to your computer and use it in GitHub Desktop.
ngrx
import { TableActions, TableActionTypes } from './table.actions';
import { initialState, State } from './table.state';
export * from './table.state';
export function reducer(state = initialState, action: TableActions): State {
switch (action.type) {
case TableActionTypes.LoadMoreTableModels:
return { ...state, loadedModelsCount: state.loadedModelsCount + action.payload.count };
case TableActionTypes.ResetTable:
return { ...initialState, ...action.payload };
case TableActionTypes.SortColumn:
return { ...state, ...action.payload };
case TableActionTypes.UpdateTableModels:
return { ...state, ...action.payload };
default:
return state;
}
}
export const selectIsFinishedModels = (state: State): boolean => {
return state.models.length <= state.loadedModelsCount;
};
export const selectSortedModels = (state: State): any[] => {
if (!state.active || state.direction === '') {
return state.models;
}
const sorted: any[] = Object.assign([], state.models);
sorted.sort((a, b) => {
if (a[state.active] > b[state.active]) {
return -1;
}
return 1;
});
if (state.direction === 'desc') {
return sorted.reverse();
}
return sorted;
};
export const selectModelsSlice = (state: State): any[] => {
const sortedModels = selectSortedModels(state);
return sortedModels.slice(0, state.loadedModelsCount);
};
export const selectTableModelsCount = (state: State): number => state.models.length;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment