1- import { Component } from '@angular/core' ;
1+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
22import { RegistryService } from '../../../core/registry/registry.service' ;
3- import { BehaviorSubject , combineLatest as observableCombineLatest , Observable , zip } from 'rxjs' ;
3+ import { BehaviorSubject , Observable , zip , Subscription } from 'rxjs' ;
44import { RemoteData } from '../../../core/data/remote-data' ;
55import { PaginatedList } from '../../../core/data/paginated-list.model' ;
66import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model' ;
77import { filter , map , switchMap , take } from 'rxjs/operators' ;
8- import { hasValue } from '../../../shared/empty.util' ;
98import { NotificationsService } from '../../../shared/notifications/notifications.service' ;
10- import { Router } from '@angular/router' ;
119import { TranslateService } from '@ngx-translate/core' ;
1210import { MetadataSchema } from '../../../core/metadata/metadata-schema.model' ;
1311import { toFindListOptions } from '../../../shared/pagination/pagination.utils' ;
@@ -24,13 +22,23 @@ import { PaginationService } from '../../../core/pagination/pagination.service';
2422 * A component used for managing all existing metadata schemas within the repository.
2523 * The admin can create, edit or delete metadata schemas here.
2624 */
27- export class MetadataRegistryComponent {
25+ export class MetadataRegistryComponent implements OnDestroy , OnInit {
2826
2927 /**
3028 * A list of all the current metadata schemas within the repository
3129 */
3230 metadataSchemas : Observable < RemoteData < PaginatedList < MetadataSchema > > > ;
3331
32+ /**
33+ * The {@link MetadataSchema}that is being edited
34+ */
35+ activeMetadataSchema$ : Observable < MetadataSchema > ;
36+
37+ /**
38+ * The selected {@link MetadataSchema} IDs
39+ */
40+ selectedMetadataSchemaIDs$ : Observable < number [ ] > ;
41+
3442 /**
3543 * Pagination config used to display the list of metadata schemas
3644 */
@@ -40,15 +48,25 @@ export class MetadataRegistryComponent {
4048 } ) ;
4149
4250 /**
43- * Whether or not the list of MetadataSchemas needs an update
51+ * Whether the list of MetadataSchemas needs an update
4452 */
4553 needsUpdate$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( true ) ;
4654
47- constructor ( private registryService : RegistryService ,
48- private notificationsService : NotificationsService ,
49- private router : Router ,
50- private paginationService : PaginationService ,
51- private translateService : TranslateService ) {
55+ subscriptions : Subscription [ ] = [ ] ;
56+
57+ constructor (
58+ protected registryService : RegistryService ,
59+ protected notificationsService : NotificationsService ,
60+ protected paginationService : PaginationService ,
61+ protected translateService : TranslateService ,
62+ ) {
63+ }
64+
65+ ngOnInit ( ) : void {
66+ this . activeMetadataSchema$ = this . registryService . getActiveMetadataSchema ( ) ;
67+ this . selectedMetadataSchemaIDs$ = this . registryService . getSelectedMetadataSchemas ( ) . pipe (
68+ map ( ( schemas : MetadataSchema [ ] ) => schemas . map ( ( schema : MetadataSchema ) => schema . id ) ) ,
69+ ) ;
5270 this . updateSchemas ( ) ;
5371 }
5472
@@ -77,30 +95,13 @@ export class MetadataRegistryComponent {
7795 * @param schema
7896 */
7997 editSchema ( schema : MetadataSchema ) {
80- this . getActiveSchema ( ) . pipe ( take ( 1 ) ) . subscribe ( ( activeSchema ) => {
98+ this . subscriptions . push ( this . activeMetadataSchema$ . pipe ( take ( 1 ) ) . subscribe ( ( activeSchema : MetadataSchema ) => {
8199 if ( schema === activeSchema ) {
82100 this . registryService . cancelEditMetadataSchema ( ) ;
83101 } else {
84102 this . registryService . editMetadataSchema ( schema ) ;
85103 }
86- } ) ;
87- }
88-
89- /**
90- * Checks whether the given metadata schema is active (being edited)
91- * @param schema
92- */
93- isActive ( schema : MetadataSchema ) : Observable < boolean > {
94- return this . getActiveSchema ( ) . pipe (
95- map ( ( activeSchema ) => schema === activeSchema )
96- ) ;
97- }
98-
99- /**
100- * Gets the active metadata schema (being edited)
101- */
102- getActiveSchema ( ) : Observable < MetadataSchema > {
103- return this . registryService . getActiveMetadataSchema ( ) ;
104+ } ) ) ;
104105 }
105106
106107 /**
@@ -114,42 +115,25 @@ export class MetadataRegistryComponent {
114115 this . registryService . deselectMetadataSchema ( schema ) ;
115116 }
116117
117- /**
118- * Checks whether a given metadata schema is selected in the list (checkbox)
119- * @param schema
120- */
121- isSelected ( schema : MetadataSchema ) : Observable < boolean > {
122- return this . registryService . getSelectedMetadataSchemas ( ) . pipe (
123- map ( ( schemas ) => schemas . find ( ( selectedSchema ) => selectedSchema === schema ) != null )
124- ) ;
125- }
126-
127118 /**
128119 * Delete all the selected metadata schemas
129120 */
130121 deleteSchemas ( ) {
131- this . registryService . getSelectedMetadataSchemas ( ) . pipe ( take ( 1 ) ) . subscribe (
132- ( schemas ) => {
133- const tasks$ = [ ] ;
134- for ( const schema of schemas ) {
135- if ( hasValue ( schema . id ) ) {
136- tasks$ . push ( this . registryService . deleteMetadataSchema ( schema . id ) . pipe ( getFirstCompletedRemoteData ( ) ) ) ;
137- }
138- }
139- zip ( ...tasks$ ) . subscribe ( ( responses : RemoteData < NoContent > [ ] ) => {
140- const successResponses = responses . filter ( ( response : RemoteData < NoContent > ) => response . hasSucceeded ) ;
141- const failedResponses = responses . filter ( ( response : RemoteData < NoContent > ) => response . hasFailed ) ;
142- if ( successResponses . length > 0 ) {
143- this . showNotification ( true , successResponses . length ) ;
144- }
145- if ( failedResponses . length > 0 ) {
146- this . showNotification ( false , failedResponses . length ) ;
147- }
148- this . registryService . deselectAllMetadataSchema ( ) ;
149- this . registryService . cancelEditMetadataSchema ( ) ;
150- } ) ;
122+ this . subscriptions . push ( this . selectedMetadataSchemaIDs$ . pipe (
123+ take ( 1 ) ,
124+ switchMap ( ( schemaIDs : number [ ] ) => zip ( schemaIDs . map ( ( schemaID : number ) => this . registryService . deleteMetadataSchema ( schemaID ) . pipe ( getFirstCompletedRemoteData ( ) ) ) ) ) ,
125+ ) . subscribe ( ( responses : RemoteData < NoContent > [ ] ) => {
126+ const successResponses : RemoteData < NoContent > [ ] = responses . filter ( ( response : RemoteData < NoContent > ) => response . hasSucceeded ) ;
127+ const failedResponses : RemoteData < NoContent > [ ] = responses . filter ( ( response : RemoteData < NoContent > ) => response . hasFailed ) ;
128+ if ( successResponses . length > 0 ) {
129+ this . showNotification ( true , successResponses . length ) ;
151130 }
152- ) ;
131+ if ( failedResponses . length > 0 ) {
132+ this . showNotification ( false , failedResponses . length ) ;
133+ }
134+ this . registryService . deselectAllMetadataSchema ( ) ;
135+ this . registryService . cancelEditMetadataSchema ( ) ;
136+ } ) ) ;
153137 }
154138
155139 /**
@@ -160,20 +144,20 @@ export class MetadataRegistryComponent {
160144 showNotification ( success : boolean , amount : number ) {
161145 const prefix = 'admin.registries.schema.notification' ;
162146 const suffix = success ? 'success' : 'failure' ;
163- const messages = observableCombineLatest (
164- this . translateService . get ( success ? `${ prefix } .${ suffix } ` : `${ prefix } .${ suffix } ` ) ,
165- this . translateService . get ( `${ prefix } .deleted.${ suffix } ` , { amount : amount } )
166- ) ;
167- messages . subscribe ( ( [ head , content ] ) => {
168- if ( success ) {
169- this . notificationsService . success ( head , content ) ;
170- } else {
171- this . notificationsService . error ( head , content ) ;
172- }
173- } ) ;
147+
148+ const head : string = this . translateService . instant ( success ? `${ prefix } .${ suffix } ` : `${ prefix } .${ suffix } ` ) ;
149+ const content : string = this . translateService . instant ( `${ prefix } .deleted.${ suffix } ` , { amount : amount } ) ;
150+
151+ if ( success ) {
152+ this . notificationsService . success ( head , content ) ;
153+ } else {
154+ this . notificationsService . error ( head , content ) ;
155+ }
174156 }
157+
175158 ngOnDestroy ( ) : void {
176159 this . paginationService . clearPagination ( this . config . id ) ;
160+ this . subscriptions . map ( ( subscription : Subscription ) => subscription . unsubscribe ( ) ) ;
177161 }
178162
179163}
0 commit comments