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

@@ -11,6 +11,11 @@ export const ObjectSelectionActionTypes = {
}; };
export class ObjectSelectionAction implements Action { export class ObjectSelectionAction implements Action {
/**
* Key of the list (of selections) for which the action should be performed
*/
key: string;
/** /**
* UUID of the object a select action can be performed on * UUID of the object a select action can be performed on
*/ */
@@ -23,9 +28,11 @@ export class ObjectSelectionAction implements Action {
/** /**
* Initialize with the object's UUID * Initialize with the object's UUID
* @param {string} key of the list
* @param {string} id of the object * @param {string} id of the object
*/ */
constructor(id: string) { constructor(key: string, id: string) {
this.key = key;
this.id = id; this.id = id;
} }
} }

View File

@@ -2,36 +2,45 @@ import { isEmpty } from '../empty.util';
import { ObjectSelectionAction, ObjectSelectionActionTypes } from './object-select.actions'; 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 { export interface ObjectSelectionState {
checked: boolean; 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 { export interface ObjectSelectionsState {
[id: string]: ObjectSelectionState [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 * Performs a selection action on the current state
* @param {SearchFiltersState} state The state before the action is performed * @param {ObjectSelectionListState} state The state before the action is performed
* @param {SearchFilterAction} action The action that should be performed * @param {ObjectSelectionAction} action The action that should be performed
* @returns {SearchFiltersState} The state after the action is 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) { switch (action.type) {
case ObjectSelectionActionTypes.INITIAL_SELECT: { 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, { return Object.assign({}, state, {
[action.id]: { [action.key]: {
checked: true [action.id]: {
checked: true
}
} }
}); });
} }
@@ -39,10 +48,12 @@ export function objectSelectionReducer(state = initialState, action: ObjectSelec
} }
case ObjectSelectionActionTypes.INITIAL_DESELECT: { 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, { return Object.assign({}, state, {
[action.id]: { [action.key]: {
checked: false [action.id]: {
checked: false
}
} }
}); });
} }
@@ -51,30 +62,42 @@ export function objectSelectionReducer(state = initialState, action: ObjectSelec
case ObjectSelectionActionTypes.SELECT: { case ObjectSelectionActionTypes.SELECT: {
return Object.assign({}, state, { return Object.assign({}, state, {
[action.id]: { [action.key]: {
checked: true [action.id]: {
checked: true
}
} }
}); });
} }
case ObjectSelectionActionTypes.DESELECT: { case ObjectSelectionActionTypes.DESELECT: {
return Object.assign({}, state, { return Object.assign({}, state, {
[action.id]: { [action.key]: {
checked: false [action.id]: {
checked: false
}
} }
}); });
} }
case ObjectSelectionActionTypes.SWITCH: { case ObjectSelectionActionTypes.SWITCH: {
return Object.assign({}, state, { return Object.assign({}, state, {
[action.id]: { [action.key]: {
checked: (isEmpty(state) || isEmpty(state[action.id])) ? true : !state[action.id].checked [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: { case ObjectSelectionActionTypes.RESET: {
return {}; if (isEmpty(action.key)) {
return {};
} else {
return Object.assign({}, state, {
[action.key]: {}
});
}
} }
default: { default: {

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { createSelector, MemoizedSelector, Store } from '@ngrx/store'; import { createSelector, MemoizedSelector, Store } from '@ngrx/store';
import { ObjectSelectionsState, ObjectSelectionState } from './object-select.reducer'; import { ObjectSelectionListState, ObjectSelectionsState, ObjectSelectionState } from './object-select.reducer';
import { import {
ObjectSelectionDeselectAction, ObjectSelectionDeselectAction,
ObjectSelectionInitialDeselectAction, ObjectSelectionInitialDeselectAction,
@@ -13,7 +13,8 @@ import { map } from 'rxjs/operators';
import { AppState } from '../../app.reducer'; import { AppState } from '../../app.reducer';
const selectionStateSelector = (state: ObjectSelectionsState) => state.objectSelection; const selectionStateSelector = (state: ObjectSelectionsState) => state.objectSelection;
const objectSelectionsStateSelector = (state: AppState) => state.objectSelection; const objectSelectionsStateSelector = (state: ObjectSelectionListState) => state.objectSelection;
const objectSelectionListStateSelector = (state: AppState) => state.objectSelection;
/** /**
* Service that takes care of selecting and deselecting objects * Service that takes care of selecting and deselecting objects
@@ -28,11 +29,12 @@ export class ObjectSelectService {
} }
/** /**
* Request the current selection of a given object * Request the current selection of a given object in a given list
* @param {string} key The key of the list where the selection resides in
* @param {string} id The UUID of the object * @param {string} id The UUID of the object
* @returns {Observable<boolean>} Emits the current selection state of the given object, if it's unavailable, return false * @returns {Observable<boolean>} Emits the current selection state of the given object, if it's unavailable, return false
*/ */
getSelected(id: string): Observable<boolean> { getSelected(key: string, id: string): Observable<boolean> {
return this.store.select(selectionByIdSelector(id)).pipe( return this.store.select(selectionByIdSelector(id)).pipe(
map((object: ObjectSelectionState) => { map((object: ObjectSelectionState) => {
if (object) { if (object) {
@@ -45,9 +47,8 @@ export class ObjectSelectService {
} }
/** /**
* Request the current selection of a given object * Request the current selection of all objects
* @param {string} id The UUID of the object * @returns {Observable<boolean>} Emits the current selection state of all objects
* @returns {Observable<boolean>} Emits the current selection state of the given object, if it's unavailable, return false
*/ */
getAllSelected(): Observable<string[]> { getAllSelected(): Observable<string[]> {
return this.appStore.select(objectSelectionsStateSelector).pipe( return this.appStore.select(objectSelectionsStateSelector).pipe(
@@ -56,50 +57,56 @@ export class ObjectSelectService {
} }
/** /**
* Dispatches an initial select action to the store for a given object * Dispatches an initial select action to the store for a given object in a given list
* @param {string} key The key of the list to select the object in
* @param {string} id The UUID of the object to select * @param {string} id The UUID of the object to select
*/ */
public initialSelect(id: string): void { public initialSelect(key: string, id: string): void {
this.store.dispatch(new ObjectSelectionInitialSelectAction(id)); this.store.dispatch(new ObjectSelectionInitialSelectAction(key, id));
} }
/** /**
* Dispatches an initial deselect action to the store for a given object * Dispatches an initial deselect action to the store for a given object in a given list
* @param {string} key The key of the list to deselect the object in
* @param {string} id The UUID of the object to deselect * @param {string} id The UUID of the object to deselect
*/ */
public initialDeselect(id: string): void { public initialDeselect(key: string, id: string): void {
this.store.dispatch(new ObjectSelectionInitialDeselectAction(id)); this.store.dispatch(new ObjectSelectionInitialDeselectAction(key, id));
} }
/** /**
* Dispatches a select action to the store for a given object * Dispatches a select action to the store for a given object in a given list
* @param {string} key The key of the list to select the object in
* @param {string} id The UUID of the object to select * @param {string} id The UUID of the object to select
*/ */
public select(id: string): void { public select(key: string, id: string): void {
this.store.dispatch(new ObjectSelectionSelectAction(id)); this.store.dispatch(new ObjectSelectionSelectAction(key, id));
} }
/** /**
* Dispatches a deselect action to the store for a given object * Dispatches a deselect action to the store for a given object in a given list
* @param {string} key The key of the list to deselect the object in
* @param {string} id The UUID of the object to deselect * @param {string} id The UUID of the object to deselect
*/ */
public deselect(id: string): void { public deselect(key: string, id: string): void {
this.store.dispatch(new ObjectSelectionDeselectAction(id)); this.store.dispatch(new ObjectSelectionDeselectAction(key, id));
} }
/** /**
* Dispatches a switch action to the store for a given object * Dispatches a switch action to the store for a given object in a given list
* @param {string} key The key of the list to select the object in
* @param {string} id The UUID of the object to select * @param {string} id The UUID of the object to select
*/ */
public switch(id: string): void { public switch(key: string, id: string): void {
this.store.dispatch(new ObjectSelectionSwitchAction(id)); this.store.dispatch(new ObjectSelectionSwitchAction(key, id));
} }
/** /**
* Dispatches a reset action to the store for all objects * Dispatches a reset action to the store for all objects (in a list)
* @param {string} key The key of the list to clear all selections for
*/ */
public reset(): void { public reset(key?: string): void {
this.store.dispatch(new ObjectSelectionResetAction(null)); this.store.dispatch(new ObjectSelectionResetAction(key, null));
} }
} }