fix package list component
[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 {PackagesApiService} from './packages-api.service';
26 import {PackagesDashboardState} from './model/packages-dashboard.state';
27 import {Observable, of} from 'rxjs';
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 = 15;
35     private bluePrintContent: BluePrintPage = new BluePrintPage();
36
37     constructor(private packagesServiceList: PackagesApiService) {
38         super(new PackagesDashboardState());
39     }
40
41     public getAll() {
42         console.log('getting all packages...');
43         this.getPagedPackages(0, this.pageSize);
44     }
45
46     public search(command: string) {
47         if (command) {
48             this.searchPagedPackages(command, 0, this.pageSize);
49         } else {
50             this.getPagedPackages(0, this.pageSize);
51         }
52     }
53
54     public filterByTags(tags: string[]) {
55         console.log(this.state.currentPage);
56         this.getPagedPackagesByTags(this.state.command, this.state.currentPage,
57             this.pageSize, this.state.sortBy, tags);
58
59     }
60
61     public getPage(pageNumber: number, pageSize: number) {
62         if (this.isCommandExist()) {
63             this.searchPagedPackages(this.state.command, pageNumber, pageSize);
64         } else {
65             this.getPagedPackages(pageNumber, pageSize);
66         }
67     }
68
69     public sortPagedPackages(sortBy: string) {
70         if (this.isCommandExist()) {
71             this.searchPagedPackages(this.state.command, this.state.currentPage, this.pageSize, sortBy);
72         } else {
73             this.getPagedPackages(this.state.currentPage, this.pageSize, sortBy);
74         }
75
76     }
77
78
79     protected getPagedPackages(pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
80
81         this.packagesServiceList.getPagedPackages(pageNumber, pageSize, sortBy)
82             .subscribe((pages: BluePrintPage[]) => {
83                 this.setState({
84                     ...this.state,
85                     page: pages[0],
86                     filteredPackages: pages[0],
87                     command: '',
88                     totalPackages: pages[0].totalElements,
89                     currentPage: pageNumber,
90                     // this param is set only in get all as it represents the total number of pacakges in the server
91                     totalPackagesWithoutSearchorFilters: pages[0].totalElements,
92                     tags: [],
93                     sortBy
94                 });
95             });
96     }
97
98     private searchPagedPackages(keyWord: string, pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
99         this.packagesServiceList.getPagedPackagesByKeyWord(keyWord, pageNumber, pageSize, sortBy)
100             .subscribe((pages: BluePrintPage[]) => {
101                 this.setState({
102                     ...this.state,
103                     page: pages[0],
104                     filteredPackages: pages[0],
105                     command: keyWord,
106                     totalPackages: pages[0].totalElements,
107                     currentPage: pageNumber,
108                     tags: [],
109                     sortBy
110                 });
111             });
112     }
113
114     private isCommandExist() {
115         return this.state.command;
116     }
117
118     private getPagedPackagesByTags(keyWord: string, currentPage1: number, pageSize: number, sortBy1: string, tagsSearchable: string[]) {
119         this.getPagedPackagesByKeyWordFilteredByTags(tagsSearchable)
120             .subscribe((pages: BluePrintPage) => {
121                 this.setState({
122                     ...this.state,
123                     page: this.state.page,
124                     filteredPackages: pages,
125                     command: keyWord,
126                     tags: tagsSearchable,
127                     //  totalPackages: pages.totalElements,
128                     currentPage: currentPage1,
129                     sortBy: sortBy1,
130                     totalPackages: this.state.page.totalElements,
131                 });
132             });
133     }
134
135     private getPagedPackagesByKeyWordFilteredByTags(tagsSearchable: string[]): Observable<any> {
136         this.bluePrintContent.content = [];
137         if (tagsSearchable && tagsSearchable.length !== 0 && !tagsSearchable.includes('All')) {
138             tagsSearchable.forEach(tag => {
139                 if (tag) {
140                     this.state.page.content.forEach(bluePrintModel => {
141                         if (tag.endsWith(',')) {
142                             tag = tag.replace(',', '');
143                         }
144                         bluePrintModel.tags.split(',').forEach(bluePrintModelTag => {
145                             if (bluePrintModelTag === tag) {
146                                 this.bluePrintContent.content.push(bluePrintModel);
147                             }
148                         });
149                     });
150                 } else {
151                     this.getPagedPackages(this.state.currentPage, this.pageSize);
152                     return of(this.state.page);
153                 }
154             });
155             this.bluePrintContent.content = this.bluePrintContent.content.filter((value, index, self) => self.indexOf(value) === index);
156             console.log('the lenght is ' + this.bluePrintContent.content.length);
157             return of(this.bluePrintContent);
158         } else {
159             this.getPagedPackages(this.state.currentPage, this.pageSize);
160             return of(this.state.page);
161         }
162     }
163
164
165 }