/
list-info-form.component.ts
173 lines (137 loc) · 4.93 KB
/
list-info-form.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
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
import { Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core';
import { FormGroup } from '@angular/forms';
import {
ApiResponseData,
ApiResponseError,
CreateListRequest,
KnoraApiConnection,
List,
ListInfoResponse,
ListNodeInfo,
ListResponse,
ReadProject,
StringLiteral,
UpdateListInfoRequest
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { ErrorHandlerService } from 'src/app/main/services/error-handler.service';
@Component({
selector: 'app-list-info-form',
templateUrl: './list-info-form.component.html',
styleUrls: ['./list-info-form.component.scss']
})
export class ListInfoFormComponent implements OnInit {
@Input() iri?: string;
@Input() mode: 'create' | 'update';
// project short code
@Input() projectCode: string;
@Input() projectIri: string;
@Output() closeDialog: EventEmitter<List | ListNodeInfo> = new EventEmitter<List>();
loading: boolean;
list: ListNodeInfo;
labels: StringLiteral[];
comments: StringLiteral[];
// possible errors for the label
labelErrors = {
label: {
'required': 'A label is required.'
},
comment: {
'required': 'A description is required.'
}
};
saveButtonDisabled = true;
labelInvalidMessage: string;
commentInvalidMessage: string;
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _errorHandler: ErrorHandlerService
) { }
ngOnInit() {
this.loading = true;
// get list info in case of edit mode
if (this.mode === 'update') {
// edit mode, get list
this._dspApiConnection.admin.listsEndpoint.getListInfo(this.iri).subscribe(
(response: ApiResponseData<ListInfoResponse>) => {
this.list = response.body.listinfo;
this.buildLists(response.body.listinfo);
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
}
);
} else {
// build the form
this.buildLists();
}
}
buildLists(list?: ListNodeInfo): void {
this.loading = true;
this.labels = [];
this.comments = [];
if (list && list.id) {
this.labels = list.labels;
this.comments = list.comments;
}
this.loading = false;
}
submitData(): void {
this.loading = true;
if (this.mode === 'update') {
// edit mode: update list info
const listInfoUpdateData: UpdateListInfoRequest = new UpdateListInfoRequest();
listInfoUpdateData.projectIri = this.projectIri;
listInfoUpdateData.listIri = this.iri;
listInfoUpdateData.labels = this.labels;
listInfoUpdateData.comments = this.comments;
this._dspApiConnection.admin.listsEndpoint.updateListInfo(listInfoUpdateData).subscribe(
(response: ApiResponseData<ListInfoResponse>) => {
this.loading = false;
this.closeDialog.emit(response.body.listinfo);
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
this.loading = false;
}
);
} else {
// new: create list
const listInfoData: CreateListRequest = new CreateListRequest();
listInfoData.projectIri = this.projectIri;
listInfoData.labels = this.labels;
listInfoData.comments = this.comments;
this._dspApiConnection.admin.listsEndpoint.createList(listInfoData).subscribe(
(response: ApiResponseData<ListResponse>) => {
this.closeDialog.emit(response.body.list);
this.loading = false;
},
(error: ApiResponseError) => {
this._errorHandler.showMessage(error);
this.loading = false;
}
);
}
}
/**
* reset the form
*/
resetLists(ev: Event, list?: ListNodeInfo) {
ev.preventDefault();
list = list ? list : new ListNodeInfo();
this.buildLists(list);
}
handleData(data: StringLiteral[], type: string) {
switch (type) {
case 'labels':
this.labels = data;
this.labelInvalidMessage = (data.length ? null : this.labelErrors.label.required);
break;
case 'comments':
this.comments = data;
this.commentInvalidMessage = (data.length ? null : this.labelErrors.comment.required);
break;
}
this.saveButtonDisabled = (!this.labels.length || !this.comments.length);
}
}