forked from UoEMainLibrary/dspace-datashare-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmission-upload-files.component.ts
More file actions
215 lines (195 loc) · 6.97 KB
/
Copy pathsubmission-upload-files.component.ts
File metadata and controls
215 lines (195 loc) · 6.97 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { NgIf } from '@angular/common';
import {
Component,
Input,
OnChanges,
OnDestroy,
} from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
Subscription,
} from 'rxjs';
import {
first,
take,
} from 'rxjs/operators';
import { WorkspaceItem } from '../../../core/submission/models/workspaceitem.model';
import { SubmissionJsonPatchOperationsService } from '../../../core/submission/submission-json-patch-operations.service';
import { normalizeSectionData } from '../../../core/submission/submission-response-parsing.service';
import {
hasValue,
isEmpty,
isNotEmpty,
} from '../../../shared/empty.util';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { UploaderComponent } from '../../../shared/upload/uploader/uploader.component';
import { UploaderCompleteEvent } from '../../../shared/upload/uploader/uploader-complete-event.model';
import { UploaderError } from '../../../shared/upload/uploader/uploader-error.model';
import { UploaderOptions } from '../../../shared/upload/uploader/uploader-options.model';
import { SectionsService } from '../../sections/sections.service';
import { SectionsType } from '../../sections/sections-type';
import { SubmissionService } from '../../submission.service';
import parseSectionErrors from '../../utils/parseSectionErrors';
/**
* This component represents the drop zone that provides to add files to the submission.
*/
@Component({
selector: 'ds-base-submission-upload-files',
templateUrl: './submission-upload-files.component.html',
imports: [
UploaderComponent,
NgIf,
],
standalone: true,
})
export class SubmissionUploadFilesComponent implements OnChanges, OnDestroy {
/**
* The collection id this submission belonging to
* @type {string}
*/
@Input() collectionId: string;
/**
* The submission id
* @type {string}
*/
@Input() submissionId: string;
/**
* The uploader configuration options
* @type {UploaderOptions}
*/
@Input() uploadFilesOptions: UploaderOptions;
/**
* A boolean representing if is possible to active drop zone over the document page
* @type {boolean}
*/
public enableDragOverDocument = true;
/**
* i18n message label
* @type {string}
*/
public dropOverDocumentMsg = 'submission.sections.upload.drop-message';
/**
* i18n message label
* @type {string}
*/
public dropMsg = 'submission.sections.upload.drop-message';
/**
* Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array}
*/
private subs: Subscription[] = [];
/**
* A boolean representing if upload functionality is enabled
* @type {boolean}
*/
private uploadEnabled: Observable<boolean> = observableOf(false);
/**
* Save submission before to upload a file
*/
public onBeforeUpload = () => {
const sub: Subscription = this.operationsService.jsonPatchByResourceType(
this.submissionService.getSubmissionObjectLinkName(),
this.submissionId,
'sections')
.subscribe();
this.subs.push(sub);
return sub;
};
/**
* Initialize instance variables
*
* @param {NotificationsService} notificationsService
* @param {SubmissionJsonPatchOperationsService} operationsService
* @param {SectionsService} sectionService
* @param {SubmissionService} submissionService
* @param {TranslateService} translate
*/
constructor(private notificationsService: NotificationsService,
private operationsService: SubmissionJsonPatchOperationsService,
private sectionService: SectionsService,
private submissionService: SubmissionService,
private translate: TranslateService) {
}
/**
* Check if upload functionality is enabled
*/
ngOnChanges() {
this.uploadEnabled = this.sectionService.isSectionTypeAvailable(this.submissionId, SectionsType.Upload);
}
/**
* Parse the submission object retrieved from REST after upload
*
* @param event
* The completed upload event, carrying the submission object retrieved from REST and the
* client-side name of the file that completed
*/
public onCompleteItem(event: UploaderCompleteEvent) {
const workspaceitem = event.response as WorkspaceItem;
const fileName = event.fileName;
// Checks if upload section is enabled so do upload
this.subs.push(
this.uploadEnabled
.pipe(first())
.subscribe((isUploadEnabled) => {
if (isUploadEnabled) {
const { sections } = workspaceitem;
const { errors } = workspaceitem;
const errorsList = parseSectionErrors(errors);
if (sections && isNotEmpty(sections)) {
Object.keys(sections)
.forEach((sectionId) => {
const sectionData = normalizeSectionData(sections[sectionId]);
const sectionErrors = errorsList[sectionId];
this.sectionService.isSectionType(this.submissionId, sectionId, SectionsType.Upload)
.pipe(take(1))
.subscribe((isUpload) => {
if (isUpload) {
// Look for errors on upload
if ((isEmpty(sectionErrors))) {
this.notificationsService.success(null, this.getNotificationContent('upload-successful', fileName));
} else {
this.notificationsService.error(null, this.getNotificationContent('upload-failed', fileName));
}
}
});
this.sectionService.updateSectionData(this.submissionId, sectionId, sectionData, sectionErrors, sectionErrors);
});
}
}
}),
);
}
/**
* Show error notification on upload fails
*
* @param error
* The upload error, carrying the file that failed to upload (when available)
*/
public onUploadError(error?: UploaderError) {
this.notificationsService.error(null, this.getNotificationContent('upload-failed', error?.item?.file?.name));
}
/**
* Build the translated notification content for an upload outcome, including the file name when
* available. Falls back to the generic (file-name-less) message when the file name is missing.
*
* @param suffix
* The i18n key suffix under `submission.sections.upload.` (e.g. `upload-successful`)
* @param fileName
* The name of the file the notification refers to, if known
*/
private getNotificationContent(suffix: string, fileName?: string): Observable<string> {
return isNotEmpty(fileName)
? this.translate.get(`submission.sections.upload.${suffix}-file`, { fileName })
: this.translate.get(`submission.sections.upload.${suffix}`);
}
/**
* Unsubscribe from all subscriptions
*/
ngOnDestroy() {
this.subs
.filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe());
}
}