From 4ce1017ff72ce4cbb68160187d42dc1a777f5491 Mon Sep 17 00:00:00 2001 From: vrthelen Date: Sat, 16 Mar 2024 23:02:59 -0400 Subject: [PATCH 1/2] Added functionality to registration page, likely not very secure --- src/pages/Register/index.tsx | 106 +++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 6 deletions(-) diff --git a/src/pages/Register/index.tsx b/src/pages/Register/index.tsx index fdad273..50a69d2 100644 --- a/src/pages/Register/index.tsx +++ b/src/pages/Register/index.tsx @@ -1,18 +1,51 @@ import './style.module.css' +import { useRequest } from 'ahooks' import PageContainer from 'components/PageContainer' import type React from 'react' import { useState } from 'react' import { Form, Button, Col, Row, Alert, Accordion } from 'react-bootstrap' import { useNavigate } from 'react-router-dom' +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 { 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...) + { + manual: true, + onSuccess: () => { + navigate('/register/confirm') + }, + onError: err => { + // TODO: if status is 409, tell user that email/username is already registered + // FIX: error.message does not work? + setError(err.message || 'Registration failed. Please try again.') + } + } + ) + const handleSubmit = (e: React.FormEvent) => { + Backend.auth.v1AuthLoginCreate({ + username: 'demo', + password: '123456' + }) + registerUser() e.preventDefault() - setError('TODO: Implement registration') - navigate('/register/confirm') + Backend.auth.v1AuthLogoutCreate() // validate input fields // if (!email || !password || !name || !phone) { // setError('Please fill in all mandatory fields') @@ -22,6 +55,14 @@ const RegistrationForm = () => { // 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 ( <>
@@ -30,20 +71,61 @@ const RegistrationForm = () => {
+ + Username + setUsername(e.target.value)} + /> + + Email - + setEmail(e.target.value)} + /> Password - + setPassword(e.target.value)} + /> + - + Name - + setFirstName(e.target.value)} + /> + + + + Last Name + setLastName(e.target.value)} + /> @@ -51,6 +133,7 @@ const RegistrationForm = () => { + Address @@ -133,6 +216,17 @@ const RegistrationForm = () => { + + {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} + +