Merge "Creating findOneBluePrintModel (configuration)"
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / packages.store.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import {Injectable} from '@angular/core';
23 import {BluePrintPage} from './model/BluePrint.model';
24 import {Store} from '../../../common/core/stores/Store';
25 import {PackagesListService} from './packages-list.service';
26 import {PackagesDashboardState} from './model/packages-dashboard.state';
27
28
29 @Injectable({
30     providedIn: 'root'
31 })
32 export class PackagesStore extends Store<PackagesDashboardState> {
33     // TDOD fixed for now as there is no requirement to change it from UI
34     public pageSize = 5;
35
36     constructor(private packagesServiceList: PackagesListService) {
37         super(new PackagesDashboardState());
38     }
39
40     public getAll() {
41         console.log('getting all packages...');
42         this.getPagedPackages(0, this.pageSize);
43     }
44
45     public search(command: string) {
46         if (command) {
47             this.searchPagedPackages(command, 0, this.pageSize);
48         } else {
49             this.getPagedPackages(0, this.pageSize);
50         }
51     }
52
53     public getPage(pageNumber: number, pageSize: number) {
54         if (this.isCommandExist()) {
55             this.searchPagedPackages(this.state.command, pageNumber, pageSize);
56         } else {
57             this.getPagedPackages(pageNumber, pageSize);
58         }
59     }
60
61     public sortPagedPackages(sortBy: string) {
62         if (this.isCommandExist()) {
63             this.searchPagedPackages(this.state.command, this.state.currentPage, this.pageSize, sortBy);
64         } else {
65             this.getPagedPackages(this.state.currentPage, this.pageSize, sortBy);
66         }
67
68     }
69
70     private getPagedPackages(pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
71
72         this.packagesServiceList.getPagedPackages(pageNumber, pageSize, sortBy)
73             .subscribe((pages: BluePrintPage[]) => {
74                 this.setState({
75                     ...this.state,
76                     page: pages[0],
77                     command: '',
78                     totalPackages: pages[0].totalElements,
79                     currentPage: pageNumber,
80                     // this param is set only in get all as it represents the total number of pacakges in the server
81                     totalPackagesWithoutSearchorFilters: pages[0].totalElements,
82                     sortBy
83                 });
84             });
85     }
86
87     private searchPagedPackages(keyWord: string, pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
88         this.packagesServiceList.getPagedPackagesByKeyWord(keyWord, pageNumber, pageSize, sortBy)
89             .subscribe((pages: BluePrintPage[]) => {
90                 this.setState({
91                     ...this.state,
92                     page: pages[0],
93                     command: keyWord,
94                     totalPackages: pages[0].totalElements,
95                     currentPage: pageNumber,
96                     sortBy
97                 });
98             });
99     }
100
101     private isCommandExist() {
102         return this.state.command;
103     }
104 }