From a20b8e1e5d2b2c86aeb40786e50b9c807c03938c Mon Sep 17 00:00:00 2001 From: vrthelen Date: Sun, 17 Mar 2024 17:08:26 -0400 Subject: [PATCH] Using new endpoint and struct for registration --- src/pages/Register/index.tsx | 311 +++++++++++++++++++++++++---------- 1 file changed, 223 insertions(+), 88 deletions(-) diff --git a/src/pages/Register/index.tsx b/src/pages/Register/index.tsx index 50a69d2..a017f35 100644 --- a/src/pages/Register/index.tsx +++ b/src/pages/Register/index.tsx @@ -9,22 +9,98 @@ import Backend from 'utils/service' const RegistrationForm = () => { const navigate = useNavigate() - const [email, setEmail] = useState('') - const [password, setPassword] = useState('') - const [firstName, setFirstName] = useState('') - const [lastName, setLastName] = useState('') - const [username, setUsername] = useState('') const [error, setError] = useState('') + const [form, setForm] = useState({ + email: '', + password: '', + name: '', + username: '', + address: '', + address2: '', + phone: '', + need_promotion: false, + city: '', + state: '', + zip: '', + card_address: '', + card_address2: '', + card_city: '', + card_state: '', + card_zip: '', + card_type: '', + card_number: '', + card_exp: '' + }) + const usStates = [ + 'AL', + 'AK', + 'AZ', + 'AR', + 'CA', + 'CO', + 'CT', + 'DE', + 'FL', + 'GA', + 'HI', + 'ID', + 'IL', + 'IN', + 'IA', + 'KS', + 'KY', + 'LA', + 'ME', + 'MD', + 'MA', + 'MI', + 'MN', + 'MS', + 'MO', + 'MT', + 'NE', + 'NV', + 'NH', + 'NJ', + 'NM', + 'NY', + 'NC', + 'ND', + 'OH', + 'OK', + 'OR', + 'PA', + 'RI', + 'SC', + 'SD', + 'TN', + 'TX', + 'UT', + 'VT', + 'VA', + 'WA', + 'WV', + 'WI', + 'WY' + ] + + // fix: is there a better way to handle this? + const handleChange = ( + event: React.ChangeEvent<{ name?: string; value: unknown }> + ) => { + const { name, value } = event.target as { name?: string; value: unknown } + if (name === 'need_promotion') { + setForm(changeForm => ({ + ...changeForm, + need_promotion: !changeForm.need_promotion + })) + } else if (typeof name === 'string') { + setForm(changeForm => ({ ...changeForm, [name]: value })) + } + } const { run: registerUser } = useRequest( - async () => - Backend.admin.v1AdminUsersCreate({ - email, - password, - first_name: firstName, - last_name: lastName, - username - }), // TODO: add more fields (promotions, payment info, address, phone...) + async () => Backend.auth.v1AuthRegisterCreate(form), { manual: true, onSuccess: () => { @@ -39,28 +115,8 @@ const RegistrationForm = () => { ) const handleSubmit = (e: React.FormEvent) => { - Backend.auth.v1AuthLoginCreate({ - username: 'demo', - password: '123456' - }) registerUser() e.preventDefault() - Backend.auth.v1AuthLogoutCreate() - // validate input fields - // if (!email || !password || !name || !phone) { - // setError('Please fill in all mandatory fields') - // return - // } - // send registration request to the server - // handle response and set error or success message accordingly - } - - // eslint-disable-next-line - const [checked, setChecked] = useState(false) - // eslint-disable-next-line - const handleCheckboxChange = () => { - // TODO: change user's promotions preference to true - setChecked(prevChecked => !prevChecked) } return ( @@ -77,8 +133,8 @@ const RegistrationForm = () => { type='username' placeholder='Enter username (5 characters or longer)' required - value={username} - onChange={e => setUsername(e.target.value)} + value={form.username} + onChange={handleChange} /> @@ -88,8 +144,8 @@ const RegistrationForm = () => { type='email' placeholder='Enter email' required - value={email} - onChange={e => setEmail(e.target.value)} + value={form.email} + onChange={handleChange} /> @@ -99,77 +155,119 @@ const RegistrationForm = () => { type='password' placeholder='Password (10 characters or longer)' required - value={password} - onChange={e => setPassword(e.target.value)} + value={form.password} + onChange={handleChange} /> - + Name setFirstName(e.target.value)} + value={form.name} + onChange={handleChange} /> - - Last Name + + Phone setLastName(e.target.value)} + value={form.phone} + onChange={handleChange} /> - - - Phone - - Address - + + Address 2 - + + City - + + State - + - + {usStates.map((state, index) => ( + + ))} + Zip - + + - + + Payment Information - + Card Type - + @@ -180,53 +278,90 @@ const RegistrationForm = () => { Card Number - + + Card Expiration - + - + + Address - + - + + Address 2 - + + - + City - + - + + State - + - + {usStates.map((state, index) => ( + + ))} - + + Zip - + - - {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} - -