From 27eb2e7629e572c86c709208c99003f009c89dc2 Mon Sep 17 00:00:00 2001 From: Keksonoid Date: Mon, 12 May 2025 13:52:41 +0500 Subject: [PATCH] fix back button and fix dublicate request --- .../companies-page.component.ts | 25 ++++++++++++++++--- .../company-page/company-page.component.html | 11 +++++--- .../company-page/company-page.component.ts | 21 ++++++++++++++++ .../pagination-buttons.component.ts | 14 +++++++++-- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/app/modules/companies/components/companies-page/companies-page.component.ts b/src/app/modules/companies/components/companies-page/companies-page.component.ts index 6a302be..fcfee1b 100644 --- a/src/app/modules/companies/components/companies-page/companies-page.component.ts +++ b/src/app/modules/companies/components/companies-page/companies-page.component.ts @@ -19,6 +19,7 @@ export class CompaniesPageComponent implements OnInit, OnDestroy { source: PaginatedList | null = null; currentPage: number = 1; searchQuery: string = ""; + private skipNextQueryParamsUpdate: boolean = false; constructor( private readonly service: CompaniesService, @@ -32,6 +33,11 @@ export class CompaniesPageComponent implements OnInit, OnDestroy { ngOnInit(): void { this.route.queryParams.pipe(untilDestroyed(this)).subscribe(params => { + if (this.skipNextQueryParamsUpdate) { + this.skipNextQueryParamsUpdate = false; + return; + } + this.currentPage = params['page'] ? Number(params['page']) : 1; this.searchQuery = params['search'] || ''; this.loadData(this.currentPage, false); @@ -45,7 +51,8 @@ export class CompaniesPageComponent implements OnInit, OnDestroy { "company_reviews", this.searchQuery, ); - this.updateUrlParams(1); + this.currentPage = 1; + this.loadData(1, true); } } @@ -67,22 +74,24 @@ export class CompaniesPageComponent implements OnInit, OnDestroy { this.searchQuery, ); + this.skipNextQueryParamsUpdate = true; this.router.navigate([], { relativeTo: this.route, queryParams: {}, }); + this.currentPage = 1; this.loadData(1, false); } loadData(pageToLoad: number, updateUrl: boolean = true): void { this.companies = null; this.source = null; + this.currentPage = pageToLoad; if (updateUrl) { + this.skipNextQueryParamsUpdate = true; this.updateUrlParams(pageToLoad); - } else { - this.currentPage = pageToLoad; } this.service @@ -113,7 +122,15 @@ export class CompaniesPageComponent implements OnInit, OnDestroy { } navigateToCompany(id: string): void { - this.router.navigate(["/companies", id]); + const state: { page: number; search?: string } = { + page: this.currentPage + }; + + if (this.searchQuery && this.searchQuery.length >= 3) { + state.search = this.searchQuery; + } + + this.router.navigate(["/companies", id], { state }); } ngOnDestroy(): void { diff --git a/src/app/modules/companies/components/company-page/company-page.component.html b/src/app/modules/companies/components/company-page/company-page.component.html index 333f513..74d4bdb 100644 --- a/src/app/modules/companies/components/company-page/company-page.component.html +++ b/src/app/modules/companies/components/company-page/company-page.component.html @@ -43,10 +43,13 @@
- +