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