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