55946: object-select store lists intermediate commit

This commit is contained in:
Kristof De Langhe
2018-11-15 14:13:16 +01:00
parent b11a168e72
commit 08063154e7
3 changed files with 84 additions and 47 deletions

View File

@@ -2,36 +2,45 @@ import { isEmpty } from '../empty.util';
import { ObjectSelectionAction, ObjectSelectionActionTypes } from './object-select.actions';
/**
* Interface that represents the state for a single filters
* Interface that represents the state for a single selection of an object
*/
export interface ObjectSelectionState {
checked: boolean;
}
/**
* Interface that represents the state for all available filters
* Interface that represents the state for all selected items within a certain category defined by a key
*/
export interface ObjectSelectionsState {
[id: string]: ObjectSelectionState
}
const initialState: ObjectSelectionsState = Object.create(null);
/**
* Interface that represents the state for all selected items
*/
export interface ObjectSelectionListState {
[key: string]: ObjectSelectionsState
}
const initialState: ObjectSelectionListState = Object.create(null);
/**
* Performs a search filter action on the current state
* @param {SearchFiltersState} state The state before the action is performed
* @param {SearchFilterAction} action The action that should be performed
* @returns {SearchFiltersState} The state after the action is performed
* Performs a selection action on the current state
* @param {ObjectSelectionListState} state The state before the action is performed
* @param {ObjectSelectionAction} action The action that should be performed
* @returns {ObjectSelectionListState} The state after the action is performed
*/
export function objectSelectionReducer(state = initialState, action: ObjectSelectionAction): ObjectSelectionsState {
export function objectSelectionReducer(state = initialState, action: ObjectSelectionAction): ObjectSelectionListState {
switch (action.type) {
case ObjectSelectionActionTypes.INITIAL_SELECT: {
if (isEmpty(state) || isEmpty(state[action.id])) {
if (isEmpty(state) || isEmpty(state[action.key]) || isEmpty(state[action.key][action.id])) {
return Object.assign({}, state, {
[action.id]: {
checked: true
[action.key]: {
[action.id]: {
checked: true
}
}
});
}
@@ -39,10 +48,12 @@ export function objectSelectionReducer(state = initialState, action: ObjectSelec
}
case ObjectSelectionActionTypes.INITIAL_DESELECT: {
if (isEmpty(state) || isEmpty(state[action.id])) {
if (isEmpty(state) || isEmpty(state[action.key]) || isEmpty(state[action.key][action.id])) {
return Object.assign({}, state, {
[action.id]: {
checked: false
[action.key]: {
[action.id]: {
checked: false
}
}
});
}
@@ -51,30 +62,42 @@ export function objectSelectionReducer(state = initialState, action: ObjectSelec
case ObjectSelectionActionTypes.SELECT: {
return Object.assign({}, state, {
[action.id]: {
checked: true
[action.key]: {
[action.id]: {
checked: true
}
}
});
}
case ObjectSelectionActionTypes.DESELECT: {
return Object.assign({}, state, {
[action.id]: {
checked: false
[action.key]: {
[action.id]: {
checked: false
}
}
});
}
case ObjectSelectionActionTypes.SWITCH: {
return Object.assign({}, state, {
[action.id]: {
checked: (isEmpty(state) || isEmpty(state[action.id])) ? true : !state[action.id].checked
[action.key]: {
[action.id]: {
checked: (isEmpty(state) || isEmpty(state[action.key]) || isEmpty(state[action.key][action.id])) ? true : !state[action.key][action.id].checked
}
}
});
}
case ObjectSelectionActionTypes.RESET: {
return {};
if (isEmpty(action.key)) {
return {};
} else {
return Object.assign({}, state, {
[action.key]: {}
});
}
}
default: {