refactor latest-activity in progress

*added pagination info on url
This commit is contained in:
Sofia Papacharalampous 2024-05-23 15:15:46 +03:00
parent 86e665452d
commit 0ed00a9dd8
1 changed files with 35 additions and 29 deletions

View File

@ -22,6 +22,7 @@ import { DmpService } from '@app/core/services/dmp/dmp.service';
import { MatomoService } from '@app/core/services/matomo/matomo-service'; import { MatomoService } from '@app/core/services/matomo/matomo-service';
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
import { BaseComponent } from '@common/base/base.component'; import { BaseComponent } from '@common/base/base.component';
import { Lookup } from '@common/model/lookup';
import { debounceTime, takeUntil } from 'rxjs/operators'; import { debounceTime, takeUntil } from 'rxjs/operators';
import { nameof } from 'ts-simple-nameof'; import { nameof } from 'ts-simple-nameof';
@ -36,7 +37,6 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
currentPage: number = 0; currentPage: number = 0;
pageSize: number = 5; pageSize: number = 5;
latestBatchCount: number = 0;
listingItems: RecentActivityItem[]= []; listingItems: RecentActivityItem[]= [];
@Input() includeDescriptions: boolean = true; @Input() includeDescriptions: boolean = true;
@Input() includeDmps: boolean = true; @Input() includeDmps: boolean = true;
@ -53,11 +53,9 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
order = RecentActivityOrder; order = RecentActivityOrder;
totalCount: number; totalCount: number;
startIndex: number = 0;
offsetLess: number = 0; offsetLess: number = 0;
page: number = 1;
@Input() isActive: boolean = false; @Input() isActive: boolean = false;
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
@ -75,13 +73,9 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
this.matomoService.trackPageView('Recent DMP Activity'); this.matomoService.trackPageView('Recent DMP Activity');
this.route.queryParams.subscribe(params => { this.route.queryParams.subscribe(params => {
if (this.isActive) { if (this.isActive) {
let page = (params['page'] === undefined) ? 1 : +params['page']; let page = (params['page'] === undefined) ? 0 : + params['page'];
this.page = (page <= 0) ? 1 : page; this.currentPage = (page <= 0) ? 0 : page;
this.startIndex = (this.page - 1) * this.pageSize;
if (this.page > 1) {
this.offsetLess = (this.page - 2) * this.pageSize;
}
let order = params['order']; let order = params['order'];
if (this.isAuthenticated()) { if (this.isAuthenticated()) {
@ -123,20 +117,28 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
} }
updateUrl() { updateUrl() {
let parameters = "" + let parametersArray: string[] = [
(this.page != 1 ? "&page=" + this.page : "") + ...(this.currentPage > 1 ? ["page=" + this.currentPage] : []),
//TODO refactor ...(this.formGroup.get("like").value ? ["&keyword=" + this.formGroup.get("like").value] : [])
// (((this.formGroup.get("order").value != this.order.UpdatedAt && !this.publicMode) || (this.formGroup.get("order").value != this.order.Published && this.publicMode)) ? "&order=" + this.formGroup.get("order").value : "") + ]
(this.formGroup.get("like").value ? ("&keyword=" + this.formGroup.get("like").value) : "");
this.location.go(this.router.url.split('?')[0] + parameters); let parameters = "";
if (parametersArray.length > 0){
parameters = "?";
parameters += parametersArray.join('&');
}
this.location.go(this.router.url.split('?')[0] + parameters);
} }
public isAuthenticated(): boolean { public isAuthenticated(): boolean {
return this.authentication.currentAccountIsAuthenticated(); return this.authentication.currentAccountIsAuthenticated();
} }
refresh(): void { refresh(resetPagination: boolean = false): void {
this._resetPagination(); if (resetPagination) this._resetPagination();
this.lookup.orderField = this.formGroup.get('order').value ?? this.order.UpdatedAt; this.lookup.orderField = this.formGroup.get('order').value ?? this.order.UpdatedAt;
this.lookup.like = this.formGroup.get('like').value; this.lookup.like = this.formGroup.get('like').value;
this.lookup.project = { this.lookup.project = {
@ -146,24 +148,25 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
] ]
}; };
this.loadMore(); this.loadMore({ size: this.pageSize*this.currentPage, offset: 0 }, false);
} }
loadMore() { loadMore(page?: Lookup.Paging, updatePage: boolean = true) {
this.lookup.page = { size: this.pageSize, offset: this.pageSize*this.currentPage }; if (!page) page = { size: this.pageSize, offset: this.pageSize*this.currentPage };
this.loadItems(PaginationAction.LoadMore); this.lookup.page = page;
this.loadItems(PaginationAction.LoadMore, updatePage);
} }
loadLess() { loadLess() {
this.loadItems(PaginationAction.LoadLess); this.loadItems(PaginationAction.LoadLess);
} }
private loadItems(action: PaginationAction){ private loadItems(action: PaginationAction, updatePage: boolean = true){
if (action == PaginationAction.LoadLess) { if (action == PaginationAction.LoadLess) {
this.listingItems = this.listingItems.slice(0, this.listingItems.length-this.latestBatchCount); let latestBatchCount = this.listingItems.length%this.pageSize == 0 ? this.pageSize : this.listingItems.length%this.pageSize;
this.listingItems = this.listingItems.slice(0, this.listingItems.length-latestBatchCount);
this.latestBatchCount = this.pageSize; this._setPage(this.currentPage-1);
this.currentPage --;
return; return;
} }
@ -174,8 +177,6 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
.subscribe(response => { .subscribe(response => {
if (response == null) return; if (response == null) return;
this.latestBatchCount = response.length;
response.forEach(item => { response.forEach(item => {
if (item.dmp){ if (item.dmp){
if (item.dmp.descriptions) { if (item.dmp.descriptions) {
@ -193,7 +194,7 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
} }
}) })
if (response.length > 0) this.currentPage ++; if (updatePage && response.length > 0 && this.listingItems.length >= this.currentPage*this.pageSize) this._setPage(this.currentPage+1);
}); });
} }
@ -202,6 +203,11 @@ export class RecentEditedActivityComponent extends BaseComponent implements OnIn
this.currentPage = 0; this.currentPage = 0;
} }
private _setPage(page: number) {
this.currentPage = page;
this.updateUrl();
}
private _getDmpLookup(): string[] { private _getDmpLookup(): string[] {
return [ return [
[nameof<RecentActivityItem>(x => x.dmp), nameof<Dmp>(x => x.id)].join('.'), [nameof<RecentActivityItem>(x => x.dmp), nameof<Dmp>(x => x.id)].join('.'),