Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import {BehaviorSubject} from 'rxjs'; |
| 2 | import * as _ from 'lodash'; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 3 | import * as pluralize from 'pluralize'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 4 | import {IWSEvent} from '../websocket/global'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 5 | import {IXosResourceService} from '../rest/model.rest'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 6 | |
| 7 | export interface IStoreHelpersService { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 8 | urlFromCoreModel(name: string): string; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 9 | updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>; |
| 10 | } |
| 11 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 12 | export class StoreHelpers implements IStoreHelpersService { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 13 | static $inject = ['ModelRest']; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 14 | |
| 15 | constructor ( |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 16 | private modelRest: IXosResourceService |
| 17 | ) { |
| 18 | } |
| 19 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 20 | public urlFromCoreModel(name: string): string { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 21 | return `/core/${pluralize(name.toLowerCase())}`; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 24 | public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> { |
| 25 | const collection: any[] = subject.value; |
| 26 | const index: number = _.findIndex(collection, (i) => { |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 27 | // NOTE evaluate to use event.msg.pk |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 28 | return i.id === event.msg.object.id; |
| 29 | }); |
| 30 | const exist: boolean = index > -1; |
| 31 | const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted'); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 32 | |
| 33 | // generate a resource for the model |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 34 | const endpoint = this.urlFromCoreModel(event.model); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 35 | const resource = this.modelRest.getResource(endpoint); |
| 36 | const model = new resource(event.msg.object); |
| 37 | |
| 38 | // remove |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 39 | if (exist && isDeleted) { |
| 40 | _.remove(collection, {id: event.msg.object.id}); |
| 41 | } |
| 42 | // Replace item at index using native splice |
| 43 | else if (exist && !isDeleted) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 44 | collection.splice(index, 1, model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 45 | } |
| 46 | // if the element is not deleted add it |
| 47 | else if (!exist && !isDeleted) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 48 | collection.push(model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | subject.next(collection); |
| 52 | |
| 53 | return subject; |
| 54 | } |
| 55 | } |