-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocuments.component.ts
144 lines (129 loc) · 4.97 KB
/
documents.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { PlanningReviewDetailService } from '../../../services/planning-review/planning-review-detail.service';
import { PlanningReviewDocumentDto } from '../../../services/planning-review/planning-review-document/planning-review-document.dto';
import { PlanningReviewDocumentService } from '../../../services/planning-review/planning-review-document/planning-review-document.service';
import { ToastService } from '../../../services/toast/toast.service';
import { ConfirmationDialogService } from '../../../shared/confirmation-dialog/confirmation-dialog.service';
import { DOCUMENT_SYSTEM } from '../../../shared/document/document.dto';
import { FILE_NAME_TRUNCATE_LENGTH } from '../../../shared/constants';
import { DocumentUploadDialogComponent } from '../../../shared/document-upload-dialog/document-upload-dialog.component';
import {
DocumentUploadDialogData,
DocumentUploadDialogOptions,
} from '../../../shared/document-upload-dialog/document-upload-dialog.interface';
const DOCUMENT_UPLOAD_DIALOG_OPTIONS: DocumentUploadDialogOptions = {
allowedVisibilityFlags: ['C'],
allowsFileEdit: true,
};
@Component({
selector: 'app-documents',
templateUrl: './documents.component.html',
styleUrls: ['./documents.component.scss'],
})
export class DocumentsComponent implements OnInit {
displayedColumns: string[] = ['source', 'type', 'fileName', 'visibilityFlags', 'uploadedAt', 'actions'];
documents: PlanningReviewDocumentDto[] = [];
private fileId = '';
DOCUMENT_SYSTEM = DOCUMENT_SYSTEM;
hasBeenSetForDiscussion = false;
@ViewChild(MatSort) sort!: MatSort;
dataSource: MatTableDataSource<PlanningReviewDocumentDto> = new MatTableDataSource<PlanningReviewDocumentDto>();
readonly fileNameTruncLen = FILE_NAME_TRUNCATE_LENGTH;
constructor(
private planningReviewDocumentService: PlanningReviewDocumentService,
private planningReviewDetailService: PlanningReviewDetailService,
private confirmationDialogService: ConfirmationDialogService,
private toastService: ToastService,
public dialog: MatDialog,
) {}
ngOnInit(): void {
this.planningReviewDetailService.$planningReview.subscribe((planningReview) => {
if (planningReview) {
this.fileId = planningReview.fileNumber;
this.loadDocuments(planningReview.fileNumber);
this.hasBeenSetForDiscussion = planningReview.meetings.length > 0;
}
});
}
async onUploadFile() {
const data: DocumentUploadDialogData = {
...DOCUMENT_UPLOAD_DIALOG_OPTIONS,
...{
fileId: this.fileId,
documentService: this.planningReviewDocumentService,
},
};
this.dialog
.open(DocumentUploadDialogComponent, {
minWidth: '600px',
maxWidth: '800px',
width: '70%',
data,
})
.afterClosed()
.subscribe((isDirty) => {
if (isDirty) {
this.loadDocuments(this.fileId);
}
});
}
async openFile(uuid: string, fileName: string) {
await this.planningReviewDocumentService.download(uuid, fileName);
}
async downloadFile(uuid: string, fileName: string) {
await this.planningReviewDocumentService.download(uuid, fileName, false);
}
private async loadDocuments(fileNumber: string) {
this.documents = await this.planningReviewDocumentService.listAll(fileNumber);
this.dataSource = new MatTableDataSource(this.documents);
this.dataSource.sortingDataAccessor = (item, property) => {
switch (property) {
case 'type':
return item.type?.oatsCode;
default: // @ts-ignore Does not like using String for Key access, but that's what Angular provides
return item[property];
}
};
this.dataSource.sort = this.sort;
}
onEditFile(element: PlanningReviewDocumentDto) {
const data: DocumentUploadDialogData = {
...DOCUMENT_UPLOAD_DIALOG_OPTIONS,
...{
allowsFileEdit: element.system === DOCUMENT_SYSTEM.ALCS,
fileId: this.fileId,
existingDocument: element,
documentService: this.planningReviewDocumentService,
},
};
this.dialog
.open(DocumentUploadDialogComponent, {
minWidth: '600px',
maxWidth: '800px',
width: '70%',
data,
})
.afterClosed()
.subscribe((isDirty: boolean) => {
if (isDirty) {
this.loadDocuments(this.fileId);
}
});
}
onDeleteFile(element: PlanningReviewDocumentDto) {
this.confirmationDialogService
.openDialog({
body: 'Are you sure you want to delete the selected file?',
})
.subscribe(async (accepted) => {
if (accepted) {
await this.planningReviewDocumentService.delete(element.uuid);
this.loadDocuments(this.fileId);
this.toastService.showSuccessToast('Document deleted');
}
});
}
}