import { Component, computed, inject, signal } from '@angular/core';
import { RouterLink } from '@angular/router';
import { firstValueFrom } from 'rxjs';
import { AuthService, isValidEmail } from '@africanies/africanies-core';
import {
AlertComponent,
ButtonComponent,
TextInputComponent,
} from '@africanies/africanies-ui';
import { DemoSectionComponent } from '../../shared/demo-section.component';
import { PageHeaderComponent } from '../../shared/page-header.component';
import { playgroundErrorMessage } from '../../shared/playground-notify';
import { USECASE_ONBOARDING_FORGOT } from '../../snippets';
/**
* Product `/onboarding/forgot-password` — email only. The emailed link is not
* handled here (no token in the route).
*/
@Component({
selector: 'app-onboarding-forgot-password-page',
standalone: true,
imports: [
RouterLink,
AlertComponent,
ButtonComponent,
TextInputComponent,
PageHeaderComponent,
DemoSectionComponent,
],
template: `
@if (successMessage(); as ok) {
} @else {
}
`,
})
export class OnboardingForgotPasswordPage {
private readonly authApi = inject(AuthService);
protected readonly email = signal('');
protected readonly sending = signal(false);
protected readonly successMessage = signal(null);
protected readonly errorMessage = signal(null);
protected readonly routeCode = USECASE_ONBOARDING_FORGOT;
protected readonly canSubmit = computed(
() => isValidEmail(this.email()) && !this.sending(),
);
protected async forgot(): Promise {
if (!this.canSubmit()) {
return;
}
this.sending.set(true);
this.errorMessage.set(null);
try {
const res = await firstValueFrom(this.authApi.forgot(this.email()));
if (res.success) {
this.successMessage.set(
res.message?.trim() || 'We have emailed your reset password.',
);
return;
}
this.errorMessage.set(
res.message?.trim() || 'Could not send a reset email.',
);
} catch (err) {
this.errorMessage.set(playgroundErrorMessage(err));
} finally {
this.sending.set(false);
}
}
}