11import _ from 'underscore'
22import { Component , OnInit , Inject , EventEmitter , Output , Input , OnChanges , SimpleChanges } from '@angular/core' ;
3+ import { MatSnackBar } from '@angular/material/snack-bar' ;
34import { Team , Event , LocalStorageService , AuthenticationConfigurationService , UserService } from '../../upgrade/ajs-upgraded-providers' ;
45import { AdminBreadcrumb } from '../admin-breadcrumb/admin-breadcrumb.model'
56import { Strategy } from '../admin-authentication/admin-settings.model' ;
67import { MatDialog } from '@angular/material/dialog' ;
78import { StateService } from '@uirouter/angular' ;
89import { AuthenticationDeleteComponent } from './admin-authentication-delete/admin-authentication-delete.component' ;
10+ import { AdminSettingsUnsavedComponent } from '../admin-settings/admin-settings-unsaved/admin-settings-unsaved.component' ;
11+ import { TransitionService } from '@uirouter/core' ;
12+ import { lastValueFrom } from 'rxjs' ;
913
1014@Component ( {
1115 selector : 'admin-authentication' ,
@@ -26,13 +30,17 @@ export class AdminAuthenticationComponent implements OnInit, OnChanges {
2630 teams : any [ ] = [ ] ;
2731 events : any [ ] = [ ] ;
2832
33+ isDirty : boolean = false ;
34+
2935 strategies : Strategy [ ] = [ ] ;
3036
3137 readonly hasAuthConfigEditPermission : boolean ;
3238
3339 constructor (
3440 private dialog : MatDialog ,
3541 private stateService : StateService ,
42+ private readonly snackBar : MatSnackBar ,
43+ private readonly transitionService : TransitionService ,
3644 @Inject ( Team )
3745 public team : any ,
3846 @Inject ( Event )
@@ -50,6 +58,7 @@ export class AdminAuthenticationComponent implements OnInit, OnChanges {
5058 const configsPromise = this . authenticationConfigurationService . getAllConfigurations ( { includeDisabled : true } ) ;
5159 const teamsPromise = this . team . query ( { state : 'all' , populate : false } ) . $promise ;
5260 const eventsPromise = this . event . query ( { state : 'all' , populate : false } ) . $promise ;
61+ this . transitionService . onExit ( { } , this . onUnsavedChanges , { bind : this } ) ;
5362
5463 Promise . all ( [ configsPromise , teamsPromise , eventsPromise ] ) . then ( result => {
5564 // Remove event teams
@@ -92,32 +101,57 @@ export class AdminAuthenticationComponent implements OnInit, OnChanges {
92101 }
93102 }
94103
95- private save ( ) : void {
104+ onAuthenticationSaved ( status : boolean ) : void {
105+ if ( status ) {
106+ this . snackBar . open ( 'Authentication successfully saved' , null , {
107+ duration : 2000 ,
108+ } ) ;
109+ } else {
110+ this . snackBar . open ( '1 or more authentications failed to save correctly' , null , {
111+ duration : 2000 ,
112+ } ) ;
113+ } ;
114+ this . isDirty = false ;
115+ }
116+
117+ onAuthenticationDeleted ( status : boolean ) : void {
118+ if ( status ) {
119+ this . snackBar . open ( 'Authentication successfully deleted' , null , {
120+ duration : 2000 ,
121+ } ) ;
122+ } else {
123+ this . snackBar . open ( 'Failed to delete authentication' , null , {
124+ duration : 2000 ,
125+ } ) ;
126+ } ;
127+ this . isDirty = false ;
128+ }
129+
130+ save ( ) : void {
131+ console . log ( 'Saving authentication configurations' ) ;
96132 const promises = [ ] ;
97133 this . strategies . forEach ( strategy => {
98134 if ( strategy . isDirty ) {
99135 promises . push ( this . authenticationConfigurationService . updateConfiguration ( strategy ) ) ;
100136 }
101137 } ) ;
102138
103- if ( promises . length > 0 ) {
104- Promise . all ( promises ) . then ( ( ) => {
105- return this . authenticationConfigurationService . getAllConfigurations ( { includeDisabled : true } ) ;
106- } ) . then ( strategies => {
107- this . processUnsortedStrategies ( strategies . data ) ;
108- this . saveComplete . emit ( true ) ;
109- } ) . catch ( err => {
110- console . log ( err ) ;
111- this . authenticationConfigurationService . getAllConfigurations ( { includeDisabled : true } ) . then ( ( newStrategies : { data : Strategy [ ] ; } ) => {
112- this . processUnsortedStrategies ( newStrategies . data ) ;
113- this . saveComplete . emit ( false ) ;
114- } ) . catch ( ( err2 : any ) => {
115- console . log ( err2 ) ;
116- this . saveComplete . emit ( false ) ;
117- } ) ;
139+ Promise . all ( promises ) . then ( ( ) => {
140+ return this . authenticationConfigurationService . getAllConfigurations ( { includeDisabled : true } ) ;
141+ } ) . then ( strategies => {
142+ this . processUnsortedStrategies ( strategies . data ) ;
143+ this . onAuthenticationSaved ( true ) ;
144+ } ) . catch ( err => {
145+ console . log ( err ) ;
146+ this . authenticationConfigurationService . getAllConfigurations ( { includeDisabled : true } ) . then ( ( newStrategies : { data : Strategy [ ] ; } ) => {
147+ this . processUnsortedStrategies ( newStrategies . data ) ;
148+ this . onAuthenticationSaved ( false ) ;
149+ } ) . catch ( ( err2 : any ) => {
150+ console . log ( err2 ) ;
151+ this . onAuthenticationSaved ( false ) ;
118152 } ) ;
119- }
120- this . onStrategyDirty ( false ) ;
153+ } ) ;
154+ this . isDirty = false ;
121155 }
122156
123157 deleteStrategy ( strategy : Strategy ) : void {
@@ -129,13 +163,13 @@ export class AdminAuthenticationComponent implements OnInit, OnChanges {
129163 if ( result === 'delete' ) {
130164 this . authenticationConfigurationService . getAllConfigurations ( ) . then ( configs => {
131165 this . processUnsortedStrategies ( configs . data ) ;
132- this . deleteComplete . emit ( true ) ;
166+ this . onAuthenticationDeleted ( true ) ;
133167 } ) . catch ( ( err : any ) => {
134168 console . error ( err ) ;
135- this . deleteComplete . emit ( false ) ;
136- } )
169+ this . onAuthenticationDeleted ( false ) ;
170+ } ) ;
137171 } else if ( result === 'error' ) {
138- this . deleteComplete . emit ( false ) ;
172+ this . onAuthenticationDeleted ( false ) ;
139173 }
140174 } ) ;
141175 }
@@ -144,12 +178,26 @@ export class AdminAuthenticationComponent implements OnInit, OnChanges {
144178 this . stateService . go ( 'admin.authenticationCreate' )
145179 }
146180
147- onStrategyDirty ( isDirty : boolean ) : void {
148- this . onDirty . emit ( isDirty ) ;
149- }
150-
151181 onAuthenticationToggled ( strategy : Strategy ) : void {
152182 strategy . isDirty = true ;
153- this . onStrategyDirty ( true )
183+ this . isDirty = true ;
184+ }
185+
186+ async onUnsavedChanges ( ) : Promise < boolean > {
187+ if ( this . isDirty ) {
188+ const ref = this . dialog . open ( AdminSettingsUnsavedComponent ) ;
189+
190+ const result_2 = await lastValueFrom ( ref . afterClosed ( ) ) ;
191+ let discard = true ;
192+ if ( result_2 ) {
193+ discard = result_2 . discard ;
194+ }
195+ if ( discard ) {
196+ this . isDirty = false ;
197+ }
198+ return await Promise . resolve ( discard ) ;
199+ }
200+
201+ return Promise . resolve ( true ) ;
154202 }
155203}
0 commit comments