-
Notifications
You must be signed in to change notification settings - Fork 1
/
structure-list-search.component.ts
247 lines (226 loc) · 8.72 KB
/
structure-list-search.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { Component, EventEmitter, Inject, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ButtonType } from '@gouvfr-anct/mediation-numerique/shared';
import { TypeModal } from '../../models/enum/typeModal.enum';
import { Category } from '../../models/category.model';
import { Filter } from '../../models/filter.model';
import { Module } from '../../models/module.model';
import { SEARCH_TOKEN, SearchRepository } from '../../repositories/search.repository';
@Component({
selector: 'app-structure-list-search',
templateUrl: './structure-list-search.component.html',
styleUrls: ['./structure-list-search.component.scss']
})
export class StructureListSearchComponent implements OnInit {
@Output() searchEvent = new EventEmitter();
public locate = false;
// Show/hide form createStructure
public addStructureFormModal = false;
public buttonTypeEnum = ButtonType;
// Form search input
public searchForm: FormGroup;
public modalTypeOpened: TypeModal;
// Checkbox variable
public checkedModulesFilter: Module[];
public numberTrainingChecked = 0;
public numberAccompanimentChecked = 0;
public numberPublicChecked = 0;
public numberEquipmentChecked = 0;
public numberMoreFiltersChecked = 0;
// Modal categories
public categoriesTraining: Category[] = [];
public categoriesAccompaniment: Category[] = [];
public categoriesPublic: Category[] = [];
public categoriesEquipment: Category[] = [];
public categoriesMoreFilters: Category[] = [];
public queryString: string;
// Modal confirmation variable
public isConfirmationModalOpen = false;
public confirmationModalContent =
'Afin d’ajouter votre structure,vous allez être redirigé vers le formulaire Grand Lyon à remplir.';
constructor(
@Inject(SEARCH_TOKEN) readonly searchService: SearchRepository,
private fb: FormBuilder,
private activatedRoute: ActivatedRoute,
private route: ActivatedRoute,
private router: Router
) {
this.searchForm = this.fb.group({
searchTerm: this.activatedRoute.snapshot.queryParamMap.get('search')
? this.activatedRoute.snapshot.queryParamMap.get('search')
: ''
});
}
ngOnInit(): void {
// Will store the different categories
this.getData();
this.queryString = this.activatedRoute.snapshot.queryParamMap.get('search');
this.checkedModulesFilter = new Array();
if (this.queryString) {
const filters: Filter[] = [];
filters.push(new Filter('query', this.queryString));
this.searchEvent.emit(filters);
}
}
public convertModulesTofilters(modules: Module[], term?: string): Filter[] {
const filters: Filter[] = [];
if (term) {
filters.push(new Filter('query', term));
}
// Add checked box filter
modules.forEach((cm) => {
filters.push(new Filter(cm.text, cm.id, cm.displayText));
});
return filters;
}
// Accessor to template angular.
public get TypeModal(): typeof TypeModal {
return TypeModal;
}
// Clear input search
public clearInput(): void {
this.searchForm.reset();
this.applyFilter(null);
}
// Sends an array containing all filters
public applyFilter(term: string): void {
// Add search input filter
if (term) {
this.router.navigate(['/acteurs'], {
relativeTo: this.route,
queryParams: {
search: term
},
queryParamsHandling: 'merge'
});
} else if (!term) {
this.router.navigate(['/acteurs'], {
relativeTo: this.route
});
}
const filters = this.convertModulesTofilters(this.checkedModulesFilter, term);
// Send filters
this.searchEvent.emit(filters);
}
public fetchResults(checkedModules: Module[]): void {
const inputTerm = this.searchForm.get('searchTerm').value;
// Check if some modules is checked in filters
if (this.checkedModulesFilter !== checkedModules) {
this.countCheckFiltersOnModules(checkedModules);
}
// Store checked modules
this.checkedModulesFilter = checkedModules;
// Close modal after receive filters from her.
this.closeModal();
this.applyFilter(inputTerm);
}
// Check if some modules is checked on filter and store number of modules checked
public countCheckFiltersOnModules(checkedModules: Module[]): void {
this.numberAccompanimentChecked = checkedModules.filter((module) => module.text === 'proceduresAccompaniment').length;
this.numberTrainingChecked = checkedModules.filter(
(module) =>
module.text === 'baseSkills' ||
module.text === 'socialAndProfessional' ||
module.text === 'parentingHelp' ||
module.text === 'accessRight' ||
module.text === 'digitalCultureSecurity'
).length;
this.numberPublicChecked = checkedModules.filter((module) => module.text === 'publicsAccompaniment').length;
this.numberEquipmentChecked = checkedModules.filter((module) => module.text === 'equipmentsAndServices').length;
this.numberMoreFiltersChecked = checkedModules.filter(
(module) => module.text === 'labelsQualifications' || module.text === 'accessModality'
).length;
}
public getModalCategory(): Category[] {
switch (this.modalTypeOpened) {
case TypeModal.accompaniment:
return this.categoriesAccompaniment;
case TypeModal.training:
return this.categoriesTraining;
case TypeModal.public:
return this.categoriesPublic;
case TypeModal.equipments:
return this.categoriesEquipment;
case TypeModal.moreFilters:
return this.categoriesMoreFilters;
default:
throw new Error('Modal type not handle');
}
}
// Open the modal and display the list according to the right filter button
public openModal(modalType: TypeModal): void {
// if modal already opened, reset type
if (this.modalTypeOpened === modalType) {
this.closeModal();
} else if (this.modalTypeOpened !== modalType) {
this.modalTypeOpened = modalType;
}
}
public closeModal(): void {
this.modalTypeOpened = undefined;
}
// Management of the checkbox event (Check / Uncheck)
public externalCheckboxCheck(event, categ, displayName): void {
const checkValue: string = event.target.value;
const inputTerm = this.searchForm.get('searchTerm').value;
if (event.target.checked) {
this.checkedModulesFilter.push(new Module(checkValue, categ, displayName));
this.numberMoreFiltersChecked++;
} else {
// Check if the module is present in the list and remove it
const index = this.checkedModulesFilter.findIndex((m: Module) => m.id === checkValue && m.text === categ);
if (index > -1) {
this.checkedModulesFilter.splice(index, 1);
this.countCheckFiltersOnModules(this.checkedModulesFilter);
}
}
this.applyFilter(inputTerm);
}
// Get the categories for each modal type
private getData(): void {
this.searchService.getCategoriesAccompaniment().subscribe((res) => {
const categories: Category[] = res;
categories.forEach((category) => {
this.categoriesAccompaniment.push(category);
});
});
this.searchService.getCategoriesTraining().subscribe((res) => {
const categories: Category[] = res;
categories.forEach((category) => {
this.categoriesTraining.push(category);
});
});
this.searchService.getCategoriesOthers().subscribe((res) => {
const categories: Category[] = res;
categories.forEach((category) => {
if (category.id === 'publicsAccompaniment') {
this.categoriesPublic.push(category);
} else if (category.id === 'equipmentsAndServices') {
this.categoriesEquipment.push(category);
} else if (category.id === 'labelsQualifications' || category.id === 'accessModality') {
this.categoriesMoreFilters.push(category);
}
});
});
}
public resetFilters(): void {
this.checkedModulesFilter = [];
this.numberTrainingChecked = 0;
this.numberAccompanimentChecked = 0;
this.numberPublicChecked = 0;
this.numberEquipmentChecked = 0;
this.numberMoreFiltersChecked = 0;
const inputTerm = this.searchForm.get('searchTerm').value;
const filters = this.convertModulesTofilters(this.checkedModulesFilter, inputTerm);
this.searchEvent.emit(filters);
}
public removeFilter(module: Module): void {
const index = this.checkedModulesFilter.findIndex((m: Module) => m.id === module.id);
this.checkedModulesFilter.splice(index, 1);
const inputTerm = this.searchForm.get('searchTerm').value;
const filters = this.convertModulesTofilters(this.checkedModulesFilter, inputTerm);
this.countCheckFiltersOnModules(this.checkedModulesFilter);
this.searchEvent.emit(filters);
}
}