-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdataset-file-uploader.component.ts
101 lines (98 loc) · 2.9 KB
/
dataset-file-uploader.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
import { Component, OnDestroy, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { Store } from "@ngrx/store";
import { Subscription } from "rxjs";
import {
PickedFile,
SubmitCaptionEvent,
} from "shared/modules/file-uploader/file-uploader.component";
import { Attachment, Dataset, User } from "shared/sdk";
import { OwnershipService } from "shared/services/ownership.service";
import {
addAttachmentAction,
removeAttachmentAction,
updateAttachmentCaptionAction,
} from "state-management/actions/datasets.actions";
import {
selectCurrentAttachments,
selectCurrentDataset,
} from "state-management/selectors/datasets.selectors";
import { selectCurrentUser } from "state-management/selectors/user.selectors";
@Component({
selector: "app-dataset-file-uploader",
templateUrl: "./dataset-file-uploader.component.html",
styleUrls: ["./dataset-file-uploader.component.scss"],
})
export class DatasetFileUploaderComponent implements OnInit, OnDestroy {
attachments: Attachment[] = [];
subscriptions: Subscription[] = [];
attachment: Partial<Attachment> = {};
dataset: Dataset | undefined;
user: User | undefined;
constructor(
private store: Store,
private ownershipService: OwnershipService,
private router: Router,
) {}
ngOnInit(): void {
this.subscriptions.push(
this.store.select(selectCurrentDataset).subscribe((dataset) => {
if (dataset) {
this.dataset = dataset;
this.ownershipService.checkDatasetAccess(
dataset,
this.store,
this.router,
);
}
}),
);
this.subscriptions.push(
this.store.select(selectCurrentUser).subscribe((user) => {
if (user) {
this.user = user;
}
}),
);
this.subscriptions.push(
this.store.select(selectCurrentAttachments).subscribe((attachments) => {
this.attachments = attachments;
}),
);
}
onFileUploaderFilePicked(file: PickedFile) {
if (this.dataset && this.user) {
this.attachment = {
thumbnail: file.content,
caption: file.name,
ownerGroup: this.dataset.ownerGroup,
accessGroups: this.dataset.accessGroups,
datasetId: this.dataset.pid,
};
this.store.dispatch(addAttachmentAction({ attachment: this.attachment }));
}
}
updateCaption(event: SubmitCaptionEvent) {
if (this.dataset) {
this.store.dispatch(
updateAttachmentCaptionAction({
datasetId: this.dataset.pid,
attachmentId: event.attachmentId,
caption: event.caption,
}),
);
}
}
deleteAttachment(attachmentId: string) {
if (this.dataset) {
this.store.dispatch(
removeAttachmentAction({ datasetId: this.dataset.pid, attachmentId }),
);
}
}
ngOnDestroy() {
this.subscriptions.forEach((subscription) => {
subscription.unsubscribe();
});
}
}