2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
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
12 http://www.apache.org/licenses/LICENSE-2.0
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============================================
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 import {NgxUiLoaderService} from 'ngx-ui-loader';
33 export class PackagesStore extends Store<PackagesDashboardState> {
34 // TDOD fixed for now as there is no requirement to change it from UI
36 private bluePrintContent: BlueprintPage = new BlueprintPage();
39 private packagesServiceList: PackagesApiService,
40 private ngxLoader: NgxUiLoaderService
42 super(new PackagesDashboardState());
46 console.log('getting all packages...');
47 this.getPagedPackages(0, this.pageSize);
50 public search(command: string) {
52 this.searchPagedPackages(command, 0, this.pageSize);
54 this.getPagedPackages(0, this.pageSize);
58 public filterByTags(tags: string[]) {
59 console.log(this.state.currentPage);
60 this.getPagedPackagesByTags(this.state.command, this.state.currentPage,
61 this.pageSize, this.state.sortBy, tags);
65 public getPage(pageNumber: number, pageSize: number) {
66 if (this.isCommandExist()) {
67 this.searchPagedPackages(this.state.command, pageNumber, pageSize);
69 this.getPagedPackages(pageNumber, pageSize);
73 public sortPagedPackages(sortBy: string) {
74 if (this.isCommandExist()) {
75 this.searchPagedPackages(this.state.command, this.state.currentPage, this.pageSize, sortBy);
77 this.getPagedPackages(this.state.currentPage, this.pageSize, sortBy);
83 protected getPagedPackages(pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
85 this.packagesServiceList.getPagedPackages(pageNumber, pageSize, sortBy)
86 .subscribe((pages: BlueprintPage[]) => {
90 filteredPackages: pages[0],
92 totalPackages: pages[0].totalElements,
93 currentPage: pageNumber,
94 // this param is set only in get all as it represents the total number of pacakges in the server
95 totalPackagesWithoutSearchorFilters: pages[0].totalElements,
102 this.ngxLoader.stop();
106 private searchPagedPackages(keyWord: string, pageNumber: number, pageSize: number, sortBy: string = this.state.sortBy) {
107 this.packagesServiceList.getPagedPackagesByKeyWord(keyWord, pageNumber, pageSize, sortBy)
108 .subscribe((pages: BlueprintPage[]) => {
112 filteredPackages: pages[0],
114 totalPackages: pages[0].totalElements,
115 currentPage: pageNumber,
122 this.ngxLoader.stop(); // start master loader
126 private isCommandExist() {
127 return this.state.command;
130 private getPagedPackagesByTags(keyWord: string, currentPage1: number, pageSize: number, sortBy1: string, tagsSearchable: string[]) {
131 this.getPagedPackagesByKeyWordFilteredByTags(tagsSearchable)
132 .subscribe((pages: BlueprintPage) => {
135 page: this.state.page,
136 filteredPackages: pages,
138 tags: tagsSearchable,
139 // totalPackages: pages.totalElements,
140 currentPage: currentPage1,
142 totalPackages: this.state.page.totalElements,
147 private getPagedPackagesByKeyWordFilteredByTags(tagsSearchable: string[]): Observable<any> {
148 this.bluePrintContent.content = [];
149 if (tagsSearchable && tagsSearchable.length !== 0 && !tagsSearchable.includes('All')) {
150 tagsSearchable.forEach(tag => {
152 this.state.page.content.forEach(bluePrintModel => {
153 if (tag.endsWith(',')) {
154 tag = tag.replace(',', '');
156 bluePrintModel.tags.split(',').forEach(bluePrintModelTag => {
157 if (tag.includes(bluePrintModelTag.trim())) {
158 this.bluePrintContent.content.push(bluePrintModel);
163 this.getPagedPackages(this.state.currentPage, this.pageSize);
164 return of(this.state.page);
167 this.bluePrintContent.content = this.bluePrintContent.content.filter((value, index, self) => self.indexOf(value) === index);
168 console.log('the lenght is ' + this.bluePrintContent.content.length);
169 return of(this.bluePrintContent);
171 this.getPagedPackages(this.state.currentPage, this.pageSize);
172 return of(this.state.page);