From 8cc4365ffe646d79131c6bab2c235dcee700f9b6 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Wed, 1 Jun 2022 14:54:45 -0500 Subject: [PATCH] state simplification --- client/src/components/User/Register.tsx | 66 ++++++++++--------------- client/src/types/main.d.ts | 5 ++ routes/register.js | 6 ++- 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/client/src/components/User/Register.tsx b/client/src/components/User/Register.tsx index 200def2..ab05bd5 100644 --- a/client/src/components/User/Register.tsx +++ b/client/src/components/User/Register.tsx @@ -5,70 +5,54 @@ import { registerNewUser } from "../../util/apiUtils"; import Page from "../../util/Page"; function Register() { - const [name, setName] = useState(''); - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const [verify, setVerify] = useState(''); - const [warningText, setWarningText] = useState('initial'); - - const [userInput, setUserInput] = useState({ - name: '', + const formInitialState: userInfo = { + firstName: '', + lastName: '', email: '', password: '', - }) + verifyPassword: '', + created: '', + headers: emptySessionHeader + } - const [userData, setUserData] = useState(); + const [userInput, setUserInput] = useState(formInitialState); + const [warningText, setWarningText] = useState('initial'); // checks password complexity useEffect(() => { - if (!password) return; + if (!userInput.password) return; switch (true) { - case (!verify): + case (!userInput.verifyPassword): setWarningText('Verify your password below.'); break; - case (verify !== password): + case (userInput.verifyPassword !== userInput.password): setWarningText('Passwords do not match.'); break; - case (verify && !verify.includes('!')): + case (userInput.verifyPassword && !userInput.verifyPassword.includes('!')): setWarningText('Password does not meet safety criteria.'); break; - case (verify === password): + case (userInput.verifyPassword === userInput.password): setWarningText(''); break; default: throw new Error("Password switch case is faulty"); } - }, [password, verify]); - - // updates user info object on each render - useEffect(() => { - let newInfo: userInfo = { - name: name, - email: email, - password: password, - headers: emptySessionHeader - } - - setUserData(newInfo); - }, [name, email, password]); + }, [userInput.password, userInput.verifyPassword]); // interrupts rendering loop by setting warning text on password data useEffect(() => { if (warningText === '') { setWarningText('Conditions met!'); } - }, [userData, warningText]); + }, [userInput, warningText]); // allows registration submission if warning text has correct value and userData is defined with all required values const handleRegistration = async () => { - if (!userData) return; - warningText === "Conditions met!" && await registerNewUser(userData); + if (userInput === formInitialState) return; + warningText === "Conditions met!" && await registerNewUser(userInput); - setName(''); - setEmail(''); - setPassword(''); - setVerify(''); + setUserInput(formInitialState); } return ( @@ -78,18 +62,18 @@ function Register() {
- setName(e.target.value)}/> + setUserInput({...userInput, firstName: e.target.value})}/>
- setName(e.target.value)}/> + setUserInput({...userInput, lastName: e.target.value})}/>
- setEmail(e.target.value)}/> + setUserInput({...userInput, email: e.target.value})}/>

{warningText}

@@ -98,17 +82,17 @@ function Register() { - setPassword(e.target.value)}/> + setUserInput({...userInput, password: e.target.value})}/>
- setVerify(e.target.value)}/> + setUserInput({...userInput, verifyPassword: e.target.value})}/>
- + ); } diff --git a/client/src/types/main.d.ts b/client/src/types/main.d.ts index b9237c1..b449f23 100644 --- a/client/src/types/main.d.ts +++ b/client/src/types/main.d.ts @@ -6,9 +6,14 @@ export type userInfo = { lastName?: string email: string id?: number + + // NOTE: userInfo.name is deprecated name?: string password: string + verifyPassword?: string headers?: SessionHeader + created?: string + modified?: string } export type LoginHeaders = { diff --git a/routes/register.js b/routes/register.js index 6abe0a8..2e6a852 100644 --- a/routes/register.js +++ b/routes/register.js @@ -5,14 +5,16 @@ const client = require('../db/Client'); registerRouter.route('/').post(async (req, res) => { const newClient = client(); - const { name, email, password } = req.body; + const { firstName, lastName, email, password } = req.body; const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(password, salt); try { newClient.connect().then(console.log("Connection successful.")); - await newClient.query("INSERT INTO users (name, email, password) values ($1, $2, $3)", [name, email, hash]); + await newClient.query( + "INSERT INTO users (first_name, last_name, email, password) values ($1, $2, $3, $4)", + [firstName, lastName, email, hash]); res.sendStatus(200); } catch(e) { console.log(e);