Some checks failed
CI / lint-typecheck (pull_request) Failing after 12s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
180 lines
5.4 KiB
TypeScript
180 lines
5.4 KiB
TypeScript
/**
|
|
* Reset Password Client Component
|
|
*
|
|
* Handles client-side reset password flow.
|
|
*/
|
|
|
|
'use client';
|
|
|
|
import { ClientWrapperProps } from '@/lib/contracts/components/ComponentContracts';
|
|
import { ResetPasswordMutation } from '@/lib/mutations/auth/ResetPasswordMutation';
|
|
import { routes } from '@/lib/routing/RouteConfig';
|
|
import { ResetPasswordFormValidation } from '@/lib/utilities/authValidation';
|
|
import { ResetPasswordViewData } from '@/lib/view-data/ResetPasswordViewData';
|
|
import { ResetPasswordViewModel } from '@/lib/view-models/auth/ResetPasswordViewModel';
|
|
import { ResetPasswordTemplate } from '@/templates/auth/ResetPasswordTemplate';
|
|
import { useRouter, useSearchParams } from 'next/navigation';
|
|
import { useState } from 'react';
|
|
|
|
export function ResetPasswordClient({ viewData }: ClientWrapperProps<ResetPasswordViewData>) {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
|
|
// Build ViewModel from ViewData
|
|
const [viewModel, setViewModel] = useState<ResetPasswordViewModel>(() =>
|
|
new ResetPasswordViewModel(
|
|
viewData.token,
|
|
viewData.returnTo,
|
|
viewData.formState,
|
|
{ showPassword: false, showConfirmPassword: false }
|
|
)
|
|
);
|
|
|
|
// Handle form field changes
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const { name, value } = e.target;
|
|
|
|
setViewModel(prev => {
|
|
const newFormState = {
|
|
...prev.formState,
|
|
fields: {
|
|
...prev.formState.fields,
|
|
[name as keyof typeof prev.formState.fields]: {
|
|
...prev.formState.fields[name as keyof typeof prev.formState.fields],
|
|
value,
|
|
touched: true,
|
|
error: undefined,
|
|
},
|
|
},
|
|
};
|
|
return prev.withFormState(newFormState);
|
|
});
|
|
};
|
|
|
|
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
|
|
const formData = {
|
|
newPassword: viewModel.formState.fields.newPassword.value as string,
|
|
confirmPassword: viewModel.formState.fields.confirmPassword.value as string,
|
|
};
|
|
|
|
// Validate form
|
|
const validationErrors = ResetPasswordFormValidation.validateForm(formData);
|
|
if (validationErrors.length > 0) {
|
|
setViewModel(prev => {
|
|
const newFormState = {
|
|
...prev.formState,
|
|
isValid: false,
|
|
submitCount: prev.formState.submitCount + 1,
|
|
fields: {
|
|
...prev.formState.fields,
|
|
...validationErrors.reduce((acc, error) => ({
|
|
...acc,
|
|
[error.field]: {
|
|
...prev.formState.fields[error.field as keyof typeof prev.formState.fields],
|
|
error: error.message,
|
|
touched: true,
|
|
},
|
|
}), {}),
|
|
},
|
|
};
|
|
return prev.withFormState(newFormState);
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Update submitting state
|
|
setViewModel(prev => prev.withMutationState(true, null));
|
|
|
|
try {
|
|
const token = searchParams.get('token');
|
|
if (!token) {
|
|
setViewModel(prev => prev.withMutationState(false, 'Invalid reset link'));
|
|
return;
|
|
}
|
|
|
|
// Execute reset password mutation
|
|
const mutation = new ResetPasswordMutation();
|
|
const result = await mutation.execute({
|
|
token,
|
|
newPassword: formData.newPassword,
|
|
});
|
|
|
|
if (result.isErr()) {
|
|
const error = result.getError();
|
|
setViewModel(prev => prev.withMutationState(false, error));
|
|
return;
|
|
}
|
|
|
|
// Success
|
|
const data = result.unwrap();
|
|
setViewModel(prev => prev.withSuccess(data.message));
|
|
|
|
// Redirect to login after a delay
|
|
setTimeout(() => {
|
|
router.push(routes.auth.login);
|
|
}, 3000);
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Failed to reset password';
|
|
setViewModel(prev => prev.withMutationState(false, errorMessage));
|
|
}
|
|
};
|
|
|
|
// Toggle password visibility
|
|
const togglePassword = () => {
|
|
setViewModel(prev => prev.withUIState({
|
|
...prev.uiState,
|
|
showPassword: !prev.uiState.showPassword,
|
|
}));
|
|
};
|
|
|
|
const toggleConfirmPassword = () => {
|
|
setViewModel(prev => prev.withUIState({
|
|
...prev.uiState,
|
|
showConfirmPassword: !prev.uiState.showConfirmPassword,
|
|
}));
|
|
};
|
|
|
|
// Build viewData for template
|
|
const templateViewData: ResetPasswordViewData = {
|
|
...viewData,
|
|
showSuccess: viewModel.showSuccess,
|
|
successMessage: viewModel.successMessage || undefined,
|
|
formState: viewModel.formState,
|
|
isSubmitting: viewModel.isSubmitting,
|
|
submitError: viewModel.submitError,
|
|
};
|
|
|
|
return (
|
|
<ResetPasswordTemplate
|
|
viewData={templateViewData}
|
|
formActions={{
|
|
handleChange,
|
|
handleSubmit,
|
|
setShowSuccess: (show) => {
|
|
if (!show) {
|
|
// Reset to initial state
|
|
setViewModel(() => new ResetPasswordViewModel(
|
|
viewData.token,
|
|
viewData.returnTo,
|
|
viewData.formState,
|
|
{ showPassword: false, showConfirmPassword: false }
|
|
));
|
|
}
|
|
},
|
|
setShowPassword: togglePassword,
|
|
setShowConfirmPassword: toggleConfirmPassword,
|
|
}}
|
|
uiState={{
|
|
showPassword: viewModel.uiState.showPassword,
|
|
showConfirmPassword: viewModel.uiState.showConfirmPassword,
|
|
}}
|
|
mutationState={{
|
|
isPending: viewModel.mutationPending,
|
|
error: viewModel.mutationError,
|
|
}}
|
|
/>
|
|
);
|
|
}
|