update validation messages
This commit is contained in:
+43
-17
@@ -135,12 +135,12 @@ export async function authorization(
|
||||
|
||||
type FormState = {
|
||||
previousState: {
|
||||
full_name: string;
|
||||
fullName: string;
|
||||
email: string;
|
||||
password: string;
|
||||
confirm_password: string;
|
||||
phone: string;
|
||||
company: string;
|
||||
companyName: string;
|
||||
agree: string;
|
||||
}
|
||||
error: Record<string, string>
|
||||
@@ -150,16 +150,16 @@ export async function registration(
|
||||
state: FormState | undefined,
|
||||
formData: FormData,
|
||||
): Promise<FormState> {
|
||||
const full_name = formData.get('full_name') as string || '';
|
||||
const fullName = formData.get('fullName') as string || '';
|
||||
const email = formData.get('email') as string || '';
|
||||
const password = formData.get('password') as string || '';
|
||||
const confirm_password = formData.get('confirm_password') as string || '';
|
||||
const phone = formData.get('phone') as string || '';
|
||||
const company = formData.get('company') as string || '';
|
||||
const companyName = formData.get('companyName') as string || '';
|
||||
const agree = formData.get('agree') as string || '';
|
||||
|
||||
const validatedFields = SignupFormSchema.safeParse({
|
||||
full_name,
|
||||
fullName,
|
||||
email,
|
||||
phone: phone.replace(/[-\(\)\s]/g, ''),
|
||||
password,
|
||||
@@ -182,12 +182,12 @@ export async function registration(
|
||||
|
||||
return {
|
||||
previousState: {
|
||||
full_name,
|
||||
fullName,
|
||||
email,
|
||||
password,
|
||||
confirm_password,
|
||||
phone,
|
||||
company,
|
||||
companyName,
|
||||
agree
|
||||
},
|
||||
error: errors
|
||||
@@ -195,18 +195,18 @@ export async function registration(
|
||||
}
|
||||
|
||||
try {
|
||||
const { full_name, email, password, phone } = validatedFields.data;
|
||||
const { fullName, email, password, phone } = validatedFields.data;
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 20002,
|
||||
message_body: {
|
||||
fullName: full_name,
|
||||
fullName: fullName,
|
||||
email,
|
||||
phone: phone,
|
||||
password,
|
||||
companyName: company,
|
||||
companyName: companyName,
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
@@ -216,23 +216,47 @@ export async function registration(
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
let parsed = await response.json() as {
|
||||
message_desc: string,
|
||||
message_body: { token: string },
|
||||
message_code: string
|
||||
};
|
||||
if (parsed.message_desc === 'Operation successful') {
|
||||
await createSession(parsed.message_body.token, email);
|
||||
} else {
|
||||
throw (`${parsed.message_code}`);
|
||||
console.log('error');
|
||||
/* console.log(parsed.message_body); */
|
||||
/* throw (`${parsed.message_code.fieldErrors}`); */
|
||||
throw parsed;
|
||||
}
|
||||
} else {
|
||||
throw (`${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
if (error) {
|
||||
const typedError = error as any;
|
||||
const errors: Record<string, string> = {};
|
||||
switch (error) {
|
||||
case '1':
|
||||
console.log(typedError);
|
||||
console.log(typedError.message_body);
|
||||
|
||||
switch (typedError.message_code) {
|
||||
case 1:
|
||||
errors['server'] = 'email-or-already-registered'
|
||||
break;
|
||||
|
||||
case 2:
|
||||
typedError.message_body.fieldErrors.forEach((obj: {
|
||||
code: string,
|
||||
field: string
|
||||
}) => {
|
||||
if (errors[obj.field]) {
|
||||
errors[obj.field] = `${errors[obj.field]}&${obj.code.replaceAll('.', '-')}`;
|
||||
} else {
|
||||
errors[obj.field] = obj.code.replaceAll('.', '-');
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
errors['server'] = 'request-ended-with-an-error'
|
||||
break;
|
||||
@@ -240,17 +264,19 @@ export async function registration(
|
||||
|
||||
return {
|
||||
previousState: {
|
||||
full_name,
|
||||
fullName,
|
||||
email,
|
||||
password,
|
||||
confirm_password,
|
||||
phone,
|
||||
company,
|
||||
companyName,
|
||||
agree
|
||||
},
|
||||
error: errors
|
||||
};
|
||||
}
|
||||
console.log('finale error');
|
||||
console.log(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user