2022-10-24 08:35:49 +02:00
import { Component , Inject , OnInit } from '@angular/core' ;
import { MatDialog , MatDialogRef , MAT_DIALOG_DATA } from '@angular/material/dialog' ;
2022-10-26 12:50:01 +02:00
import { DepositConfigurationStatus } from '@app/core/common/enum/deposit-configuration-status' ;
import { DepositConfigurationModel } from '@app/core/model/deposit/deposit-configuration' ;
import { DmpOverviewModel } from '@app/core/model/dmp/dmp-overview' ;
2022-11-02 08:41:40 +01:00
import { DoiModel } from '@app/core/model/doi/doi' ;
2022-10-26 12:50:01 +02:00
import { DepositRepositoriesService } from '@app/core/services/deposit-repositories/deposit-repositories.service' ;
import { DmpService } from '@app/core/services/dmp/dmp.service' ;
import { SnackBarNotificationLevel , UiNotificationService } from '@app/core/services/notification/ui-notification-service' ;
import { Oauth2DialogService } from '@app/ui/misc/oauth2-dialog/service/oauth2-dialog.service' ;
2022-10-24 08:35:49 +02:00
import { BaseComponent } from '@common/base/base.component' ;
2022-10-26 12:50:01 +02:00
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component' ;
import { MultipleChoiceDialogComponent } from '@common/modules/multiple-choice-dialog/multiple-choice-dialog.component' ;
import { TranslateService } from '@ngx-translate/core' ;
import { takeUntil } from 'rxjs/operators' ;
2022-10-24 08:35:49 +02:00
@Component ( {
selector : 'app-dmp-deposit-dialog' ,
templateUrl : './dmp-deposit-dialog.component.html' ,
styleUrls : [ './dmp-deposit-dialog.component.scss' ]
} )
export class DmpDepositDialogComponent extends BaseComponent implements OnInit {
2022-10-26 12:50:01 +02:00
inputRepos : DepositConfigurationModel [ ] ;
2022-11-02 08:41:40 +01:00
outputRepos : DoiModel [ ] ;
2022-10-26 12:50:01 +02:00
dmp : DmpOverviewModel ;
2022-10-24 08:35:49 +02:00
constructor (
2022-10-26 12:50:01 +02:00
private dmpService : DmpService ,
private depositRepositoriesService : DepositRepositoriesService ,
2022-10-24 08:35:49 +02:00
public dialogRef : MatDialogRef < DmpDepositDialogComponent > ,
2022-10-26 12:50:01 +02:00
private dialog : MatDialog ,
private language : TranslateService ,
private uiNotificationService : UiNotificationService ,
private oauth2DialogService : Oauth2DialogService ,
2022-10-24 08:35:49 +02:00
@Inject ( MAT_DIALOG_DATA ) public data : any
) {
super ( ) ;
2022-10-26 12:50:01 +02:00
this . inputRepos = data [ 'depositRepos' ] [ 0 ] ;
this . dmp = data [ 'depositRepos' ] [ 1 ] ;
2022-11-02 08:41:40 +01:00
for ( var i = 0 ; i < this . dmp . dois . length ; i ++ ) {
this . inputRepos = this . inputRepos . filter ( r = > this . hasDoi ( r , this . dmp . dois , i ) ) ;
}
2022-10-24 08:35:49 +02:00
this . outputRepos = [ ] ;
}
2022-11-02 08:41:40 +01:00
hasDoi ( repo , dois , i ) {
return repo . repositoryId !== dois [ i ] . repositoryId ;
}
2022-10-24 08:35:49 +02:00
ngOnInit ( ) : void {
}
onSubmit() {
this . dialogRef . close ( this . outputRepos ) ;
}
close() {
this . dialogRef . close ( this . outputRepos ) ;
}
2022-10-26 12:50:01 +02:00
deposit ( repo : DepositConfigurationModel ) {
if ( repo . depositAccountStatus == DepositConfigurationStatus . BothSystemAndUser ) {
const dialogRef = this . dialog . open ( MultipleChoiceDialogComponent , {
maxWidth : '600px' ,
restoreFocus : false ,
data : {
message : 'Which account would you like to use?' , //this.language.instant('GENERAL.ERRORS.HTTP-REQUEST-ERROR'),
titles : [ 'Login with ' + repo . repositoryId /*this.language.instant('DMP-OVERVIEW.MULTIPLE-DIALOG.ZENODO-LOGIN')*/ , this . language . instant ( 'DMP-OVERVIEW.MULTIPLE-DIALOG.USE-DEFAULT' ) ]
}
} ) ;
dialogRef . afterClosed ( ) . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( result = > {
switch ( result ) {
case 0 :
this . showOauth2Dialog ( 'https://sandbox.zenodo.org/oauth/authorize' + '?client_id=' + repo . repositoryLoginClientId
+ '&response_type=code&scope=deposit:write+deposit:actions+user:email&state=astate&redirect_uri='
+ 'http://localhost:4200/login/external/zenodo' , repo , this . dmp ) ;
break ;
case 1 :
2022-10-27 08:32:26 +02:00
this . depositRepositoriesService . createDoi ( repo . repositoryId , this . dmp . id , null )
. pipe ( takeUntil ( this . _destroyed ) )
. subscribe ( doi = > {
this . onDOICallbackSuccess ( ) ;
this . outputRepos . push ( doi ) ;
2022-11-02 08:41:40 +01:00
this . inputRepos = this . inputRepos . filter ( function ( x ) { return x . repositoryId !== repo . repositoryId ; } ) ;
if ( this . inputRepos . length == 0 ) {
this . close ( ) ;
}
2022-10-27 08:32:26 +02:00
} , error = > this . onDeleteCallbackError ( error ) ) ;
2022-10-26 12:50:01 +02:00
break ;
}
} ) ;
}
}
onDOICallbackSuccess ( ) : void {
this . uiNotificationService . snackBarNotification ( this . language . instant ( 'DMP-EDITOR.SNACK-BAR.SUCCESSFUL-DOI' ) , SnackBarNotificationLevel . Success ) ;
}
onDeleteCallbackError ( error ) {
this . uiNotificationService . snackBarNotification ( error . error . message ? this . language . instant ( error . error . message ) : this . language . instant ( 'GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE' ) , SnackBarNotificationLevel . Error ) ;
}
showOauth2Dialog ( url : string , repo : DepositConfigurationModel , dmp : DmpOverviewModel ) {
this . oauth2DialogService . login ( url )
. pipe ( takeUntil ( this . _destroyed ) )
. subscribe ( result = > {
if ( result !== undefined ) {
if ( result . oauthCode !== undefined && result . oauthCode !== null ) {
this . depositRepositoriesService . getAccessToken ( repo . repositoryId , result . oauthCode )
. pipe ( takeUntil ( this . _destroyed ) )
. subscribe ( token = > {
this . depositRepositoriesService . createDoi ( repo . repositoryId , dmp . id , token )
. pipe ( takeUntil ( this . _destroyed ) )
. subscribe ( doi = > {
2022-10-27 08:32:26 +02:00
this . onDOICallbackSuccess ( ) ;
this . outputRepos . push ( doi ) ;
2022-11-02 08:41:40 +01:00
this . inputRepos = this . inputRepos . filter ( function ( x ) { return x . repositoryId !== repo . repositoryId ; } ) ;
if ( this . inputRepos . length == 0 ) {
this . close ( ) ;
}
2022-10-27 08:32:26 +02:00
} , error = > this . onDeleteCallbackError ( error ) ) ;
2022-10-26 12:50:01 +02:00
} ) ;
// this.userService.registerDOIToken(result.oauthCode, this.configurationService.app + 'oauth2')
// .pipe(takeUntil(this._destroyed))
// .subscribe(() => {
// this.hasDOIToken = true;
// this.showConfirmationDOIDialog(dmp);
// });
}
}
} ) ;
}
2022-10-24 08:35:49 +02:00
}