Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 32x 32x 32x 26x 7x 31x 7x 1x 1x 6x 6x 6x 6x 5x 5x 1x 5x 1x 5x 1x 1x 4x 4x 1x 1x 6x 26x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 26x 1x 1x 1x 26x | import { useState, useCallback } from 'react';
import {
checkRegistrationAvailability,
requestRegistrationOtp,
verifyRegistration,
} from '@/services/authService';
import { AuthSessionResponse } from '@/models/auth';
import { ApiError } from '@/services/api';
import {
validateEmail,
validateOtp,
validateUsername,
validatePassword,
validatePhoneNumber,
validateBirthDate,
} from '@/utils/validators';
export type RegisterStep = 'details' | 'otp';
export type Gender = 'male' | 'female' | 'other' | 'prefer_not_to_say' | '';
export interface RegisterFormData {
email: string;
otp: string;
username: string;
password: string;
phone_number: string;
gender: Gender;
birth_date: string;
}
export interface RegisterFormErrors {
email?: string | null;
otp?: string | null;
username?: string | null;
password?: string | null;
phone_number?: string | null;
birth_date?: string | null;
}
export interface RegisterViewModel {
step: RegisterStep;
formData: RegisterFormData;
errors: RegisterFormErrors;
isLoading: boolean;
apiError: string | null;
updateField: <K extends keyof RegisterFormData>(
field: K,
value: RegisterFormData[K],
) => void;
handleSubmitDetails: () => Promise<void>;
handleVerifyOtp: () => Promise<AuthSessionResponse | null>;
goBack: () => void;
}
const INITIAL_FORM_DATA: RegisterFormData = {
email: '',
otp: '',
username: '',
password: '',
phone_number: '',
gender: '',
birth_date: '',
};
const OTP_ERROR_CODES = new Set(['invalid_otp', 'otp_attempts_exceeded']);
export function useRegisterViewModel(): RegisterViewModel {
const [step, setStep] = useState<RegisterStep>('details');
const [formData, setFormData] = useState<RegisterFormData>(INITIAL_FORM_DATA);
const [errors, setErrors] = useState<RegisterFormErrors>({});
const [isLoading, setIsLoading] = useState(false);
const [apiError, setApiError] = useState<string | null>(null);
const updateField = useCallback(
<K extends keyof RegisterFormData>(field: K, value: RegisterFormData[K]) => {
setFormData((prev) => ({ ...prev, [field]: value }));
setErrors((prev) => ({ ...prev, [field]: null }));
setApiError(null);
},
[],
);
const handleSubmitDetails = useCallback(async () => {
const newErrors: RegisterFormErrors = {
email: validateEmail(formData.email),
username: validateUsername(formData.username),
password: validatePassword(formData.password),
phone_number: validatePhoneNumber(formData.phone_number),
birth_date: validateBirthDate(formData.birth_date),
};
const hasErrors = Object.values(newErrors).some((e) => e != null);
if (hasErrors) {
setErrors(newErrors);
return;
}
setIsLoading(true);
setApiError(null);
try {
const availability = await checkRegistrationAvailability({
username: formData.username,
email: formData.email,
});
const taken: RegisterFormErrors = {};
if (availability.email === 'TAKEN') {
taken.email = 'This email is already in use.';
}
if (availability.username === 'TAKEN') {
taken.username = 'This username is already in use.';
}
if (Object.keys(taken).length > 0) {
setErrors((prev) => ({ ...prev, ...taken }));
return;
}
await requestRegistrationOtp({ email: formData.email });
setStep('otp');
} catch (err) {
if (err instanceof ApiError) {
setApiError(err.message);
} else E{
setApiError('An unexpected error occurred. Please try again.');
}
} finally {
setIsLoading(false);
}
}, [formData]);
const handleVerifyOtp = useCallback(async (): Promise<AuthSessionResponse | null> => {
const otpError = validateOtp(formData.otp);
Iif (otpError) {
setErrors((prev) => ({ ...prev, otp: otpError }));
return null;
}
setIsLoading(true);
setApiError(null);
try {
return await verifyRegistration({
email: formData.email,
otp: formData.otp,
username: formData.username,
password: formData.password,
phone_number: formData.phone_number || null,
gender: formData.gender || null,
birth_date: formData.birth_date || null,
});
} catch (err) {
if (err instanceof ApiError) {
Eif (OTP_ERROR_CODES.has(err.code)) {
setFormData((prev) => ({ ...prev, otp: '' }));
setErrors({ otp: err.message });
}
setApiError(err.message);
} else E{
setApiError('An unexpected error occurred. Please try again.');
}
return null;
} finally {
setIsLoading(false);
}
}, [formData]);
const goBack = useCallback(() => {
setApiError(null);
setErrors({});
Eif (step === 'otp') setStep('details');
}, [step]);
return {
step,
formData,
errors,
isLoading,
apiError,
updateField,
handleSubmitDetails,
handleVerifyOtp,
goBack,
};
}
|