Commits
Alan Orth authored and GitHub committed 727df563b85 Merge
1 - | import { Component, OnInit } from '@angular/core'; |
1 + | import { Component, OnInit, OnDestroy } from '@angular/core'; |
2 2 | import { RegistryService } from '../../../core/registry/registry.service'; |
3 - | import { ActivatedRoute, Router } from '@angular/router'; |
3 + | import { ActivatedRoute } from '@angular/router'; |
4 4 | import { |
5 5 | BehaviorSubject, |
6 6 | combineLatest as observableCombineLatest, |
7 7 | combineLatest, |
8 8 | Observable, |
9 9 | of as observableOf, |
10 10 | zip |
11 11 | } from 'rxjs'; |
12 12 | import { RemoteData } from '../../../core/data/remote-data'; |
13 13 | import { PaginatedList } from '../../../core/data/paginated-list.model'; |
25 25 | |
26 26 | @Component({ |
27 27 | selector: 'ds-metadata-schema', |
28 28 | templateUrl: './metadata-schema.component.html', |
29 29 | styleUrls: ['./metadata-schema.component.scss'] |
30 30 | }) |
31 31 | /** |
32 32 | * A component used for managing all existing metadata fields within the current metadata schema. |
33 33 | * The admin can create, edit or delete metadata fields here. |
34 34 | */ |
35 - | export class MetadataSchemaComponent implements OnInit { |
35 + | export class MetadataSchemaComponent implements OnInit, OnDestroy { |
36 36 | /** |
37 37 | * The metadata schema |
38 38 | */ |
39 39 | metadataSchema$: Observable<MetadataSchema>; |
40 40 | |
41 41 | /** |
42 42 | * A list of all the fields attached to this metadata schema |
43 43 | */ |
44 44 | metadataFields$: Observable<RemoteData<PaginatedList<MetadataField>>>; |
45 45 | |
53 53 | }); |
54 54 | |
55 55 | /** |
56 56 | * Whether or not the list of MetadataFields needs an update |
57 57 | */ |
58 58 | needsUpdate$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true); |
59 59 | |
60 60 | constructor(private registryService: RegistryService, |
61 61 | private route: ActivatedRoute, |
62 62 | private notificationsService: NotificationsService, |
63 - | private router: Router, |
64 63 | private paginationService: PaginationService, |
65 64 | private translateService: TranslateService) { |
66 65 | |
67 66 | } |
68 67 | |
69 68 | ngOnInit(): void { |
70 69 | this.route.params.subscribe((params) => { |
71 70 | this.initialize(params); |
72 71 | }); |
73 72 | } |
79 78 | initialize(params) { |
80 79 | this.metadataSchema$ = this.registryService.getMetadataSchemaByPrefix(params.schemaName).pipe(getFirstSucceededRemoteDataPayload()); |
81 80 | this.updateFields(); |
82 81 | } |
83 82 | |
84 83 | /** |
85 84 | * Update the list of fields by fetching it from the rest api or cache |
86 85 | */ |
87 86 | private updateFields() { |
88 87 | this.metadataFields$ = this.paginationService.getCurrentPagination(this.config.id, this.config).pipe( |
89 - | switchMap((currentPagination) => combineLatest(this.metadataSchema$, this.needsUpdate$, observableOf(currentPagination))), |
88 + | switchMap((currentPagination) => combineLatest([this.metadataSchema$, this.needsUpdate$, observableOf(currentPagination)])), |
90 89 | switchMap(([schema, update, currentPagination]: [MetadataSchema, boolean, PaginationComponentOptions]) => { |
91 90 | if (update) { |
92 91 | this.needsUpdate$.next(false); |
93 92 | } |
94 93 | return this.registryService.getMetadataFieldsBySchema(schema, toFindListOptions(currentPagination), !update, true); |
95 94 | }) |
96 95 | ); |
97 96 | } |
98 97 | |
99 98 | /** |
186 185 | } |
187 186 | |
188 187 | /** |
189 188 | * Show notifications for an amount of deleted metadata fields |
190 189 | * @param success Whether or not the notification should be a success message (error message when false) |
191 190 | * @param amount The amount of deleted metadata fields |
192 191 | */ |
193 192 | showNotification(success: boolean, amount: number) { |
194 193 | const prefix = 'admin.registries.schema.notification'; |
195 194 | const suffix = success ? 'success' : 'failure'; |
196 - | const messages = observableCombineLatest( |
195 + | const messages = observableCombineLatest([ |
197 196 | this.translateService.get(success ? `${prefix}.${suffix}` : `${prefix}.${suffix}`), |
198 197 | this.translateService.get(`${prefix}.field.deleted.${suffix}`, { amount: amount }) |
199 - | ); |
198 + | ]); |
200 199 | messages.subscribe(([head, content]) => { |
201 200 | if (success) { |
202 201 | this.notificationsService.success(head, content); |
203 202 | } else { |
204 203 | this.notificationsService.error(head, content); |
205 204 | } |
206 205 | }); |
207 206 | } |
208 207 | ngOnDestroy(): void { |
209 208 | this.paginationService.clearPagination(this.config.id); |
209 + | this.registryService.deselectAllMetadataField(); |
210 210 | } |
211 211 | |
212 212 | } |