1
- import { Component , OnInit } from '@angular/core' ;
1
+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
2
+ import { ActivatedRoute , Router } from '@angular/router' ;
2
3
import {
3
4
Attribute ,
4
5
AttributesManagerService ,
@@ -10,42 +11,109 @@ import {
10
11
UsersManagerService ,
11
12
Vo ,
12
13
} from '@perun-web-apps/perun/openapi' ;
13
- import { StoreService } from '@perun-web-apps/perun/services' ;
14
+ import { StoreService , NotificatorService } from '@perun-web-apps/perun/services' ;
15
+ import { TranslateService } from '@ngx-translate/core' ;
14
16
15
17
@Component ( {
16
18
selector : 'perun-web-apps-settings-mailing-lists' ,
17
19
templateUrl : './settings-mailing-lists.component.html' ,
18
20
styleUrls : [ './settings-mailing-lists.component.scss' ] ,
19
21
} )
20
- export class SettingsMailingListsComponent implements OnInit {
22
+ export class SettingsMailingListsComponent implements OnInit , OnDestroy {
21
23
user : User ;
22
24
vos : Vo [ ] = [ ] ;
23
25
resources : RichResource [ ] = [ ] ;
24
- mailingLists : string [ ] = [ ] ;
25
26
optOuts : InputSetMemberResourceAttribute [ ] = [ ] ;
26
27
optOutAttribute : Attribute ;
27
28
index : number ;
28
29
filteredVos : Vo [ ] = [ ] ;
29
- loading : boolean ;
30
+ loading = true ;
31
+ selectedVo : string = null ;
32
+ selectedResource : string = null ;
33
+ changeOptOut : string ;
30
34
31
35
constructor (
32
36
private store : StoreService ,
33
37
private usersManagerService : UsersManagerService ,
34
38
private membersService : MembersManagerService ,
35
39
private resourcesManagerService : ResourcesManagerService ,
36
- private attributesManagerService : AttributesManagerService
40
+ private attributesManagerService : AttributesManagerService ,
41
+ private route : ActivatedRoute ,
42
+ private router : Router ,
43
+ private notificator : NotificatorService ,
44
+ private translate : TranslateService
37
45
) { }
38
46
47
+ ngOnDestroy ( ) : void {
48
+ void this . router . navigate ( [ ] , {
49
+ relativeTo : this . route ,
50
+ queryParams : { vo : null , resource : null } ,
51
+ replaceUrl : true ,
52
+ } ) ;
53
+ }
54
+
39
55
ngOnInit ( ) : void {
40
- this . user = this . store . getPerunPrincipal ( ) . user ;
56
+ this . route . queryParams
57
+ . subscribe ( ( params ) => {
58
+ this . selectedVo = params [ 'vo' ] as string ;
59
+ this . selectedResource = params [ 'resource' ] as string ;
60
+ this . changeOptOut = params [ 'action' ] as string ;
61
+
62
+ this . user = this . store . getPerunPrincipal ( ) . user ;
63
+
64
+ this . usersManagerService . getVosWhereUserIsMember ( this . user . id ) . subscribe ( ( vos ) => {
65
+ this . vos = vos ;
66
+ this . filteredVos = vos ;
67
+ if ( this . selectedResource !== undefined ) {
68
+ const vo = this . vos . find ( ( obj ) => obj . shortName === this . selectedVo ) ;
69
+ if ( vo ) {
70
+ this . getMailingLists ( vo ) ;
71
+ }
72
+ } else if ( this . selectedVo !== undefined ) {
73
+ const vo = this . vos . find ( ( obj ) => obj . shortName === this . selectedVo ) ;
74
+ if ( vo ) {
75
+ this . getMailingLists ( vo ) ;
76
+ this . changeSelectedVo ( vo ) ;
77
+ }
78
+ }
79
+ } ) ;
80
+ } )
81
+ . unsubscribe ( ) ;
82
+ }
83
+
84
+ changeSelectedResource ( resource : RichResource ) : void {
85
+ if ( this . selectedResource !== resource . name ) {
86
+ this . getOptOutAttribute ( resource ) ;
87
+ }
88
+ if ( this . changeOptOut ) {
89
+ if ( this . changeOptOut === 'subscribe' ) {
90
+ this . subscribe ( ) ;
91
+ } else if ( this . changeOptOut === 'unsubscribe' ) {
92
+ this . unsubscribe ( ) ;
93
+ }
94
+ this . changeOptOut = null ;
95
+ }
96
+ void this . router . navigate ( [ ] , {
97
+ relativeTo : this . route ,
98
+ queryParams : { vo : this . selectedVo , resource : this . selectedResource , action : null } ,
99
+ queryParamsHandling : 'merge' ,
100
+ } ) ;
101
+ }
41
102
42
- this . usersManagerService . getVosWhereUserIsMember ( this . user . id ) . subscribe ( ( vos ) => {
43
- this . vos = vos ;
44
- this . filteredVos = vos ;
103
+ changeSelectedVo ( vo : Vo ) : void {
104
+ if ( this . selectedVo !== vo . shortName ) {
105
+ this . getMailingLists ( vo ) ;
106
+ this . selectedResource = null ;
107
+ }
108
+ void this . router . navigate ( [ ] , {
109
+ relativeTo : this . route ,
110
+ queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
111
+ queryParamsHandling : 'merge' ,
45
112
} ) ;
46
113
}
47
114
48
115
getMailingLists ( vo : Vo ) : void {
116
+ this . selectedVo = vo . shortName ;
49
117
this . loading = true ;
50
118
this . resources = [ ] ;
51
119
this . membersService . getMemberByUser ( vo . id , this . user . id ) . subscribe ( ( member ) => {
@@ -80,6 +148,10 @@ export class SettingsMailingListsComponent implements OnInit {
80
148
attribute : attribute ,
81
149
} ) ;
82
150
this . resources . push ( resource ) ;
151
+ if ( this . selectedResource === resource . name ) {
152
+ this . getOptOutAttribute ( resource ) ;
153
+ this . changeSelectedResource ( resource ) ;
154
+ }
83
155
}
84
156
this . loading = count !== 0 ;
85
157
} ) ;
@@ -90,24 +162,75 @@ export class SettingsMailingListsComponent implements OnInit {
90
162
}
91
163
92
164
getOptOutAttribute ( resource : RichResource ) : void {
165
+ this . selectedResource = resource . name ;
93
166
this . index = this . resources . indexOf ( resource ) ;
94
167
this . optOutAttribute = this . optOuts [ this . index ] . attribute ;
95
168
}
96
169
170
+ unsubscribe ( ) : void {
171
+ const originalState = this . optOuts [ this . index ] . attribute . value ;
172
+ this . optOuts [ this . index ] . attribute . value = 'true' as unknown as object ;
173
+ this . attributesManagerService . setMemberResourceAttribute ( this . optOuts [ this . index ] ) . subscribe (
174
+ ( ) => {
175
+ this . notificator . showSuccess (
176
+ ( this . translate . instant ( 'OPT_OUT_MAILING_LISTS.UNSUBSCRIBED' ) as string ) +
177
+ this . selectedResource +
178
+ '.'
179
+ ) ;
180
+ } ,
181
+ ( ) => {
182
+ this . optOuts [ this . index ] . attribute . value = originalState ;
183
+ }
184
+ ) ;
185
+ }
186
+ subscribe ( ) : void {
187
+ const originalState = this . optOuts [ this . index ] . attribute . value ;
188
+ this . optOuts [ this . index ] . attribute . value = null ;
189
+ this . attributesManagerService . setMemberResourceAttribute ( this . optOuts [ this . index ] ) . subscribe (
190
+ ( ) => {
191
+ this . notificator . showSuccess (
192
+ ( this . translate . instant ( 'OPT_OUT_MAILING_LISTS.SUBSCRIBED' ) as string ) +
193
+ this . selectedResource +
194
+ '.'
195
+ ) ;
196
+ } ,
197
+ ( ) => {
198
+ this . optOuts [ this . index ] . attribute . value = originalState ;
199
+ }
200
+ ) ;
201
+ }
202
+
97
203
setOptOut ( ) : void {
98
- this . optOuts [ this . index ] . attribute . value = this . optOutAttribute . value
99
- ? null
100
- : ( 'true' as unknown as object ) ;
101
- this . attributesManagerService
102
- . setMemberResourceAttribute ( this . optOuts [ this . index ] )
103
- . subscribe ( ( ) => {
104
- // console.log('done');
105
- } ) ;
204
+ if ( this . optOutAttribute . value ) {
205
+ this . subscribe ( ) ;
206
+ } else {
207
+ this . unsubscribe ( ) ;
208
+ }
106
209
}
107
210
108
211
applyFilter ( filter : string ) : void {
109
212
this . filteredVos = this . vos . filter ( ( vo ) =>
110
213
vo . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
111
214
) ;
112
215
}
216
+
217
+ deselectVo ( ) : void {
218
+ this . loading = true ;
219
+ this . selectedVo = null ;
220
+ this . selectedResource = null ;
221
+ void this . router . navigate ( [ ] , {
222
+ relativeTo : this . route ,
223
+ queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
224
+ queryParamsHandling : 'merge' ,
225
+ } ) ;
226
+ }
227
+
228
+ deselectResource ( ) : void {
229
+ this . selectedResource = null ;
230
+ void this . router . navigate ( [ ] , {
231
+ relativeTo : this . route ,
232
+ queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
233
+ queryParamsHandling : 'merge' ,
234
+ } ) ;
235
+ }
113
236
}
0 commit comments