Commits
Alan Orth authored and GitHub committed e9b94f8e028 Merge
1 1 | import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core'; |
2 2 | import { |
3 3 | DynamicFormControlModel, |
4 4 | DynamicFormGroupModel, |
5 5 | DynamicFormLayout, |
6 6 | DynamicInputModel |
7 7 | } from '@ng-dynamic-forms/core'; |
8 8 | import { UntypedFormGroup } from '@angular/forms'; |
9 9 | import { RegistryService } from '../../../../core/registry/registry.service'; |
10 10 | import { FormBuilderService } from '../../../../shared/form/builder/form-builder.service'; |
11 - | import { take } from 'rxjs/operators'; |
11 + | import { switchMap, take, tap } from 'rxjs/operators'; |
12 12 | import { TranslateService } from '@ngx-translate/core'; |
13 - | import { combineLatest } from 'rxjs'; |
13 + | import { Observable, combineLatest } from 'rxjs'; |
14 14 | import { MetadataSchema } from '../../../../core/metadata/metadata-schema.model'; |
15 15 | |
16 16 | @Component({ |
17 17 | selector: 'ds-metadata-schema-form', |
18 18 | templateUrl: './metadata-schema-form.component.html' |
19 19 | }) |
20 20 | /** |
21 21 | * A form used for creating and editing metadata schemas |
22 22 | */ |
23 23 | export class MetadataSchemaFormComponent implements OnInit, OnDestroy { |
140 140 | this.registryService.cancelEditMetadataSchema(); |
141 141 | } |
142 142 | |
143 143 | /** |
144 144 | * Submit the form |
145 145 | * When the schema has an id attached -> Edit the schema |
146 146 | * When the schema has no id attached -> Create new schema |
147 147 | * Emit the updated/created schema using the EventEmitter submitForm |
148 148 | */ |
149 149 | onSubmit(): void { |
150 - | this.registryService.clearMetadataSchemaRequests().subscribe(); |
151 - | this.registryService.getActiveMetadataSchema().pipe(take(1)).subscribe( |
152 - | (schema: MetadataSchema) => { |
153 - | const values = { |
154 - | prefix: this.name.value, |
155 - | namespace: this.namespace.value |
156 - | }; |
157 - | if (schema == null) { |
158 - | this.registryService.createOrUpdateMetadataSchema(Object.assign(new MetadataSchema(), values)).subscribe((newSchema) => { |
159 - | this.submitForm.emit(newSchema); |
160 - | }); |
161 - | } else { |
162 - | this.registryService.createOrUpdateMetadataSchema(Object.assign(new MetadataSchema(), schema, { |
163 - | id: schema.id, |
164 - | prefix: schema.prefix, |
165 - | namespace: values.namespace, |
166 - | })).subscribe((updatedSchema: MetadataSchema) => { |
167 - | this.submitForm.emit(updatedSchema); |
150 + | this.registryService |
151 + | .getActiveMetadataSchema() |
152 + | .pipe( |
153 + | take(1), |
154 + | switchMap((schema: MetadataSchema) => { |
155 + | const metadataValues = { |
156 + | prefix: this.name.value, |
157 + | namespace: this.namespace.value, |
158 + | }; |
159 + | |
160 + | let createOrUpdate$: Observable<MetadataSchema>; |
161 + | |
162 + | if (schema == null) { |
163 + | createOrUpdate$ = |
164 + | this.registryService.createOrUpdateMetadataSchema( |
165 + | Object.assign(new MetadataSchema(), metadataValues) |
166 + | ); |
167 + | } else { |
168 + | const updatedSchema = Object.assign( |
169 + | new MetadataSchema(), |
170 + | schema, |
171 + | { |
172 + | namespace: metadataValues.namespace, |
173 + | } |
174 + | ); |
175 + | createOrUpdate$ = |
176 + | this.registryService.createOrUpdateMetadataSchema( |
177 + | updatedSchema |
178 + | ); |
179 + | } |
180 + | |
181 + | return createOrUpdate$; |
182 + | }), |
183 + | tap(() => { |
184 + | this.registryService.clearMetadataSchemaRequests().subscribe(); |
185 + | }) |
186 + | ) |
187 + | .subscribe((updatedOrCreatedSchema: MetadataSchema) => { |
188 + | this.submitForm.emit(updatedOrCreatedSchema); |
189 + | this.clearFields(); |
190 + | this.registryService.cancelEditMetadataSchema(); |
168 191 | }); |
169 - | } |
170 - | this.clearFields(); |
171 - | this.registryService.cancelEditMetadataSchema(); |
172 - | } |
173 - | ); |
174 192 | } |
175 193 | |
176 194 | /** |
177 195 | * Reset all input-fields to be empty |
178 196 | */ |
179 197 | clearFields(): void { |
180 198 | this.formGroup.reset('metadatadataschemagroup'); |
181 199 | this.name.disabled = false; |
182 200 | } |
183 201 | |