From 59c971e0e732897ff43127c5c4544e4293cd7428 Mon Sep 17 00:00:00 2001 From: KimiaMontazeri Date: Mon, 27 Nov 2023 22:22:38 +0330 Subject: [PATCH] refactor: improve signup code --- frontend/src/pages/Signup/Signup.jsx | 78 ++++++++++++++++------------ 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/frontend/src/pages/Signup/Signup.jsx b/frontend/src/pages/Signup/Signup.jsx index 35b919c..461305c 100644 --- a/frontend/src/pages/Signup/Signup.jsx +++ b/frontend/src/pages/Signup/Signup.jsx @@ -48,19 +48,20 @@ const SignUpForm = ({ onLoginClick }) => { navigate(ROUTES.myAccount); }; - const handleSignUp = useCallback(() => { - const data = {}; - data.name = fullname; - data.phone_number = phoneNumber; - data.account = { - password: password, - email: email, - }; - createUser(data); - }, [createUser, email, fullname, password, phoneNumber]); + // const handleSignUp = useCallback(() => { + // const data = {}; + // data.name = fullname; + // data.phone_number = phoneNumber; + // data.account = { + // password: password, + // email: email, + // }; + // createUser(data); + // }, [createUser, email, fullname, password, phoneNumber]); // TODO: add form validation - const handleFormSubmit = (e) => { + const handleFormSubmit = async (e) => { + console.log('handle form submit'); e.preventDefault(); if (hasEmailError(email)) { setIsEmailWrong(true); @@ -83,28 +84,40 @@ const SignUpForm = ({ onLoginClick }) => { setSecondPassHelperText('Passwords are not the same'); return; } - // TODO: route to my-account page + + // API call + const data = { + name: fullname, + phone_number: phoneNumber, + account: { + password: password, + email: email, + }, + }; + createUser(data); }; useEffect(() => { if (createUserData == null) return; const toastDataTemp = {}; - switch (createUserData.status) { - case 200: - case 201: - toastDataTemp.message = 'Account Created Successfully! Please Check Your Email.'; - toastDataTemp.alertType = 'success'; - navToMyAccountPage(); - break; - case 400: - toastDataTemp.message = 'User with This Email Already Exists!'; - toastDataTemp.alertType = 'error'; - break; - default: - toastDataTemp.message = 'Unexpected Error! Please Try Again Later.'; - toastDataTemp.alertType = 'error'; - break; + if (createUserData) { + switch (createUserData.status) { + case 200: + case 201: + toastDataTemp.message = 'Account Created Successfully! Please Check Your Email.'; + toastDataTemp.alertType = 'success'; + navToMyAccountPage(); + break; + case 400: + toastDataTemp.message = 'User with This Email Already Exists!'; + toastDataTemp.alertType = 'error'; + break; + default: + toastDataTemp.message = 'Unexpected Error! Please Try Again Later.'; + toastDataTemp.alertType = 'error'; + break; + } } setAccessTokenFromLocalStorage(); @@ -185,7 +198,7 @@ const SignUpForm = ({ onLoginClick }) => { }} /> -