Refactored the submit function to improve readability
This commit is contained in:
1 parent
a8db11b26e
commit
35f2879e34
1 file changed
+55
-43
@@ -423,57 +423,69 @@ const Register = ({ isAdmin }) => {
|
|||||||
createToast({ body: error.details[0].message || "Error validating data." });
|
createToast({ body: error.details[0].message || "Error validating data." });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleStepOne = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
let error;
|
let error = validateForm({
|
||||||
let registerForm;
|
firstName: form.firstName,
|
||||||
|
lastName: form.lastName,
|
||||||
if (step === 1) {
|
});
|
||||||
error = validateForm({
|
|
||||||
firstName: form.firstName,
|
|
||||||
lastName: form.lastName,
|
|
||||||
});
|
|
||||||
} else if (step === 2) {
|
|
||||||
error = validateForm({ email: form.email });
|
|
||||||
} else if (step === 3) {
|
|
||||||
registerForm = { ...form, role: isAdmin ? ["admin"] : form.role };
|
|
||||||
error = validateForm(registerForm, {
|
|
||||||
context: { password: form.password },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
handleError(error);
|
handleError(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (step === 1) {
|
setStep(2);
|
||||||
setStep(2);
|
};
|
||||||
} else if (step === 2) {
|
|
||||||
setStep(3);
|
const handleStepTwo = async (e) => {
|
||||||
} else if (step === 3) {
|
e.preventDefault();
|
||||||
delete registerForm.confirm;
|
|
||||||
const action = await dispatch(register(registerForm));
|
let error;
|
||||||
if (action.payload.success) {
|
error = validateForm({ email: form.email });
|
||||||
const token = action.payload.data;
|
if (error) {
|
||||||
localStorage.setItem("token", token);
|
handleError(error);
|
||||||
navigate("/");
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setStep(3);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Final step
|
||||||
|
// Attempts account registration
|
||||||
|
const handleStepThree = async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
let registerForm = { ...form, role: isAdmin ? ["admin"] : form.role };
|
||||||
|
let error = validateForm(registerForm, {
|
||||||
|
context: { password: form.password },
|
||||||
|
});
|
||||||
|
if (error) {
|
||||||
|
handleError(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete registerForm.confirm;
|
||||||
|
const action = await dispatch(register(registerForm));
|
||||||
|
if (action.payload.success) {
|
||||||
|
const token = action.payload.data;
|
||||||
|
localStorage.setItem("token", token);
|
||||||
|
navigate("/");
|
||||||
|
createToast({
|
||||||
|
body: "Welcome! Your account was created successfully.",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (action.payload) {
|
||||||
|
// dispatch errors
|
||||||
createToast({
|
createToast({
|
||||||
body: "Welcome! Your account was created successfully.",
|
body: action.payload.msg,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (action.payload) {
|
// unknown errors
|
||||||
// dispatch errors
|
createToast({
|
||||||
createToast({
|
body: "Unknown error.",
|
||||||
body: action.payload.msg,
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// unknown errors
|
|
||||||
createToast({
|
|
||||||
body: "Unknown error.",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -542,7 +554,7 @@ const Register = ({ isAdmin }) => {
|
|||||||
<StepOne
|
<StepOne
|
||||||
form={form}
|
form={form}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleStepOne}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
onBack={() => setStep(0)}
|
onBack={() => setStep(0)}
|
||||||
/>
|
/>
|
||||||
@@ -550,7 +562,7 @@ const Register = ({ isAdmin }) => {
|
|||||||
<StepTwo
|
<StepTwo
|
||||||
form={form}
|
form={form}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleStepTwo}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
onBack={() => setStep(1)}
|
onBack={() => setStep(1)}
|
||||||
/>
|
/>
|
||||||
@@ -558,7 +570,7 @@ const Register = ({ isAdmin }) => {
|
|||||||
<StepThree
|
<StepThree
|
||||||
form={form}
|
form={form}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleStepThree}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
onBack={() => setStep(2)}
|
onBack={() => setStep(2)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in new issue
Block a user