1- import React from 'react' ;
2-
1+ import React , { useState , useContext } from 'react' ;
32import Logo from '../../olx-logo.png' ;
43import './Signup.css' ;
4+ import FirebaseContext from '../../store/Context' ;
5+ import { useHistory } from 'react-router-dom' ;
56
67export default function Signup ( ) {
8+ const history = useHistory ( ) ;
9+ const [ username , setUsername ] = useState ( '' ) ;
10+ const [ email , setEmail ] = useState ( '' ) ;
11+ const [ phone , setPhone ] = useState ( '' ) ;
12+ const [ password , setPassword ] = useState ( '' ) ;
13+ const { firebase } = useContext ( FirebaseContext ) ; // Remove 'firestore' from the destructured assignment
14+
15+ const handleSubmit = ( e ) => {
16+ e . preventDefault ( ) ;
17+ firebase
18+ . auth ( )
19+ . createUserWithEmailAndPassword ( email , password )
20+ . then ( ( result ) => {
21+ result . user
22+ . updateProfile ( { displayName : username } )
23+ . then ( ( ) => {
24+ firebase
25+ . firestore ( ) // Access Firestore directly from the 'firebase' instance
26+ . collection ( 'users' ) // Access the 'users' collection
27+ . add ( {
28+ id : result . user . uid ,
29+ username : username ,
30+ phone : phone ,
31+ } )
32+ . then ( ( ) => {
33+ history . push ( '/login' ) ;
34+ } )
35+ . catch ( ( error ) => {
36+ console . error ( 'Error adding document: ' , error ) ;
37+ } ) ;
38+ } ) ;
39+ } )
40+ . catch ( ( error ) => {
41+ console . error ( 'Error creating user: ' , error ) ;
42+ } ) ;
43+ } ;
44+
745 return (
846 < div >
947 < div className = "signupParentDiv" >
10- < img width = "200px" height = "200px" src = { Logo } > </ img >
11- < form >
48+ < img width = "200px" height = "200px" src = { Logo } alt = "Logo" / >
49+ < form onSubmit = { handleSubmit } >
1250 < label htmlFor = "fname" > Username</ label >
1351 < br />
1452 < input
1553 className = "input"
1654 type = "text"
1755 id = "fname"
56+ value = { username }
57+ onChange = { ( e ) => setUsername ( e . target . value ) }
1858 name = "name"
1959 defaultValue = "John"
2060 />
@@ -25,6 +65,8 @@ export default function Signup() {
2565 className = "input"
2666 type = "email"
2767 id = "fname"
68+ value = { email }
69+ onChange = { ( e ) => setEmail ( e . target . value ) }
2870 name = "email"
2971 defaultValue = "John"
3072 />
@@ -35,6 +77,8 @@ export default function Signup() {
3577 className = "input"
3678 type = "number"
3779 id = "lname"
80+ value = { phone }
81+ onChange = { ( e ) => setPhone ( e . target . value ) }
3882 name = "phone"
3983 defaultValue = "Doe"
4084 />
@@ -45,14 +89,16 @@ export default function Signup() {
4589 className = "input"
4690 type = "password"
4791 id = "lname"
92+ value = { password }
93+ onChange = { ( e ) => setPassword ( e . target . value ) }
4894 name = "password"
4995 defaultValue = "Doe"
5096 />
5197 < br />
5298 < br />
5399 < button > Signup</ button >
54100 </ form >
55- < a > Login</ a >
101+ < a href = "/login" > Login</ a >
56102 </ div >
57103 </ div >
58104 ) ;
0 commit comments