Created
April 26, 2018 07:10
-
-
Save oops-wrong/31b18d52dd744d3511e6cbc7d56e0037 to your computer and use it in GitHub Desktop.
ngrx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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