website refactor
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Reset Password Client Component
|
||||
*
|
||||
*
|
||||
* Handles client-side reset password flow.
|
||||
*/
|
||||
|
||||
@@ -13,6 +13,7 @@ import { ResetPasswordTemplate } from '@/templates/auth/ResetPasswordTemplate';
|
||||
import { ResetPasswordMutation } from '@/lib/mutations/auth/ResetPasswordMutation';
|
||||
import { ResetPasswordViewModelBuilder } from '@/lib/builders/view-models/ResetPasswordViewModelBuilder';
|
||||
import { ResetPasswordViewModel } from '@/lib/view-models/auth/ResetPasswordViewModel';
|
||||
import { ResetPasswordFormValidation } from '@/lib/utilities/authValidation';
|
||||
import { routes } from '@/lib/routing/RouteConfig';
|
||||
|
||||
interface ResetPasswordClientProps {
|
||||
@@ -22,23 +23,63 @@ interface ResetPasswordClientProps {
|
||||
export function ResetPasswordClient({ viewData }: ResetPasswordClientProps) {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
|
||||
// Build ViewModel from ViewData
|
||||
const [viewModel, setViewModel] = useState<ResetPasswordViewModel>(() =>
|
||||
const [viewModel, setViewModel] = useState<ResetPasswordViewModel>(() =>
|
||||
ResetPasswordViewModelBuilder.build(viewData)
|
||||
);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
newPassword: '',
|
||||
confirmPassword: ''
|
||||
});
|
||||
// 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]: {
|
||||
...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();
|
||||
|
||||
// Validate passwords match
|
||||
if (formData.newPassword !== formData.confirmPassword) {
|
||||
setViewModel(prev => prev.withMutationState(false, 'Passwords do not match'));
|
||||
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],
|
||||
error: error.message,
|
||||
touched: true,
|
||||
},
|
||||
}), {}),
|
||||
},
|
||||
};
|
||||
return prev.withFormState(newFormState);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -68,7 +109,7 @@ export function ResetPasswordClient({ viewData }: ResetPasswordClientProps) {
|
||||
// Success
|
||||
const data = result.unwrap();
|
||||
setViewModel(prev => prev.withSuccess(data.message));
|
||||
|
||||
|
||||
// Redirect to login after a delay
|
||||
setTimeout(() => {
|
||||
router.push(routes.auth.login);
|
||||
@@ -116,7 +157,7 @@ export function ResetPasswordClient({ viewData }: ResetPasswordClientProps) {
|
||||
submitError={templateViewData.submitError}
|
||||
// Add the additional props
|
||||
formActions={{
|
||||
setFormData,
|
||||
handleChange,
|
||||
handleSubmit,
|
||||
setShowSuccess: (show) => {
|
||||
if (!show) {
|
||||
|
||||
Reference in New Issue
Block a user