Skip to content

Commit bcff955

Browse files
committed
fix on fetch for filters for projects in program
1 parent 40bbf59 commit bcff955

2 files changed

Lines changed: 46 additions & 51 deletions

File tree

projects/social_platform/src/app/office/program/detail/projects/projects.component.ts

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,6 @@ import { HttpParams } from "@angular/common/http";
3939
import { IconComponent } from "@uilib";
4040
import { PartnerProgramFields } from "@office/models/partner-program-fields.model";
4141

42-
/**
43-
* Компонент списка проектов программы
44-
*
45-
* Отображает все проекты, связанные с программой, с поддержкой:
46-
* - Бесконечной прокрутки (infinite scroll)
47-
* - Пагинации данных
48-
* - Модального окна для оценки проектов (для экспертов)
49-
*
50-
* Принимает:
51-
* @param {ActivatedRoute} route - Для получения данных из резолвера
52-
* @param {ProgramService} programService - Сервис для загрузки проектов
53-
* @param {AuthService} authService - Для проверки прав пользователя
54-
*
55-
* Данные:
56-
* @property {Project[]} projects - Массив проектов программы
57-
* @property {number} projectsTotalCount - Общее количество проектов
58-
* @property {Observable<Program>} program$ - Поток данных программы
59-
* @property {number} page - Текущая страница пагинации
60-
* @property {number} perPage - Количество проектов на странице
61-
* @property {boolean} isFilterOpen - состояние панели фильтров (мобильные)
62-
* ViewChild:
63-
* @ViewChild projectsRoot - Ссылка на DOM элемент списка проектов
64-
*
65-
* Функциональность:
66-
* - Загрузка начальных данных из резолвера
67-
* - Автоматическая подгрузка при прокрутке
68-
* - Отображение карточек проектов
69-
* - Интеграция с компонентом оценки проектов
70-
*
71-
* Методы:
72-
* @method onScroll() - Обработчик прокрутки для подгрузки данных
73-
* @method onFetch() - Загрузка следующей порции проектов
74-
*
75-
* Возвращает:
76-
* HTML шаблон со списком проектов и элементами управления
77-
*/
7842
@Component({
7943
selector: "app-projects",
8044
templateUrl: "./projects.component.html",
@@ -125,6 +89,8 @@ export class ProgramProjectsComponent implements OnInit, AfterViewInit, OnDestro
12589
private previousReqQuery: Record<string, any> = {};
12690
private availableFilters: PartnerProgramFields[] = [];
12791

92+
private currentFilters: Record<string, any> = {};
93+
12894
ngOnInit(): void {
12995
const routeData$ = this.route.data
13096
.pipe(
@@ -170,18 +136,23 @@ export class ProgramProjectsComponent implements OnInit, AfterViewInit, OnDestro
170136
if (JSON.stringify(reqQuery) !== JSON.stringify(this.previousReqQuery)) {
171137
this.previousReqQuery = reqQuery;
172138

139+
this.currentFilters = reqQuery["filters"] || {};
140+
this.page = 1;
141+
173142
const hasFilters =
174143
reqQuery && reqQuery["filters"] && Object.keys(reqQuery["filters"]).length > 0;
175144

176145
const params = new HttpParams({ fromObject: { offset: 0, limit: 21 } });
177146

178147
if (hasFilters) {
179-
return this.programService.createProgramFilters(programId, reqQuery["filters"]).pipe(
180-
catchError(err => {
181-
console.error("createFilters failed, fallback to getAllProjects()", err);
182-
return this.programService.getAllProjects(programId, params);
183-
})
184-
);
148+
return this.programService
149+
.createProgramFilters(programId, reqQuery["filters"], params)
150+
.pipe(
151+
catchError(err => {
152+
console.error("createFilters failed, fallback to getAllProjects()", err);
153+
return this.programService.getAllProjects(programId, params);
154+
})
155+
);
185156
}
186157

187158
return this.programService.getAllProjects(programId, params).pipe(
@@ -202,6 +173,7 @@ export class ProgramProjectsComponent implements OnInit, AfterViewInit, OnDestro
202173

203174
this.projects = projects.results;
204175
this.searchedProjects = projects.results;
176+
this.projectsTotalCount = projects.count;
205177

206178
this.cdref.detectChanges();
207179
});
@@ -299,19 +271,37 @@ export class ProgramProjectsComponent implements OnInit, AfterViewInit, OnDestro
299271
const offset = this.page * this.perPage;
300272
const limit = this.perPage;
301273

302-
return this.programService
303-
.getAllProjects(programId, new HttpParams({ fromObject: { offset, limit } }))
304-
.pipe(
274+
const hasFilters = this.currentFilters && Object.keys(this.currentFilters).length > 0;
275+
276+
if (hasFilters) {
277+
const params = new HttpParams({ fromObject: { offset, limit } });
278+
279+
return this.programService.createProgramFilters(programId, this.currentFilters, params).pipe(
305280
tap(projects => {
306281
this.projectsTotalCount = projects.count;
307282
this.projects = [...this.projects, ...projects.results];
308283
this.searchedProjects = this.projects;
309-
310284
this.page++;
311-
312285
this.cdref.detectChanges();
286+
}),
287+
catchError(err => {
288+
console.error("Pagination with filters failed", err);
289+
return of({ results: [], count: 0 });
313290
})
314291
);
292+
} else {
293+
return this.programService
294+
.getAllProjects(programId, new HttpParams({ fromObject: { offset, limit } }))
295+
.pipe(
296+
tap(projects => {
297+
this.projectsTotalCount = projects.count;
298+
this.projects = [...this.projects, ...projects.results];
299+
this.searchedProjects = this.projects;
300+
this.page++;
301+
this.cdref.detectChanges();
302+
})
303+
);
304+
}
315305
}
316306

317307
private buildFilterQuery(q: Params): Record<string, any> {

projects/social_platform/src/app/office/program/services/program.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,16 @@ export class ProgramService {
9696

9797
createProgramFilters(
9898
programId: number,
99-
filters: { filters: { string: string[] } }
99+
filters: Record<string, string[]>,
100+
params?: HttpParams
100101
): Observable<ApiPagination<Project>> {
101-
return this.apiService.post(`${this.PROGRAMS_URL}/${programId}/projects/filter/`, {
102-
filters,
103-
});
102+
let url = `${this.PROGRAMS_URL}/${programId}/projects/filter/`;
103+
104+
if (params) {
105+
url += `?${params.toString()}`;
106+
}
107+
108+
return this.apiService.post(url, { filters: filters });
104109
}
105110

106111
submitCompettetiveProject(relationId: number): Observable<Project> {

0 commit comments

Comments
 (0)