|
| 1 | +import { DialogRef } from "@angular/cdk/dialog"; |
| 2 | +import { Component } from "@angular/core"; |
| 3 | +import { |
| 4 | + AbstractControl, |
| 5 | + FormBuilder, |
| 6 | + FormControl, |
| 7 | + FormGroup, |
| 8 | + FormsModule, |
| 9 | + ReactiveFormsModule, |
| 10 | + ValidationErrors, |
| 11 | + Validators, |
| 12 | +} from "@angular/forms"; |
| 13 | +import { firstValueFrom, map } from "rxjs"; |
| 14 | + |
| 15 | +import { JslibModule } from "@bitwarden/angular/jslib.module"; |
| 16 | +import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; |
| 17 | +import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; |
| 18 | +import { ButtonModule, DialogModule, DialogService, FormFieldModule } from "@bitwarden/components"; |
| 19 | + |
| 20 | +interface RequestSponsorshipForm { |
| 21 | + sponsorshipEmail: FormControl<string | null>; |
| 22 | + sponsorshipNote: FormControl<string | null>; |
| 23 | +} |
| 24 | + |
| 25 | +export interface AddSponsorshipDialogResult { |
| 26 | + action: AddSponsorshipDialogAction; |
| 27 | + value: Partial<AddSponsorshipFormValue> | null; |
| 28 | +} |
| 29 | + |
| 30 | +interface AddSponsorshipFormValue { |
| 31 | + sponsorshipEmail: string; |
| 32 | + sponsorshipNote: string; |
| 33 | + status: string; |
| 34 | +} |
| 35 | + |
| 36 | +enum AddSponsorshipDialogAction { |
| 37 | + Saved = "saved", |
| 38 | + Canceled = "canceled", |
| 39 | +} |
| 40 | + |
| 41 | +@Component({ |
| 42 | + templateUrl: "add-sponsorship-dialog.component.html", |
| 43 | + standalone: true, |
| 44 | + imports: [ |
| 45 | + JslibModule, |
| 46 | + ButtonModule, |
| 47 | + DialogModule, |
| 48 | + FormsModule, |
| 49 | + ReactiveFormsModule, |
| 50 | + FormFieldModule, |
| 51 | + ], |
| 52 | +}) |
| 53 | +export class AddSponsorshipDialogComponent { |
| 54 | + sponsorshipForm: FormGroup<RequestSponsorshipForm>; |
| 55 | + loading = false; |
| 56 | + |
| 57 | + constructor( |
| 58 | + private dialogRef: DialogRef<AddSponsorshipDialogResult>, |
| 59 | + private formBuilder: FormBuilder, |
| 60 | + private accountService: AccountService, |
| 61 | + private i18nService: I18nService, |
| 62 | + ) { |
| 63 | + this.sponsorshipForm = this.formBuilder.group<RequestSponsorshipForm>({ |
| 64 | + sponsorshipEmail: new FormControl<string | null>("", { |
| 65 | + validators: [Validators.email, Validators.required], |
| 66 | + asyncValidators: [this.validateNotCurrentUserEmail.bind(this)], |
| 67 | + updateOn: "change", |
| 68 | + }), |
| 69 | + sponsorshipNote: new FormControl<string | null>("", {}), |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + static open(dialogService: DialogService): DialogRef<AddSponsorshipDialogResult> { |
| 74 | + return dialogService.open<AddSponsorshipDialogResult>(AddSponsorshipDialogComponent); |
| 75 | + } |
| 76 | + |
| 77 | + protected async save() { |
| 78 | + if (this.sponsorshipForm.invalid) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + this.loading = true; |
| 83 | + // TODO: This is a mockup implementation - needs to be updated with actual API integration |
| 84 | + await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate API call |
| 85 | + |
| 86 | + const formValue = this.sponsorshipForm.getRawValue(); |
| 87 | + const dialogValue: Partial<AddSponsorshipFormValue> = { |
| 88 | + status: "Sent", |
| 89 | + sponsorshipEmail: formValue.sponsorshipEmail ?? "", |
| 90 | + sponsorshipNote: formValue.sponsorshipNote ?? "", |
| 91 | + }; |
| 92 | + |
| 93 | + this.dialogRef.close({ |
| 94 | + action: AddSponsorshipDialogAction.Saved, |
| 95 | + value: dialogValue, |
| 96 | + }); |
| 97 | + |
| 98 | + this.loading = false; |
| 99 | + } |
| 100 | + |
| 101 | + protected close = () => { |
| 102 | + this.dialogRef.close({ action: AddSponsorshipDialogAction.Canceled, value: null }); |
| 103 | + }; |
| 104 | + |
| 105 | + get sponsorshipEmailControl() { |
| 106 | + return this.sponsorshipForm.controls.sponsorshipEmail; |
| 107 | + } |
| 108 | + |
| 109 | + get sponsorshipNoteControl() { |
| 110 | + return this.sponsorshipForm.controls.sponsorshipNote; |
| 111 | + } |
| 112 | + |
| 113 | + private async validateNotCurrentUserEmail( |
| 114 | + control: AbstractControl, |
| 115 | + ): Promise<ValidationErrors | null> { |
| 116 | + const value = control.value; |
| 117 | + if (!value) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + const currentUserEmail = await firstValueFrom( |
| 122 | + this.accountService.activeAccount$.pipe(map((a) => a?.email ?? "")), |
| 123 | + ); |
| 124 | + |
| 125 | + if (!currentUserEmail) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + if (value.toLowerCase() === currentUserEmail.toLowerCase()) { |
| 130 | + return { currentUserEmail: true }; |
| 131 | + } |
| 132 | + |
| 133 | + return null; |
| 134 | + } |
| 135 | +} |
0 commit comments