11/** @format */
22
3- import { Component , OnDestroy , OnInit , signal } from "@angular/core" ;
4- import { ActivatedRoute , RouterLink } from "@angular/router" ;
5- import { map , Subscription } from "rxjs" ;
3+ import { ChangeDetectorRef , Component , inject , OnDestroy , OnInit , signal } from "@angular/core" ;
4+ import { ActivatedRoute , Params , Router , RouterLink } from "@angular/router" ;
5+ import {
6+ combineLatest ,
7+ concatMap ,
8+ distinctUntilChanged ,
9+ map ,
10+ of ,
11+ Subscription ,
12+ switchMap ,
13+ } from "rxjs" ;
614import { Program } from "@office/program/models/program.model" ;
715import { NavService } from "@office/services/nav.service" ;
816import Fuse from "fuse.js" ;
917import { CheckboxComponent , SelectComponent } from "@ui/components" ;
1018import { generateOptionsList } from "@utils/generate-options-list" ;
1119import { ClickOutsideModule } from "ng-click-outside" ;
1220import { ProgramCardComponent } from "../shared/program-card/program-card.component" ;
21+ import { HttpParams } from "@angular/common/http" ;
22+ import { ProgramService } from "../services/program.service" ;
1323
1424/**
1525 * Главный компонент списка программ
@@ -59,13 +69,18 @@ import { ProgramCardComponent } from "../shared/program-card/program-card.compon
5969 ] ,
6070} )
6171export class ProgramMainComponent implements OnInit , OnDestroy {
62- constructor ( private readonly route : ActivatedRoute , private readonly navService : NavService ) { }
72+ private readonly navService = inject ( NavService ) ;
73+ private readonly route = inject ( ActivatedRoute ) ;
74+ private readonly router = inject ( Router ) ;
75+ private readonly programService = inject ( ProgramService ) ;
76+ private readonly cdref = inject ( ChangeDetectorRef ) ;
6377
6478 programCount = 0 ;
6579
6680 programs : Program [ ] = [ ] ;
6781 searchedPrograms : Program [ ] = [ ] ;
6882 subscriptions$ : Subscription [ ] = [ ] ;
83+ isPparticipating = signal < boolean > ( false ) ;
6984
7085 readonly programOptionsFilter = generateOptionsList ( 4 , "strings" , [
7186 "все" ,
@@ -77,32 +92,65 @@ export class ProgramMainComponent implements OnInit, OnDestroy {
7792 ngOnInit ( ) : void {
7893 this . navService . setNavTitle ( "Программы" ) ;
7994
80- const querySearch$ = this . route . queryParams . pipe ( map ( q => q [ "search" ] ) ) . subscribe ( search => {
81- const fuse = new Fuse ( this . programs , {
82- keys : [ "name" ] ,
83- } ) ;
95+ const combined$ = combineLatest ( [
96+ this . route . queryParams . pipe (
97+ map ( q => ( { filter : this . buildFilterQuery ( q ) , search : q [ "search" ] || "" } ) ) ,
98+ distinctUntilChanged ( ( a , b ) => JSON . stringify ( a ) === JSON . stringify ( b ) )
99+ ) ,
100+ ] )
101+ . pipe (
102+ switchMap ( ( [ { filter, search } ] ) => {
103+ this . isPparticipating . set ( filter [ "participating" ] === "true" ) ;
84104
85- this . searchedPrograms = search ? fuse . search ( search ) . map ( el => el . item ) : this . programs ;
86- } ) ;
105+ return this . programService
106+ . getAll ( 0 , 20 , new HttpParams ( { fromObject : filter } ) )
107+ . pipe ( map ( response => ( { response, search } ) ) ) ;
108+ } )
109+ )
110+ . subscribe ( ( { response, search } ) => {
111+ this . programCount = response . count ;
112+ this . programs = response . results ?? [ ] ;
87113
88- querySearch$ && this . subscriptions$ . push ( querySearch$ ) ;
114+ if ( search ) {
115+ const fuse = new Fuse ( this . programs , {
116+ keys : [ "name" ] ,
117+ threshold : 0.3 ,
118+ } ) ;
119+ this . searchedPrograms = fuse . search ( search ) . map ( el => el . item ) ;
120+ } else {
121+ this . searchedPrograms = this . programs ;
122+ }
89123
90- const programs$ = this . route . data . pipe ( map ( r => r [ "data" ] ) ) . subscribe ( programs => {
91- this . programCount = programs . count ;
92- this . programs = programs . results ?? [ ] ;
93- this . searchedPrograms = programs . results ?? [ ] ;
94- } ) ;
124+ this . cdref . detectChanges ( ) ;
125+ } ) ;
95126
96- programs$ && this . subscriptions$ . push ( programs $) ;
127+ this . subscriptions$ . push ( combined $) ;
97128 }
98129
99- isPparticipating = signal < boolean > ( false ) ;
130+ private buildFilterQuery ( q : Params ) : Record < string , any > {
131+ const reqQuery : Record < string , any > = { } ;
132+
133+ if ( q [ "participating" ] ) {
134+ reqQuery [ "participating" ] = q [ "participating" ] ;
135+ }
136+
137+ return reqQuery ;
138+ }
100139
101140 /**
102- * Переключает состояние чекбокса
141+ * Переключает состояние чекбокса "участвую"
103142 */
104143 onTogglePparticipating ( ) : void {
105- this . isPparticipating . set ( ! this . isPparticipating ( ) ) ;
144+ const newValue = ! this . isPparticipating ( ) ;
145+ this . isPparticipating . set ( newValue ) ;
146+
147+ this . router . navigate ( [ ] , {
148+ queryParams : {
149+ participating : newValue ? "true" : null ,
150+ } ,
151+ relativeTo : this . route ,
152+ queryParamsHandling : "merge" ,
153+ } ) ;
106154 }
107155
108156 ngOnDestroy ( ) : void {
0 commit comments