80 lines
1.8 KiB
TypeScript
80 lines
1.8 KiB
TypeScript
/**
|
|
* Signup ViewModel
|
|
*
|
|
* Client-side state management for signup flow.
|
|
* Immutable, class-based, contains only UI state.
|
|
*/
|
|
|
|
export interface SignupFormField {
|
|
value: string;
|
|
error?: string;
|
|
touched: boolean;
|
|
validating: boolean;
|
|
}
|
|
|
|
export interface SignupFormState {
|
|
fields: {
|
|
firstName: SignupFormField;
|
|
lastName: SignupFormField;
|
|
email: SignupFormField;
|
|
password: SignupFormField;
|
|
confirmPassword: SignupFormField;
|
|
};
|
|
isValid: boolean;
|
|
isSubmitting: boolean;
|
|
submitError?: string;
|
|
submitCount: number;
|
|
}
|
|
|
|
export interface SignupUIState {
|
|
showPassword: boolean;
|
|
showConfirmPassword: boolean;
|
|
}
|
|
|
|
export class SignupViewModel {
|
|
constructor(
|
|
public readonly returnTo: string,
|
|
public readonly formState: SignupFormState,
|
|
public readonly uiState: SignupUIState,
|
|
public readonly mutationPending: boolean = false,
|
|
public readonly mutationError: string | null = null
|
|
) {}
|
|
|
|
withFormState(formState: SignupFormState): SignupViewModel {
|
|
return new SignupViewModel(
|
|
this.returnTo,
|
|
formState,
|
|
this.uiState,
|
|
this.mutationPending,
|
|
this.mutationError
|
|
);
|
|
}
|
|
|
|
withUIState(uiState: SignupUIState): SignupViewModel {
|
|
return new SignupViewModel(
|
|
this.returnTo,
|
|
this.formState,
|
|
uiState,
|
|
this.mutationPending,
|
|
this.mutationError
|
|
);
|
|
}
|
|
|
|
withMutationState(pending: boolean, error: string | null): SignupViewModel {
|
|
return new SignupViewModel(
|
|
this.returnTo,
|
|
this.formState,
|
|
this.uiState,
|
|
pending,
|
|
error
|
|
);
|
|
}
|
|
|
|
get isSubmitting(): boolean {
|
|
return this.formState.isSubmitting || this.mutationPending;
|
|
}
|
|
|
|
get submitError(): string | undefined {
|
|
return this.formState.submitError || this.mutationError || undefined;
|
|
}
|
|
} |