-
Notifications
You must be signed in to change notification settings - Fork 6
/
edit-resource-class-dialog.component.ts
126 lines (112 loc) · 4.05 KB
/
edit-resource-class-dialog.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
import { Component, Inject, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import {
DeleteResourceClassComment,
KnoraApiConnection,
ResourceClassDefinitionWithAllLanguages,
UpdateOntology,
UpdateResourceClassComment,
UpdateResourceClassLabel,
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from '@dasch-swiss/vre/shared/app-config';
import { switchMap, tap } from 'rxjs/operators';
export interface EditResourceClassDialogProps {
id: string;
title: string;
ontologyId: string;
lastModificationDate: string;
}
@Component({
selector: 'app-edit-resource-class-dialog',
template: `
<app-dialog-header [title]="data.title" subtitle="Customize resource class"></app-dialog-header>
<div mat-dialog-content>
<app-resource-class-form
[formData]="{ name: '', labels: [], comments: [] }"
(formValueChange)="form = $event"></app-resource-class-form>
</div>
<div mat-dialog-actions align="end">
<button mat-button mat-dialog-close>Cancel</button>
<button
mat-raised-button
color="primary"
appLoadingButton
[isLoading]="loading"
[disabled]="form.invalid"
(click)="onSubmit()">
Update
</button>
</div>
`,
})
export class EditResourceClassDialogComponent implements OnInit {
loading = false;
form: FormGroup;
lastModificationDate: string;
constructor(
@Inject(DspApiConnectionToken)
private _dspApiConnection: KnoraApiConnection,
@Inject(MAT_DIALOG_DATA)
public data: EditResourceClassDialogProps,
public dialogRef: MatDialogRef<EditResourceClassDialogComponent, boolean>
) {}
ngOnInit() {
this.dialogRef.updateSize('800px', ''); // Set your desired width
this.lastModificationDate = this.data.lastModificationDate;
}
onSubmit() {
this.loading = true;
// label
const onto4Label = new UpdateOntology<UpdateResourceClassLabel>();
onto4Label.id = this.data.ontologyId;
onto4Label.lastModificationDate = this.lastModificationDate;
const updateLabel = new UpdateResourceClassLabel();
updateLabel.id = this.data.id;
updateLabel.labels = this.form.value.labels;
onto4Label.entity = updateLabel;
// comment
const onto4Comment = new UpdateOntology<UpdateResourceClassComment>();
onto4Comment.id = this.data.ontologyId;
const updateComment = new UpdateResourceClassComment();
updateComment.id = this.data.id;
updateComment.comments = this.form.value.comments;
onto4Comment.entity = updateComment;
this._dspApiConnection.v2.onto
.updateResourceClass(onto4Label)
.pipe(
switchMap((classLabelResponse: ResourceClassDefinitionWithAllLanguages) => {
this.lastModificationDate = classLabelResponse.lastModificationDate;
onto4Comment.lastModificationDate = this.lastModificationDate;
if (updateComment.comments.length) {
return this._updateComment$(onto4Comment);
} else {
return this._deleteResourceComment$();
}
}),
tap(() => {
this.loading = false;
})
)
.subscribe(() => {
this.dialogRef.close(true);
});
}
private _deleteResourceComment$() {
const deleteResourceClassComment = new DeleteResourceClassComment();
deleteResourceClassComment.id = this.data.id;
deleteResourceClassComment.lastModificationDate = this.lastModificationDate;
return this._dspApiConnection.v2.onto.deleteResourceClassComment(deleteResourceClassComment).pipe(
tap((deleteCommentResponse: ResourceClassDefinitionWithAllLanguages) => {
this.lastModificationDate = deleteCommentResponse.lastModificationDate;
})
);
}
private _updateComment$(onto4Comment) {
return this._dspApiConnection.v2.onto.updateResourceClass(onto4Comment).pipe(
tap((classCommentResponse: ResourceClassDefinitionWithAllLanguages) => {
this.lastModificationDate = classCommentResponse.lastModificationDate;
})
);
}
}