mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-14 05:23:06 +00:00
55946: Refactored item-select to object-select to allow for easier implementation of collection-select
This commit is contained in:
84
src/app/shared/object-select/object-select.reducer.ts
Normal file
84
src/app/shared/object-select/object-select.reducer.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { isEmpty } from '../empty.util';
|
||||
import { ObjectSelectionAction, ObjectSelectionActionTypes } from './object-select.actions';
|
||||
|
||||
/**
|
||||
* Interface that represents the state for a single filters
|
||||
*/
|
||||
export interface ObjectSelectionState {
|
||||
checked: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface that represents the state for all available filters
|
||||
*/
|
||||
export interface ObjectSelectionsState {
|
||||
[id: string]: ObjectSelectionState
|
||||
}
|
||||
|
||||
const initialState: ObjectSelectionsState = 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
|
||||
*/
|
||||
export function objectSelectionReducer(state = initialState, action: ObjectSelectionAction): ObjectSelectionsState {
|
||||
|
||||
switch (action.type) {
|
||||
|
||||
case ObjectSelectionActionTypes.INITIAL_SELECT: {
|
||||
if (isEmpty(state) || isEmpty(state[action.id])) {
|
||||
return Object.assign({}, state, {
|
||||
[action.id]: {
|
||||
checked: true
|
||||
}
|
||||
});
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
case ObjectSelectionActionTypes.INITIAL_DESELECT: {
|
||||
if (isEmpty(state) || isEmpty(state[action.id])) {
|
||||
return Object.assign({}, state, {
|
||||
[action.id]: {
|
||||
checked: false
|
||||
}
|
||||
});
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
case ObjectSelectionActionTypes.SELECT: {
|
||||
return Object.assign({}, state, {
|
||||
[action.id]: {
|
||||
checked: true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
case ObjectSelectionActionTypes.DESELECT: {
|
||||
return Object.assign({}, state, {
|
||||
[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
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
case ObjectSelectionActionTypes.RESET: {
|
||||
return {};
|
||||
}
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user