77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import { ViewModel } from "../../contracts/view-models/ViewModel";
|
|
import type { LoginFormState, LoginUIState } from "./LoginInterfaces";
|
|
|
|
/**
|
|
* Login ViewModel
|
|
*
|
|
* Client-side state management for login flow.
|
|
* Immutable, class-based, contains only UI state.
|
|
*/
|
|
export class LoginViewModel extends ViewModel {
|
|
constructor(
|
|
public readonly returnTo: string,
|
|
public readonly hasInsufficientPermissions: boolean,
|
|
public readonly formState: LoginFormState,
|
|
public readonly uiState: LoginUIState,
|
|
public readonly mutationPending: boolean = false,
|
|
public readonly mutationError: string | null = null
|
|
) {
|
|
super();
|
|
}
|
|
|
|
// Immutable updates
|
|
withFormState(formState: LoginFormState): LoginViewModel {
|
|
return new LoginViewModel(
|
|
this.returnTo,
|
|
this.hasInsufficientPermissions,
|
|
formState,
|
|
this.uiState,
|
|
this.mutationPending,
|
|
this.mutationError
|
|
);
|
|
}
|
|
|
|
withUIState(uiState: LoginUIState): LoginViewModel {
|
|
return new LoginViewModel(
|
|
this.returnTo,
|
|
this.hasInsufficientPermissions,
|
|
this.formState,
|
|
uiState,
|
|
this.mutationPending,
|
|
this.mutationError
|
|
);
|
|
}
|
|
|
|
withMutationState(pending: boolean, error: string | null): LoginViewModel {
|
|
return new LoginViewModel(
|
|
this.returnTo,
|
|
this.hasInsufficientPermissions,
|
|
this.formState,
|
|
this.uiState,
|
|
pending,
|
|
error
|
|
);
|
|
}
|
|
|
|
// Getters for template consumption
|
|
get showPassword(): boolean {
|
|
return this.uiState.showPassword;
|
|
}
|
|
|
|
get showErrorDetails(): boolean {
|
|
return this.uiState.showErrorDetails;
|
|
}
|
|
|
|
get isSubmitting(): boolean {
|
|
return this.formState.isSubmitting || this.mutationPending;
|
|
}
|
|
|
|
get submitError(): string | undefined {
|
|
return this.formState.submitError || this.mutationError || undefined;
|
|
}
|
|
|
|
get formFields() {
|
|
return this.formState.fields;
|
|
}
|
|
}
|