fix sorting service, hide restore button if time passed

This commit is contained in:
Dan Percic 2021-07-28 16:03:36 +03:00
parent bad4dedbf5
commit 8609609b55
4 changed files with 55 additions and 50 deletions

View File

@ -39,7 +39,7 @@
<redaction-table-col-name
[label]="'trash.table-col-names.name' | translate"
[withSort]="true"
column="name"
column="dossierName"
></redaction-table-col-name>
<redaction-table-col-name
[label]="'trash.table-col-names.owner' | translate"
@ -48,12 +48,12 @@
<redaction-table-col-name
[label]="'trash.table-col-names.deleted-on' | translate"
[withSort]="true"
column="dateDeleted"
column="softDeletedTime"
></redaction-table-col-name>
<redaction-table-col-name
[label]="'trash.table-col-names.time-to-restore' | translate"
[withSort]="true"
column="timeToRestore"
column="softDeletedTime"
></redaction-table-col-name>
<div class="scrollbar-placeholder"></div>
</div>
@ -106,6 +106,7 @@
</div>
<div class="action-buttons">
<redaction-circle-button
*ngIf="canRestore(entity.softDeletedTime)"
(action)="bulkRestore([entity.dossierId])"
[tooltip]="'trash.action.restore' | translate"
icon="red:put-back"

View File

@ -1,7 +1,6 @@
import { ChangeDetectionStrategy, Component, Injector, OnInit } from '@angular/core';
import { AppStateService } from '@state/app-state.service';
import { PermissionsService } from '@services/permissions.service';
import { Dossier, StatusControllerService } from '@redaction/red-ui-http';
import { Dossier } from '@redaction/red-ui-http';
import { LoadingService } from '@services/loading.service';
import { AppConfigKey, AppConfigService } from '../../../app-config/app-config.service';
import * as moment from 'moment';
@ -12,6 +11,9 @@ import { SortingService } from '@services/sorting.service';
import { BaseListingComponent } from '@shared/base/base-listing.component';
import { DossiersService } from '../../../dossier/services/dossiers.service';
const HOURS_IN_A_DAY = 24;
const MINUTES_IN_AN_HOUR = 60;
@Component({
templateUrl: './trash-screen.component.html',
styleUrls: ['./trash-screen.component.scss'],
@ -24,8 +26,6 @@ export class TrashScreenComponent extends BaseListingComponent<Dossier> implemen
private readonly _deleteRetentionHours = this._appConfigService.getConfig(AppConfigKey.DELETE_RETENTION_HOURS);
constructor(
private readonly _appStateService: AppStateService,
private readonly _statusControllerService: StatusControllerService,
readonly permissionsService: PermissionsService,
protected readonly _injector: Injector,
private readonly _dossiersService: DossiersService,
@ -47,6 +47,19 @@ export class TrashScreenComponent extends BaseListingComponent<Dossier> implemen
this.screenStateService.setEntities(await this._dossiersService.getDeleted());
}
canRestore(softDeletedTime: string): boolean {
const date = moment(this.getRestoreDate(softDeletedTime));
const now = new Date(Date.now());
const daysLeft = date.diff(now, 'days');
const hoursFromNow = date.diff(now, 'hours');
const hoursLeft = hoursFromNow - HOURS_IN_A_DAY * daysLeft;
const minutesFromNow = date.diff(now, 'minutes');
const minutesLeft = minutesFromNow - HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR * daysLeft;
return daysLeft >= 0 && hoursLeft >= 0 && minutesLeft > 0;
}
getRestoreDate(softDeletedTime: string): string {
return moment(softDeletedTime).add(this._deleteRetentionHours, 'hours').toISOString();
}

View File

@ -5,7 +5,7 @@ import { SearchService } from '../services/search.service';
import { ScreenStateService } from '../services/screen-state.service';
import { combineLatest, Observable } from 'rxjs';
import { AutoUnsubscribeComponent } from './auto-unsubscribe.component';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { distinctUntilChanged, map, switchMap } from 'rxjs/operators';
import { PermissionsService } from '@services/permissions.service';
import { FilterService } from '../services/filter.service';
@ -14,14 +14,14 @@ export abstract class BaseListingComponent<T> extends AutoUnsubscribeComponent i
@ViewChild(CdkVirtualScrollViewport)
readonly scrollViewport: CdkVirtualScrollViewport;
readonly permissionsService: PermissionsService;
readonly filterService: FilterService;
readonly sortingService: SortingService;
readonly searchService: SearchService;
readonly screenStateService: ScreenStateService<T>;
readonly permissionsService = this._injector.get(PermissionsService);
readonly filterService = this._injector.get(FilterService);
readonly sortingService = this._injector.get(SortingService);
readonly searchService = this._injector.get(SearchService);
readonly screenStateService = this._injector.get<ScreenStateService<T>>(ScreenStateService);
readonly sortedDisplayedEntities$: Observable<T[]>;
readonly noMatch$: Observable<boolean>;
readonly sortedDisplayedEntities$ = this._sortedDisplayedEntities$;
readonly noMatch$ = this._noMatch$;
protected readonly _tableHeaderLabel: string;
/**
@ -34,15 +34,7 @@ export abstract class BaseListingComponent<T> extends AutoUnsubscribeComponent i
protected constructor(protected readonly _injector: Injector) {
super();
this.trackByPrimaryKey = this.trackByPrimaryKey.bind(this);
this.permissionsService = this._injector.get(PermissionsService);
this.filterService = this._injector.get(FilterService);
this.sortingService = this._injector.get(SortingService);
this.searchService = this._injector.get(SearchService);
this.screenStateService = this._injector.get<ScreenStateService<T>>(ScreenStateService);
setTimeout(() => this.setInitialConfig());
this.sortedDisplayedEntities$ = this._sortedDisplayedEntities$;
this.noMatch$ = this._noMatch$;
}
get tableHeaderLabel(): string {
@ -58,7 +50,9 @@ export abstract class BaseListingComponent<T> extends AutoUnsubscribeComponent i
}
private get _sortedDisplayedEntities$(): Observable<T[]> {
return this.screenStateService.displayedEntities$.pipe(map(entities => this.sortingService.defaultSort(entities)));
return this.sortingService.sortingOptionChanged$.pipe(
switchMap(() => this.screenStateService.displayedEntities$.pipe(map(entities => this.sortingService.defaultSort(entities))))
);
}
private get _noMatch$(): Observable<boolean> {
@ -71,7 +65,7 @@ export abstract class BaseListingComponent<T> extends AutoUnsubscribeComponent i
setInitialConfig() {
this.sortingService.setSortingOption({
column: this._primaryKey,
order: SortingOrders.ASC
order: SortingOrders.asc
});
this.searchService.setSearchKey(this._primaryKey);
}

View File

@ -1,46 +1,40 @@
import { Injectable } from '@angular/core';
import { orderBy } from 'lodash';
import { BehaviorSubject } from 'rxjs';
export type SortingOrder = 'asc' | 'desc';
export const SortingOrders = {
asc: 'asc',
desc: 'desc'
} as const;
export const enum SortingOrders {
ASC = 'asc',
DESC = 'desc'
}
export type SortingOrder = keyof typeof SortingOrders;
export interface SortingOption {
order: SortingOrder;
column: string;
readonly order: SortingOrder;
readonly column: string;
}
@Injectable({
providedIn: 'root'
})
export class SortingService {
private _sortingOption: SortingOption;
private readonly _sortingOption$ = new BehaviorSubject<SortingOption>(null);
readonly sortingOptionChanged$ = this._sortingOption$.asObservable();
get sortingOption(): SortingOption {
return this._sortingOption;
}
private get _currentOrder(): SortingOrder {
return this._sortingOption.order;
}
private set _currentOrder(value: SortingOrder) {
this._sortingOption.order = value;
return this._sortingOption$.getValue();
}
setSortingOption(value: SortingOption): void {
this._sortingOption = value;
this._sortingOption$.next(value);
}
sort<T>(values: T[], order = '', column: string = ''): T[] {
if (!values || order === '' || !order) {
sort<T>(values: T[], order?: string, column?: string): T[] {
if (!values || !order) {
return values;
} // no array
if (!column || column === '') {
if (order === SortingOrders.ASC) {
if (!column) {
if (order === SortingOrders.asc) {
return values.sort();
} else {
return values.sort().reverse();
@ -57,10 +51,13 @@ export class SortingService {
}
toggleSort(column: string) {
if (this._sortingOption.column === column) {
this._currentOrder = this._currentOrder === SortingOrders.ASC ? SortingOrders.DESC : SortingOrders.ASC;
if (this.sortingOption.column === column) {
this._sortingOption$.next({
column,
order: this.sortingOption.order === SortingOrders.asc ? SortingOrders.desc : SortingOrders.asc
});
} else {
this._sortingOption = { column, order: SortingOrders.ASC };
this._sortingOption$.next({ column, order: SortingOrders.asc });
}
}
}