|
| 1 | +import { createDispatchMap, select } from '@ngxs/store'; |
| 2 | + |
| 3 | +import { TranslatePipe } from '@ngx-translate/core'; |
| 4 | + |
| 5 | +import { Button } from 'primeng/button'; |
| 6 | +import { Tooltip } from 'primeng/tooltip'; |
| 7 | + |
| 8 | +import { finalize } from 'rxjs'; |
| 9 | + |
| 10 | +import { NgOptimizedImage } from '@angular/common'; |
| 11 | +import { ChangeDetectionStrategy, Component, computed, inject, OnInit } from '@angular/core'; |
| 12 | + |
| 13 | +import { ENVIRONMENT } from '@core/provider/environment.provider'; |
| 14 | +import { AuthService } from '@core/services/auth.service'; |
| 15 | +import { ExternalIdentityStatus } from '@osf/shared/enums/external-identity-status.enum'; |
| 16 | +import { CustomConfirmationService } from '@osf/shared/services/custom-confirmation.service'; |
| 17 | +import { LoaderService } from '@osf/shared/services/loader.service'; |
| 18 | +import { ToastService } from '@osf/shared/services/toast.service'; |
| 19 | + |
| 20 | +import { |
| 21 | + AccountSettingsSelectors, |
| 22 | + DeleteExternalIdentity, |
| 23 | + GetExternalIdentities, |
| 24 | +} from '../../../account-settings/store'; |
| 25 | +import { ProfileSettingsTabOption } from '../../enums'; |
| 26 | + |
| 27 | +@Component({ |
| 28 | + selector: 'osf-authenticated-identity', |
| 29 | + imports: [NgOptimizedImage, Button, Tooltip, TranslatePipe], |
| 30 | + templateUrl: './authenticated-identity.component.html', |
| 31 | + styleUrl: './authenticated-identity.component.scss', |
| 32 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 33 | +}) |
| 34 | +export class AuthenticatedIdentityComponent implements OnInit { |
| 35 | + private readonly authService = inject(AuthService); |
| 36 | + private readonly environment = inject(ENVIRONMENT); |
| 37 | + private readonly customConfirmationService = inject(CustomConfirmationService); |
| 38 | + private readonly toastService = inject(ToastService); |
| 39 | + private readonly loaderService = inject(LoaderService); |
| 40 | + |
| 41 | + private readonly ORCID_PROVIDER = 'ORCID'; |
| 42 | + |
| 43 | + ngOnInit() { |
| 44 | + this.actions.getExternalIdentities(); |
| 45 | + } |
| 46 | + |
| 47 | + readonly actions = createDispatchMap({ |
| 48 | + deleteExternalIdentity: DeleteExternalIdentity, |
| 49 | + getExternalIdentities: GetExternalIdentities, |
| 50 | + }); |
| 51 | + |
| 52 | + readonly externalIdentities = select(AccountSettingsSelectors.getExternalIdentities); |
| 53 | + |
| 54 | + readonly orcidUrl = computed(() => { |
| 55 | + return this.existingOrcid() ? `https://orcid.org/${this.existingOrcid()}` : null; |
| 56 | + }); |
| 57 | + |
| 58 | + readonly existingOrcid = computed( |
| 59 | + (): string | undefined => |
| 60 | + this.externalIdentities()?.find((i) => i.id === 'ORCID' && i.status === ExternalIdentityStatus.VERIFIED) |
| 61 | + ?.externalId |
| 62 | + ); |
| 63 | + |
| 64 | + disconnectOrcid(): void { |
| 65 | + this.customConfirmationService.confirmDelete({ |
| 66 | + headerKey: 'settings.accountSettings.connectedIdentities.deleteDialog.header', |
| 67 | + messageParams: { name: this.ORCID_PROVIDER }, |
| 68 | + messageKey: 'settings.accountSettings.connectedIdentities.deleteDialog.message', |
| 69 | + onConfirm: () => { |
| 70 | + this.loaderService.show(); |
| 71 | + this.actions |
| 72 | + .deleteExternalIdentity(this.ORCID_PROVIDER) |
| 73 | + .pipe(finalize(() => this.loaderService.hide())) |
| 74 | + .subscribe(() => this.toastService.showSuccess('settings.accountSettings.connectedIdentities.successDelete')); |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + connectOrcid(): void { |
| 80 | + const webUrl = this.environment.webUrl; |
| 81 | + const casUrl = this.environment.casUrl; |
| 82 | + const finalDestination = new URL(`${webUrl}/settings/profile`); |
| 83 | + finalDestination.searchParams.set('tab', ProfileSettingsTabOption.Social.toString()); |
| 84 | + const casLoginUrl = new URL(`${casUrl}/login`); |
| 85 | + casLoginUrl.search = new URLSearchParams({ |
| 86 | + redirectOrcid: 'true', |
| 87 | + service: `${webUrl}/login`, |
| 88 | + next: encodeURIComponent(finalDestination.toString()), |
| 89 | + }).toString(); |
| 90 | + this.authService.logout(casLoginUrl.toString()); |
| 91 | + } |
| 92 | +} |
0 commit comments