1+ import React , { useState } from "react" ;
2+ import "./Register.css" ;
3+ import user_icon from "../assets/person.png"
4+ import email_icon from "../assets/email.png"
5+ import password_icon from "../assets/password.png"
6+ import close_icon from "../assets/close.png"
7+
8+ const Register = ( ) => {
9+ // State variables for form inputs
10+ const [ userName , setUserName ] = useState ( "" ) ;
11+ const [ password , setPassword ] = useState ( "" ) ;
12+ const [ email , setEmail ] = useState ( "" ) ;
13+ const [ firstName , setFirstName ] = useState ( "" ) ;
14+ const [ lastName , setlastName ] = useState ( "" ) ;
15+
16+ // Redirect to home
17+ const gohome = ( ) => {
18+ window . location . href = window . location . origin ;
19+ }
20+
21+ // Handle form submission
22+ const register = async ( e ) => {
23+ e . preventDefault ( ) ;
24+
25+ let register_url = window . location . origin + "/djangoapp/register" ;
26+
27+ // Send POST request to register endpoint
28+ const res = await fetch ( register_url , {
29+ method : "POST" ,
30+ headers : {
31+ "Content-Type" : "application/json" ,
32+ } ,
33+ body : JSON . stringify ( {
34+ "userName" : userName ,
35+ "password" : password ,
36+ "firstName" :firstName ,
37+ "lastName" :lastName ,
38+ "email" :email
39+ } ) ,
40+ } ) ;
41+
42+ const json = await res . json ( ) ;
43+ if ( json . status ) {
44+ // Save username in session and reload home
45+ sessionStorage . setItem ( 'username' , json . userName ) ;
46+ window . location . href = window . location . origin ;
47+ }
48+ else if ( json . error === "Already Registered" ) {
49+ alert ( "The user with same username is already registered" ) ;
50+ window . location . href = window . location . origin ;
51+ }
52+ } ;
53+
54+ return (
55+ < div className = "register_container" style = { { width : "50%" } } >
56+ < div className = "header" style = { { display : "flex" , flexDirection : "row" , justifyContent : "space-between" } } >
57+ < span className = "text" style = { { flexGrow :"1" } } > SignUp</ span >
58+ < div style = { { display : "flex" , flexDirection : "row" , justifySelf : "end" , alignSelf : "start" } } >
59+ < a href = "/" onClick = { ( ) => { gohome ( ) } } style = { { justifyContent : "space-between" , alignItems :"flex-end" } } >
60+ < img style = { { width :"1cm" } } src = { close_icon } alt = "X" />
61+ </ a >
62+ </ div >
63+ < hr />
64+ </ div >
65+
66+ < form onSubmit = { register } >
67+ < div className = "inputs" >
68+ < div className = "input" >
69+ < img src = { user_icon } className = "img_icon" alt = 'Username' />
70+ < input type = "text" name = "username" placeholder = "Username" className = "input_field" onChange = { ( e ) => setUserName ( e . target . value ) } />
71+ </ div >
72+ < div >
73+ < img src = { user_icon } className = "img_icon" alt = 'First Name' />
74+ < input type = "text" name = "first_name" placeholder = "First Name" className = "input_field" onChange = { ( e ) => setFirstName ( e . target . value ) } />
75+ </ div >
76+
77+ < div >
78+ < img src = { user_icon } className = "img_icon" alt = 'Last Name' />
79+ < input type = "text" name = "last_name" placeholder = "Last Name" className = "input_field" onChange = { ( e ) => setlastName ( e . target . value ) } />
80+ </ div >
81+
82+ < div >
83+ < img src = { email_icon } className = "img_icon" alt = 'Email' />
84+ < input type = "email" name = "email" placeholder = "email" className = "input_field" onChange = { ( e ) => setEmail ( e . target . value ) } />
85+ </ div >
86+
87+ < div className = "input" >
88+ < img src = { password_icon } className = "img_icon" alt = 'password' />
89+ < input name = "psw" type = "password" placeholder = "Password" className = "input_field" onChange = { ( e ) => setPassword ( e . target . value ) } />
90+ </ div >
91+
92+ </ div >
93+ < div className = "submit_panel" >
94+ < input className = "submit" type = "submit" value = "Register" />
95+ </ div >
96+ </ form >
97+ </ div >
98+ )
99+ }
100+
101+ export default Register ;
0 commit comments