Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class CompaniesPageComponent implements OnInit, OnDestroy {
source: PaginatedList<Company> | null = null;
currentPage: number = 1;
searchQuery: string = "";
private skipNextQueryParamsUpdate: boolean = false;

constructor(
private readonly service: CompaniesService,
Expand All @@ -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);
Expand All @@ -45,7 +51,8 @@ export class CompaniesPageComponent implements OnInit, OnDestroy {
"company_reviews",
this.searchQuery,
);
this.updateUrlParams(1);
this.currentPage = 1;
this.loadData(1, true);
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
</div>

<div class="d-flex justify-content-between">
<app-go-back-button
[routerLink]="'/companies'"
[text]="'К списку компаний'"
/>
<button
type="button"
class="btn btn-outline-dark small"
(click)="goBackToList()"
>
<span class="small"><i class="bi bi-arrow-left me-1"></i>К списку компаний</span>
</button>
<button
type="button"
*ngIf="company.reviewsCount > 0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class CompanyPageComponent implements OnInit, OnDestroy {

private readonly activateRoute: ActivatedRouteExtended;
private isAuthenticated = false;
private previousPage: number | null = null;
private previousSearchQuery: string | null = null;

constructor(
private readonly service: CompaniesService,
Expand All @@ -31,6 +33,11 @@ export class CompanyPageComponent implements OnInit, OnDestroy {
private readonly gtag: GoogleAnalyticsService,
) {
this.activateRoute = new ActivatedRouteExtended(activatedRoute);
const queryParams = this.router.getCurrentNavigation()?.extras.state;
if (queryParams) {
this.previousPage = queryParams['page'] || null;
this.previousSearchQuery = queryParams['search'] || null;
}
}

ngOnInit(): void {
Expand Down Expand Up @@ -64,6 +71,20 @@ export class CompanyPageComponent implements OnInit, OnDestroy {
});
}

goBackToList(): void {
if (this.previousPage) {
const queryParams: any = { page: this.previousPage };

if (this.previousSearchQuery) {
queryParams.search = this.previousSearchQuery;
}

this.router.navigate(['/companies'], { queryParams });
} else {
this.router.navigate(['/companies']);
}
}

leaveReview(): void {
if (this.company == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from "@angular/core";
import { PaginatedModel } from "@models/paginated-list";

@Component({
selector: "app-pagination-buttons",
templateUrl: "./pagination-buttons.component.html",
standalone: false,
})
export class PaginationButtonsComponent implements OnInit {
export class PaginationButtonsComponent implements OnInit, OnChanges {
@Input()
source: PaginatedModel | null = null;

Expand Down Expand Up @@ -37,6 +37,16 @@ export class PaginationButtonsComponent implements OnInit {
}

ngOnInit(): void {
this.generatePages();
}

ngOnChanges(changes: SimpleChanges): void {
if (changes['source'] || changes['currentPage']) {
this.generatePages();
}
}

private generatePages(): void {
if (this.source) {
this.lastPage = Math.ceil(this.source.totalItems / this.source.pageSize);
const allPages = Array.from(Array(this.lastPage).keys()).map(
Expand Down
Loading