Skip to content

Commit b768035

Browse files
authored
NRPT-198: Link records to collection (#515)
1 parent 77c153b commit b768035

File tree

6 files changed

+133
-16
lines changed

6 files changed

+133
-16
lines changed

angular/projects/admin-nrpti/src/app/mines/mines-collections-add-edit/mines-collections-add-edit.component.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
22
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
33
import { FormArray, FormControl, FormGroup } from '@angular/forms';
44
import { ActivatedRoute, Router } from '@angular/router';
5+
import {Location} from '@angular/common';
56
import moment from 'moment';
67
import { DialogService } from 'ng2-bootstrap-modal';
78
import { LoadingScreenService, Utils, StoreService } from 'nrpti-angular-components';
@@ -44,6 +45,7 @@ export class MinesCollectionsAddEditComponent implements OnInit, OnDestroy {
4445
constructor(
4546
public route: ActivatedRoute,
4647
public router: Router,
48+
private location: Location,
4749
private recordUtils: RecordUtils,
4850
private factoryService: FactoryService,
4951
private loadingScreenService: LoadingScreenService,
@@ -172,7 +174,8 @@ export class MinesCollectionsAddEditComponent implements OnInit, OnDestroy {
172174
false
173175
),
174176
collectionRecords: new FormArray(
175-
(this.collectionState && this.getRecordsFormGroups(this.collectionState.collectionRecords)) ||
177+
// If editing and have selected records then combine them with existing collection records.
178+
(this.collectionState && this.getRecordsFormGroups(this.getUniqueRecords())) ||
176179
(this.collection && this.getRecordsFormGroups(this.collection.collectionRecords)) ||
177180
[]
178181
)
@@ -192,6 +195,26 @@ export class MinesCollectionsAddEditComponent implements OnInit, OnDestroy {
192195
}
193196
}
194197

198+
/**
199+
* If records are arriving form the Records List screen then combine them
200+
* with existing collection records, but make sure they are unique.
201+
*
202+
* @returns {obejct[]} Unique records.
203+
*/
204+
getUniqueRecords() {
205+
const records = this.collection && this.collection.collectionRecords || [];
206+
const stateRecords = this.collectionState && this.collectionState.collectionRecords || [];
207+
const collectionRecords = this.collection && this.collection.collectionRecords || [];
208+
209+
for (const stateRecord of stateRecords) {
210+
if (!collectionRecords.find(collectionRecord => stateRecord._id === collectionRecord._id)) {
211+
records.push(stateRecord);
212+
}
213+
}
214+
215+
return records;
216+
}
217+
195218
/**
196219
* Builds an array of records FormGroups, each with its own set of FormControls.
197220
*
@@ -413,7 +436,7 @@ export class MinesCollectionsAddEditComponent implements OnInit, OnDestroy {
413436
if (this.isEditing) {
414437
this.router.navigate(['mines', this.collection._master, 'collections', this.collection._id, 'detail']);
415438
} else {
416-
this.router.navigate(['../'], { relativeTo: this.route });
439+
this.location.back();
417440
}
418441
}
419442
}

angular/projects/admin-nrpti/src/app/mines/mines-collections-list/mines-collections-list.component.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
LoadingScreenService,
1111
TableObject,
1212
TableTemplateUtils,
13-
Utils
13+
Utils,
14+
StoreService,
1415
} from 'nrpti-angular-components';
1516
import { MinesCollectionsTableRowComponent } from '../mines-collections-rows/mines-collections-table-row.component';
1617
import {
@@ -21,6 +22,8 @@ import {
2122
DateFilterDefinition
2223
} from '../../../../../common/src/app/search-filter-template/filter-object';
2324
import { Mine } from '../../../../../common/src/app/models/bcmi/mine';
25+
import { StateIDs, StateStatus } from '../../../../../common/src/app/utils/record-constants';
26+
2427

2528
/**
2629
* Mine list page component.
@@ -75,11 +78,6 @@ export class MinesCollectionsListComponent implements OnInit, OnDestroy {
7578
name: 'Publish State',
7679
value: 'published',
7780
width: 'col-2'
78-
},
79-
{
80-
name: '', // Buttons
81-
width: 'col-1',
82-
nosort: true
8381
}
8482
];
8583

@@ -88,11 +86,15 @@ export class MinesCollectionsListComponent implements OnInit, OnDestroy {
8886
public queryParams: Params;
8987
public mine: Mine;
9088

89+
// collection add edit state
90+
public collectionState = null;
91+
9192
constructor(
9293
public location: Location,
9394
public router: Router,
9495
public route: ActivatedRoute,
9596
public utils: Utils,
97+
private storeService: StoreService,
9698
private loadingScreenService: LoadingScreenService,
9799
private tableTemplateUtils: TableTemplateUtils,
98100
private _changeDetectionRef: ChangeDetectorRef
@@ -157,6 +159,8 @@ export class MinesCollectionsListComponent implements OnInit, OnDestroy {
157159
ngOnInit(): void {
158160
this.loadingScreenService.setLoadingState(true, 'body');
159161

162+
this.setOrRemoveCollectionAddEditState();
163+
160164
this.route.params.pipe(takeUntil(this.ngUnsubscribe)).subscribe((params: Params) => {
161165
this.queryParams = { ...params };
162166
// Get params from route, shove into the tableTemplateUtils so that we get a new dataset to work with.
@@ -325,6 +329,13 @@ export class MinesCollectionsListComponent implements OnInit, OnDestroy {
325329
* @memberof MinesCollectionsListComponent
326330
*/
327331
ngOnDestroy(): void {
332+
// When the component is destroying, if collectionAddEdit state exists, but the user hadn't clicked the
333+
// 'Add to collection' button, then remove the state from the store.
334+
const collectionAddEditState = this.storeService.getItem(StateIDs.collectionAddEdit);
335+
if (collectionAddEditState && collectionAddEditState.status !== StateStatus.valid) {
336+
this.storeService.removeItem(StateIDs.collectionAddEdit);
337+
}
338+
328339
this.loadingScreenService.setLoadingState(false, 'body');
329340

330341
this.ngUnsubscribe.next();
@@ -356,6 +367,23 @@ export class MinesCollectionsListComponent implements OnInit, OnDestroy {
356367
this.submit();
357368
}
358369

370+
371+
/**
372+
* Sets the initial collectionState state, or removes it from the store if it is invalid.
373+
*
374+
* @memberof MinesRecordsListComponent
375+
*/
376+
setOrRemoveCollectionAddEditState() {
377+
const tempCollectionAddEditState = this.storeService.getItem(StateIDs.collectionAddEdit);
378+
if (tempCollectionAddEditState) {
379+
if (tempCollectionAddEditState.status === StateStatus.invalid) {
380+
this.storeService.removeItem(StateIDs.collectionAddEdit);
381+
} else {
382+
this.collectionState = tempCollectionAddEditState;
383+
}
384+
}
385+
}
386+
359387
private clearQueryParamsFilters() {
360388
delete this.queryParams['keywords'];
361389
delete this.queryParams['isBcmiPublished'];

angular/projects/admin-nrpti/src/app/mines/mines-collections-rows/mines-collections-table-row.component.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
</ng-template>
1818
</td>
1919
<td scope="row" class="col-1 d-xl-flex" (click)="$event.stopPropagation()">
20-
<button class="btn" type="button" (click)="goToEdit()" title="Edit record">
20+
<button class="btn" type="button" [disabled]="isEditingCollection" (click)="goToEdit()" title="Edit record">
2121
<i class="material-icons">create</i>
2222
</button>
23-
<button mat-icon-button class="btn" [matMenuTriggerFor]="menu" title="More options" aria-label="More options menu">
23+
<button mat-icon-button class="btn" [disabled]="isEditingCollection" [matMenuTriggerFor]="menu" title="More options" aria-label="More options menu">
2424
<i class="material-icons">more_vert</i>
2525
</button>
2626
<mat-menu #menu="matMenu">
27-
<button mat-menu-item (click)="goToDetails()" title="View collection details">
27+
<button mat-menu-item [disabled]="isEditingCollection" (click)="goToDetails()" title="View collection details">
2828
View Collection Details
2929
</button>
3030
<button
3131
mat-menu-item
3232
*ngIf="rowData.read && !rowData.read.includes('public')"
33+
[disabled]="isEditingCollection"
3334
(click)="publishCollection()"
3435
title="Publish collection"
3536
>
@@ -38,12 +39,13 @@
3839
<button
3940
mat-menu-item
4041
*ngIf="rowData.read && rowData.read.includes('public')"
42+
[disabled]="isEditingCollection"
4143
(click)="unpublishCollection()"
4244
title="Unpublish collection"
4345
>
4446
Unpublish Collection
4547
</button>
46-
<button mat-menu-item (click)="deleteCollection()" title="Delete collection">
48+
<button mat-menu-item [disabled]="isEditingCollection" (click)="deleteCollection()" title="Delete collection">
4749
Delete Collection
4850
</button>
4951
</mat-menu>

angular/projects/admin-nrpti/src/app/mines/mines-collections-rows/mines-collections-table-row.component.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { ChangeDetectorRef, Component, HostListener, OnInit, OnDestroy } from '@angular/core';
22
import { Router, ActivatedRoute } from '@angular/router';
33
import { DialogService } from 'ng2-bootstrap-modal';
4-
import { TableRowComponent } from 'nrpti-angular-components';
4+
import { TableRowComponent, StoreService } from 'nrpti-angular-components';
55
import { ConfirmComponent } from '../../confirm/confirm.component';
66
import { FactoryService } from '../../services/factory.service';
7+
import { StateIDs, StateStatus } from '../../../../../common/src/app/utils/record-constants';
78
import { takeUntil, catchError } from 'rxjs/operators';
89
import { Subject, of } from 'rxjs';
910

@@ -13,11 +14,13 @@ import { Subject, of } from 'rxjs';
1314
styleUrls: ['./mines-collections-table-row.component.scss']
1415
})
1516
export class MinesCollectionsTableRowComponent extends TableRowComponent implements OnInit, OnDestroy {
17+
public isEditingCollection: boolean;
1618
private ngUnsubscribe: Subject<boolean> = new Subject<boolean>();
1719

1820
constructor(
1921
private router: Router,
2022
private route: ActivatedRoute,
23+
private storeService: StoreService,
2124
public changeDetectionRef: ChangeDetectorRef,
2225
public factoryService: FactoryService,
2326
private dialogService: DialogService
@@ -26,6 +29,8 @@ export class MinesCollectionsTableRowComponent extends TableRowComponent impleme
2629
}
2730

2831
ngOnInit() {
32+
this.setOrRemoveCollectionAddEditState();
33+
2934
this.changeDetectionRef.detectChanges();
3035
}
3136

@@ -38,7 +43,11 @@ export class MinesCollectionsTableRowComponent extends TableRowComponent impleme
3843
* @memberof MinesCollectionsTableRowComponent
3944
*/
4045
@HostListener('click') onItemClicked() {
41-
this.goToDetails();
46+
if (this.isEditingCollection) {
47+
this.goToEdit();
48+
} else {
49+
this.goToDetails();
50+
}
4251
}
4352

4453
/**
@@ -156,7 +165,29 @@ export class MinesCollectionsTableRowComponent extends TableRowComponent impleme
156165
});
157166
}
158167

168+
/**
169+
* Sets the initial collectionState state, or removes it from the store if it is invalid.
170+
*
171+
* @memberof MinesRecordsListComponent
172+
*/
173+
setOrRemoveCollectionAddEditState() {
174+
const tempCollectionAddEditState = this.storeService.getItem(StateIDs.collectionAddEdit);
175+
if (tempCollectionAddEditState) {
176+
if (tempCollectionAddEditState.status === StateStatus.invalid) {
177+
this.storeService.removeItem(StateIDs.collectionAddEdit);
178+
this.isEditingCollection = false;
179+
} else {
180+
this.isEditingCollection = true;
181+
}
182+
}
183+
}
184+
159185
ngOnDestroy() {
186+
const collectionAddEditState = this.storeService.getItem(StateIDs.collectionAddEdit);
187+
if (collectionAddEditState && collectionAddEditState.status !== StateStatus.valid) {
188+
this.storeService.removeItem(StateIDs.collectionAddEdit);
189+
}
190+
160191
this.ngUnsubscribe.next();
161192
this.ngUnsubscribe.complete();
162193
}

angular/projects/admin-nrpti/src/app/mines/mines-records-list/mines-records-list.component.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ <h1 id="name" class="m-0">{{ (mine && mine.name) || '-' }}</h1>
5252
</search-filter-template>
5353
</section>
5454

55+
<section class="d-flex justify-content-end">
56+
<div>
57+
<span ngbDropdown>
58+
<button class="btn btn-primary" id="actionDropdown" ngbDropdownToggle>Actions</button>
59+
<div class="dropdown-menu" aria-labelledby="actionDropdown" ngbDropdownMenu>
60+
<button class="dropdown-item" [disabled]="!anySelectedRecords()" (click)="submitAddEditCollectionRecords()">Add to new Collection</button>
61+
<button class="dropdown-item" [disabled]="!anySelectedRecords()" (click)="submitAddToExistingCollection()">Add to existing Collection</button>
62+
</div>
63+
</span>
64+
</div>
65+
</section>
66+
5567
<section *ngIf="collectionState">
5668
<div class="d-flex justify-content-end mb-1">
5769
<div>

angular/projects/admin-nrpti/src/app/mines/mines-records-list/mines-records-list.component.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export class MinesRecordsListComponent implements OnInit, OnDestroy {
439439
* @memberof MinesRecordsListComponent
440440
*/
441441
updateCollectionAddEditState(rowData: any, checked: boolean) {
442-
if (!rowData || !this.collectionState) {
442+
if (!rowData) {
443443
return;
444444
}
445445

@@ -540,13 +540,34 @@ export class MinesRecordsListComponent implements OnInit, OnDestroy {
540540
}
541541
});
542542

543-
if (this.collectionState.collectionId) {
543+
if (this.collectionState && this.collectionState.collectionId) {
544544
this.router.navigate(['mines', this.mine._id, 'collections', this.collectionState.collectionId, 'edit']);
545545
} else {
546546
this.router.navigate(['mines', this.mine._id, 'collections', 'add']);
547547
}
548548
}
549549

550+
/**
551+
* Submit adding records to an existing collection.
552+
*
553+
* @memberof MinesRecordsListComponent
554+
*/
555+
submitAddToExistingCollection() {
556+
// Mark collectionAddEdit status as valid
557+
this.storeService.setItem({
558+
[StateIDs.collectionAddEdit]: {
559+
...this.storeService.getItem(StateIDs.collectionAddEdit),
560+
status: StateStatus.valid
561+
}
562+
});
563+
564+
this.router.navigate(['mines', this.mine._id, 'collections']);
565+
}
566+
567+
anySelectedRecords() {
568+
return this.storeService.getItem(StateIDs.collectionAddEdit) ? true : false;
569+
}
570+
550571
/**
551572
* Cleanup on component destroy.
552573
*

0 commit comments

Comments
 (0)