Skip to content
Snippets Groups Projects
Commit a69b8484 authored by Gergely Reményi's avatar Gergely Reményi
Browse files

Make history ordered by date

By default the service history is ordered by it's dates descending.
The ordering can be changed by a button in the top right corner,
which is hidden when a user is logged in.
parent c6471354
Branches
Tags
1 merge request!1small page rework
......@@ -75,6 +75,14 @@
<h5 class='base_container_header'>
Története
</h5>
<button class='btn' *ngIf='currentHistoryOrderState === historyOrderState.Descending && !isLoggedIn'
(click)='toggleHistoryOrder(historyOrderState.Ascending)'>
</button>
<button class='btn' *ngIf='currentHistoryOrderState === historyOrderState.Ascending && !isLoggedIn'
(click)='toggleHistoryOrder(historyOrderState.Descending)'>
</button>
<button class='btn btn-link' *ngIf='isLoggedIn'
(click)='updateState(serviceDetailComponentState.CREATE_HISTORY)'>
Új hozzáadása
......@@ -136,7 +144,7 @@
<div class='border-top mt-3 pt-3' *ngIf='!service.history'>
Nincs
</div>
<div class='border-top mt-3 pt-3' *ngFor='let history of service.history; TrackBy: trackById'>
<div class='border-top mt-3 pt-3' *ngFor='let history of sortByCreation(service.history, currentHistoryOrderState); TrackBy: trackById'>
<div>
{{history.createdAt | date: 'yyyy. MMM dd. hh:mm:ss'}}
</div>
......
......@@ -21,6 +21,14 @@ export enum ServiceDetailComponentState {
DELETE_HISTORY,
}
/**
* States of history ordering
*/
export enum HistoryOrderState {
Ascending,
Descending
}
/**
* Detail component of a service
*/
......@@ -95,6 +103,16 @@ export class ServiceDetailComponent implements OnInit {
description: new FormControl('', [Validators.required]),
});
/**
* History order state
*/
historyOrderState = HistoryOrderState;
/**
* Current state of history ordering
*/
currentHistoryOrderState: HistoryOrderState = HistoryOrderState.Descending;
constructor(
private route: ActivatedRoute,
private router: Router,
......@@ -130,6 +148,32 @@ export class ServiceDetailComponent implements OnInit {
return item.id;
}
/**
* Sort history by creation date
* @param history unsorted history
* @param order sorting direction
* @returns sorted history
*/
private sortByCreation(history: History[], order: HistoryOrderState): History[] {
if (order === this.historyOrderState.Ascending) {
return history.sort((a, b) => a.createdAt > b.createdAt ? 1 : a.createdAt === b.createdAt ? 0 : -1);
}
else if (order === this.historyOrderState.Descending) {
return history.sort((a, b) => a.createdAt < b.createdAt ? 1 : a.createdAt === b.createdAt ? 0 : -1);
}
else {
throw new Error("Unexpected history order");
}
}
/**
* Toggle history order
* @param newOrder new ordering of history
*/
private toggleHistoryOrder(newOrder: HistoryOrderState) {
this.currentHistoryOrderState = newOrder;
}
/**
* Update the current state of the component
* @param newState the new State
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment