Skip to content

Commit

Permalink
Add sorting options
Browse files Browse the repository at this point in the history
  • Loading branch information
little9 committed Nov 15, 2023
1 parent a4afb84 commit 30a4317
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 15,966 deletions.
4 changes: 2 additions & 2 deletions cloudapp/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { MainComponent } from './main/main.component';
import { PrintButtonComponent } from './print-button/print-button.component';
import { StorageLocationIdSortPipe } from './storage-location-id-sort.pipe';
import { ApplySortPipe } from './main/apply_sort_pipe';

@NgModule({
declarations: [
AppComponent,
MainComponent,
PrintButtonComponent,
StorageLocationIdSortPipe
ApplySortPipe
],
imports: [
MaterialModule,
Expand Down
8 changes: 4 additions & 4 deletions cloudapp/src/app/interfaces/requested-resources.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export interface RequestedResources {
export interface Copy {
pid: string
barcode: string
base_status: BaseStatus
alternative_call_number: string
base_status?: BaseStatus
alternative_call_number?: string
storage_location_id: string
link: string
}
Expand All @@ -58,7 +58,7 @@ export interface RequestedResources {
}

export interface Request {
description: any
description?: any
id: string
destination: Destination
requester: Requester
Expand All @@ -70,7 +70,7 @@ export interface RequestedResources {
request_time: string
link: string
comment?: string
email: string
email?: string
}

export interface Destination {
Expand Down
48 changes: 48 additions & 0 deletions cloudapp/src/app/main/apply_sort_pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Pipe, PipeTransform } from '@angular/core';
import { RequestedResource } from '../interfaces/requested-resources.interface';

@Pipe({ name: 'applySort', pure: false })
export class ApplySortPipe implements PipeTransform {
transform(requestedResources: RequestedResource[], selectedSort: string): RequestedResource[] {
switch (selectedSort) {
case 'storageLocationIdSort':
return this.storageLocationIdSort(requestedResources, 'asc');
case 'requestDateSort':
return this.requestDateSort(requestedResources);
break;
default:
return requestedResources;
}
}

private storageLocationIdSort(requestedResources: RequestedResource[], sortOrder: 'asc' | 'desc'): RequestedResource[] {
if (!requestedResources) {
return [];
}

// If you sort the original array, it will only work the first time.
const copy = [...requestedResources];

return copy.sort((a, b) => {
const storageLocationIdA = a.location?.copy?.length ? a.location.copy[0].storage_location_id : '';
const storageLocationIdB = b.location?.copy?.length ? b.location.copy[0].storage_location_id : '';

return sortOrder === 'asc'
? storageLocationIdA.localeCompare(storageLocationIdB)
: storageLocationIdB.localeCompare(storageLocationIdA);
});
}

private requestDateSort(requestedResources: RequestedResource[]): RequestedResource[] {
const copy = [...requestedResources];

return copy.sort((a, b) => {

const dateA = a.request[0]?.request_date ? new Date(a.request[0].request_date) : new Date('1900-01-01T00:00:00Z');
const dateB = b.request[0]?.request_date ? new Date(b.request[0].request_date) : new Date('1900-01-01T00:00:00Z');

return dateA.getTime() - dateB.getTime();
});
}

}
69 changes: 39 additions & 30 deletions cloudapp/src/app/main/main.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@
</ul>
<div *ngIf="!loading">
<app-print-button></app-print-button>

<mat-form-field class="sort" appearance="fill">
<mat-label>Sort Selection</mat-label>
<mat-select [(value)]="selectedSort" title="Sort Selection">
<mat-option value="storageLocationIdSort">Sort by RMST</mat-option>
<mat-option value="requestDateSort">Sort by Date (Oldest First)</mat-option>
</mat-select>
</mat-form-field>
<br/>
<br/>
</div>
</mat-card-content>
</mat-card>

<div>
<div *ngFor="let resource of getVisibleResources() | storageLocationIdSort: 'asc'; index as i">
<div *ngFor="let resource of getVisibleResources() | applySort:selectedSort; index as i">
<div *ngIf="resource.location.copy[0]">
<mat-card>
<mat-card-title-group>
Expand All @@ -35,35 +45,34 @@
<ng-container *ngFor="let request of resource.request">
<fieldset>
<legend>Request {{ request.id }}</legend>

<mat-card-content>
<dl *ngFor="let copy of resource.location.copy">
<dt><strong>RMST</strong></dt>
<dd>{{ copy.storage_location_id }}</dd>
<dt><strong>Barcode</strong></dt>
<dd>{{ copy.barcode }}</dd>
<dt><strong>Status</strong></dt>
<dd>{{ copy.base_status.desc }}</dd>
</dl>

<dl>
<div *ngIf="request.description">
<dt><strong>Description</strong></dt>
<dd>{{ request.description }}</dd>
</div>
<dt><strong>Pickup Location</strong></dt>
<dd>{{ request.destination.desc }}</dd>
<dt><strong>Requester</strong></dt>
<dd>{{ request.requester.desc }}</dd>
<div *ngIf="request.email">
<dt>Requester Email</dt>
<dd>{{ request.email }}</dd>
</div>
</dl>
</mat-card-content>
</fieldset>
<br>
</ng-container>

<mat-card-content>
<dl *ngFor="let copy of resource.location.copy">
<dt><strong>RMST</strong></dt>
<dd>{{ copy.storage_location_id }}</dd>
<dt><strong>Barcode</strong></dt>
<dd>{{ copy.barcode }}</dd>
<dt><strong>Status</strong></dt>
<dd>{{ copy.base_status.desc }}</dd>
</dl>

<dl>
<dt><strong>Request Date</strong></dt>
<dd>{{ request.request_time | date: 'medium' }}</dd>

<dt><strong>Pickup Location</strong></dt>
<dd>{{ request.destination.desc }}</dd>
<dt><strong>Requester</strong></dt>
<dd>{{ request.requester.desc }}</dd>
<div *ngIf="request.email">
<dt>Requester Email</dt>
<dd>{{ request.email }}</dd>
</div>
</dl>
</mat-card-content>
</fieldset>
<br>
</ng-container>
</mat-card>
</div>
</div>
Expand Down
25 changes: 22 additions & 3 deletions cloudapp/src/app/main/main.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
.remove-button {
display: none;
}

dl {
display: flex;
flex-direction: row;
flex-wrap: wrap;
border-collapse: collapse;
width: 100%;
margin: 0;
padding: 0;
}

dt, dd {
display: flex;
flex: 1 1 auto;
justify-content: space-between;
align-items: center;
margin-left: 0;
padding: .25em;
box-sizing: border-box;
}
}

dl {
Expand All @@ -21,8 +41,7 @@ dl {
padding: 0;
}

dt,
dd {
dt, dd {
display: flex;
flex: 1;
justify-content: space-between;
Expand All @@ -33,4 +52,4 @@ dd {

dt {
font-weight: bold;
}
}
1 change: 1 addition & 0 deletions cloudapp/src/app/main/main.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class MainComponent implements OnInit, OnDestroy {
requestedResources: RequestedResources;
currentlyAtLibCode: string;
curentlyAtCircDeskCode: string;
selectedSort = 'storageLocationIdSort';

constructor(
private alert: AlertService,
Expand Down
39 changes: 39 additions & 0 deletions cloudapp/src/app/storage-location-id-sort.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { StorageLocationIdSortPipe } from './storage-location-id-sort.pipe';
import { RequestedResource } from './interfaces/requested-resources.interface';

describe('StorageLocationIdSortPipe', () => {
let pipe: StorageLocationIdSortPipe;

beforeEach(() => {
pipe = new StorageLocationIdSortPipe();
});

it('should sort the requested resources in ascending order by storage location ID', () => {
const input: RequestedResource[] = [
{ location: { copy: [{ storage_location_id: 'r1', pid:'1', barcode: '1', link: '' }] } },
{ location: { copy: [{ storage_location_id: 'r5', pid:'1', barcode: '1', link: '' }] } },
];
const output = pipe.transform(input, 'asc');
expect(output[0].location.copy[0].storage_location_id).toEqual('a');
expect(output[1].location.copy[0].storage_location_id).toEqual('b');
});

it('should sort the requested resources in descending order by storage location ID', () => {
const input: RequestedResource[] = [
{ location: { copy: [{ storage_location_id: 'r7', pid:'1', barcode: '1', link: '' }] } },
{ location: { copy: [{ storage_location_id: 'r1', pid:'1', barcode: '1', link: '' }] } },
];
const output = pipe.transform(input, 'desc');
expect(output[0].location.copy[0].storage_location_id).toEqual('r3');
expect(output[1].location.copy[0].storage_location_id).toEqual('r8');
});

it('should return an empty array when input is null or undefined', () => {
expect(pipe.transform(null)).toEqual([]);
expect(pipe.transform(undefined)).toEqual([]);
});

it('should return an empty array when input is empty', () => {
expect(pipe.transform([])).toEqual([]);
});
});
Loading

0 comments on commit 30a4317

Please sign in to comment.