42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
|
|
import {Decoration} from '../../models/model-interfaces';
|
|
|
|
export const LOAD = 'LOAD';
|
|
export const ADD = 'ADD';
|
|
export const EDIT = 'EDIT';
|
|
export const REMOVE = 'REMOVE';
|
|
|
|
export class DecorationStore {
|
|
|
|
private decorations: Decoration[] = [];
|
|
|
|
items$ = new BehaviorSubject<Decoration[]>([]);
|
|
|
|
dispatch(action) {
|
|
this.decorations = this._reduce(this.decorations, action);
|
|
this.items$.next(this.decorations);
|
|
}
|
|
|
|
_reduce(decorations: Decoration[], action) {
|
|
switch (action.type) {
|
|
case LOAD:
|
|
return [...action.data];
|
|
case ADD:
|
|
return [...decorations, action.data];
|
|
case EDIT:
|
|
return decorations.map(decoration => {
|
|
const editedDecoration = action.data;
|
|
if (decoration._id !== editedDecoration._id) {
|
|
return decoration;
|
|
}
|
|
return editedDecoration;
|
|
});
|
|
case REMOVE:
|
|
return decorations.filter(decoration => decoration._id !== action.data._id);
|
|
default:
|
|
return decorations;
|
|
}
|
|
}
|
|
|
|
}
|