-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Overhaul data collector modal
- Loading branch information
Showing
8 changed files
with
70 additions
and
72 deletions.
There are no files selected for viewing
51 changes: 29 additions & 22 deletions
51
src/app/audit/add-data-collector-modal/add-data-collector-modal.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
<div class="modal-header"> | ||
<h4 class="modal-title">Add Data Collector ({{ audit.auditName | titlecase }})</h4> | ||
<button type="button" class="btn-close btn-close-white" (click)="activeModal.dismiss()"> | ||
</button> | ||
</div> | ||
<div class="modal-body" style="height: 200px; overflow-y: scroll;"> | ||
<ul> | ||
@for (item of dataCollectors; track item) { | ||
<li class="data-collector-item"> | ||
<label> | ||
<input type="checkbox" [(ngModel)]="item.selected"/> | ||
{{ item.userName | titlecase }} | ||
<pre>{{ item.email }}</pre> | ||
</label> | ||
</li> | ||
} | ||
</ul> | ||
</div> | ||
<div class="modal-footer"> | ||
<button type="button" class="btn btn-secondary" (click)="activeModal.dismiss()">Close</button> | ||
<button type="button" class="btn btn-primary" (click)="onOkClick()">Add</button> | ||
</div> | ||
<ngbx-modal #modal [back]="['..']" [scrollable]="true"> | ||
<ng-container modal-title> | ||
Add Data Collector | ||
<span class="text-muted">{{ audit?.auditName | titlecase }}</span> | ||
</ng-container> | ||
<ng-container modal-body> | ||
<ul class="list-unstyled"> | ||
@for (item of dataCollectors; track item) { | ||
<li> | ||
<div class="form-check"> | ||
<input type="checkbox" class="form-check-input" id="data-collector-{{ item.id }}" [(ngModel)]="selected[item.id]"/> | ||
<label class="form-check-label" for="data-collector-{{ item.id }}"> | ||
{{ item.userName | titlecase }} | ||
<pre>{{ item.email }}</pre> | ||
</label> | ||
</div> | ||
</li> | ||
} | ||
</ul> | ||
</ng-container> | ||
<ng-container modal-footer> | ||
<button type="button" class="btn btn-secondary" (click)="modal.close()"> | ||
Cancel | ||
</button> | ||
<button type="button" class="btn btn-primary" (click)="onOkClick(); modal.close()"> | ||
Save | ||
</button> | ||
</ng-container> | ||
</ngbx-modal> |
60 changes: 27 additions & 33 deletions
60
src/app/audit/add-data-collector-modal/add-data-collector-modal.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,53 @@ | ||
import {ChangeDetectorRef, Component, OnInit} from '@angular/core'; | ||
import {NgbActiveModal} from '@ng-bootstrap/ng-bootstrap'; | ||
import {DataCollector} from './data-collector.interface'; | ||
import {Component, OnInit} from '@angular/core'; | ||
import {DataCollector} from '../../shared/model/data-collector.interface'; | ||
import {AuditService} from 'src/app/shared/services/audit.service'; | ||
import {ToastService} from '@mean-stream/ngbx'; | ||
import {Audit} from '../../shared/model/audit.interface'; | ||
import {ActivatedRoute} from '@angular/router'; | ||
import {switchMap} from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'app-add-data-collector-modal', | ||
templateUrl: './add-data-collector-modal.component.html', | ||
styleUrls: ['./add-data-collector-modal.component.scss'], | ||
}) | ||
export class AddDataCollectorModalComponent implements OnInit { | ||
audit: any; | ||
audit?: Audit; | ||
dataCollectors: DataCollector[] = []; | ||
selected: Partial<Record<number, boolean>> = {}; | ||
|
||
constructor( | ||
private auditService: AuditService, | ||
public activeModal: NgbActiveModal, | ||
private cdRef: ChangeDetectorRef, | ||
private toastService: ToastService, | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.fetchDataCollectors(); | ||
private route: ActivatedRoute, | ||
) { | ||
} | ||
|
||
fetchDataCollectors(): void { | ||
this.auditService.dataCollectors(this.audit.auditId).subscribe((res: DataCollector[]) => { | ||
this.dataCollectors = res.map((item: DataCollector) => ({ | ||
...item, | ||
selected: false, | ||
})); | ||
this.cdRef.detectChanges(); | ||
ngOnInit(): void { | ||
this.route.params.pipe( | ||
switchMap(({aid}) => this.auditService.getSingleAudit(aid)), | ||
).subscribe(({data}) => this.audit = data); | ||
|
||
this.route.params.pipe( | ||
switchMap(({aid}) => this.auditService.dataCollectors(aid)), | ||
).subscribe((res: DataCollector[]) => { | ||
this.dataCollectors = res; | ||
this.selected = {}; | ||
}); | ||
} | ||
|
||
onOkClick(): void { | ||
const selectedDataCollectors = this.dataCollectors.filter((item) => item.selected); | ||
|
||
if (!selectedDataCollectors.length) { | ||
this.activeModal.close(); | ||
const assigned = this.dataCollectors | ||
.filter(d => this.selected[d.id]) | ||
.map(d => ({ | ||
auditId: this.audit!.auditId, | ||
dataCollectorId: d.id, | ||
})); | ||
if (!assigned.length) { | ||
return; | ||
} | ||
|
||
let dataCollectorArray:any = []; | ||
for (const dataCollector of selectedDataCollectors) { | ||
const assignmentRequest = { | ||
dataCollectorId: dataCollector.id, | ||
auditId: this.audit.auditId, | ||
}; | ||
dataCollectorArray.push(assignmentRequest); | ||
} | ||
|
||
this.auditService.assignAudits(dataCollectorArray).subscribe((res: any)=>{ | ||
this.auditService.assignAudits(assigned).subscribe(() => { | ||
this.toastService.success('Success', `Audit Assignment successful`); | ||
}); | ||
this.activeModal.close(); | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
src/app/audit/add-data-collector-modal/data-collector.interface.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface DataCollector { | ||
id: number; | ||
userName: string; | ||
email: string; | ||
state: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters