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 | |
| 12 | export class StoreHelpers { |
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 | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 21 | switch (name) { |
| 22 | // FIXME handling exceptions, understand why these 3 endpoints are autogenerated with an _f |
| 23 | case 'SiteRole': |
| 24 | case 'SliceRole': |
| 25 | case 'SlicePrivilege': |
| 26 | return `/core/${pluralize(name.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join('_'))}`; |
| 27 | default: |
| 28 | return `/core/${pluralize(name.toLowerCase())}`; |
| 29 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 32 | public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> { |
| 33 | const collection: any[] = subject.value; |
| 34 | const index: number = _.findIndex(collection, (i) => { |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 35 | // NOTE evaluate to use event.msg.pk |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 36 | return i.id === event.msg.object.id; |
| 37 | }); |
| 38 | const exist: boolean = index > -1; |
| 39 | const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted'); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 40 | |
| 41 | // generate a resource for the model |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 42 | const endpoint = this.urlFromCoreModel(event.model); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 43 | const resource = this.modelRest.getResource(endpoint); |
| 44 | const model = new resource(event.msg.object); |
| 45 | |
| 46 | // remove |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 47 | if (exist && isDeleted) { |
| 48 | _.remove(collection, {id: event.msg.object.id}); |
| 49 | } |
| 50 | // Replace item at index using native splice |
| 51 | else if (exist && !isDeleted) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 52 | collection.splice(index, 1, model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 53 | } |
| 54 | // if the element is not deleted add it |
| 55 | else if (!exist && !isDeleted) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 56 | collection.push(model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | subject.next(collection); |
| 60 | |
| 61 | return subject; |
| 62 | } |
| 63 | } |