chore: add api and change code styles

This commit is contained in:
Koosha Lahouti
2025-08-13 14:13:57 +03:30
parent 378b4057e2
commit 881e6384d3
12 changed files with 264 additions and 120 deletions

View File

@@ -9,6 +9,9 @@ import { SubmitSection } from './SubmitSection';
import apiClient from '@/lib/apiClient';
import { useToast } from '@rkheftan/harmony-ui';
import { AxiosError } from 'axios';
import { regex } from '../../../utils/regex';
import { toLocaleDigits } from '../../../utils/persianDigit';
import i18n from '@/config/i18n';
export function UserCompletionForm() {
const { t } = useTranslation('completionForm');
@@ -37,13 +40,14 @@ export function UserCompletionForm() {
const correctEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const {
hasNumber,
hasMinLength,
hasUpperAndLower,
hasSpecialChar,
validPassword,
} = regex(password);
const matchPassword = password === confirmPassword;
const hasNumber = /\d/.test(password);
const hasMinLength = password.length >= 8;
const hasUpperAndLower = /[A-Z]/.test(password) && /[a-z]/.test(password);
const hasSpecialChar = /[!@#$%^&*]/.test(password);
const validPassword =
hasNumber && hasMinLength && hasUpperAndLower && hasSpecialChar;
const [showPasswordValidations, setShowPasswordValidations] = useState(false);
const [loading, setLoading] = useState(false);
@@ -82,24 +86,31 @@ export function UserCompletionForm() {
return () => clearInterval(timer);
}, [buttonState, countdown]);
const toPersianDigits = (str: string) =>
str.replace(/\d/g, (d) => '۰۱۲۳۴۵۶۷۸۹'[parseInt(d, 10)]);
const getButtonLabel = () => {
if (buttonState === 'counting') {
const m = String(Math.floor(countdown / 60)).padStart(2, '0');
const s = String(countdown % 60).padStart(2, '0');
return toPersianDigits(`${m}:${s}`);
return toLocaleDigits(`${m}:${s}`, i18n.language);
}
return t('completion.vericationCodeButton');
};
const storedToken = localStorage.getItem('authToken');
const handleSendCode = async () => {
setError(null);
setLoading(true);
setSuccess(false);
try {
const response = await apiClient.post('/User/SendEmailOtp', { email });
const response = await apiClient.post(
'/User/SendEmailOtp',
{ email },
{
headers: {
Authorization: `Bearer ${storedToken}`,
},
},
);
if (response.data?.success) {
showToast({
message: response.data.message || t('completion.successfullCodeSent'),
@@ -145,10 +156,18 @@ export function UserCompletionForm() {
setError(null);
try {
const res = await apiClient.post('/User/ConfirmEmailOtp', {
email,
otpCode: verificationCode,
});
const res = await apiClient.post(
'/User/ConfirmEmailOtp',
{
email,
otpCode: verificationCode,
},
{
headers: {
Authorization: `Bearer ${storedToken}`,
},
},
);
if (res.data?.success) {
setEmailVerified(true);
@@ -193,19 +212,27 @@ export function UserCompletionForm() {
errorCode: number;
message: string;
validations: { property: string; message: string }[];
}>('/User/CompleteUserInformation', {
userId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
firstName,
lastName,
gender: sex === 'female' ? 2 : 1,
nationalId,
savePassword: showPasswordSection,
password: showPasswordSection ? password : undefined,
saveEmail: showEmail,
email: showEmail ? email : undefined,
birthDate,
country,
});
}>(
'/User/CompleteUserInformation',
{
userId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
firstName,
lastName,
gender: sex === 'female' ? 2 : 1,
nationalId,
savePassword: showPasswordSection,
password: showPasswordSection ? password : undefined,
saveEmail: showEmail,
email: showEmail ? email : undefined,
birthDate,
country,
},
{
headers: {
Authorization: `Bearer ${storedToken}`,
},
},
);
if (data.success) {
showToast({
message: data.message || t('completion.submitSuccess'),
@@ -214,7 +241,7 @@ export function UserCompletionForm() {
} else {
showToast({
message: data.message || t('completion.submitError'),
severity: 'success',
severity: 'error',
});
}
} catch (error: unknown) {