1- import React , { Fragment } from 'react' ;
1+ import React , { Fragment , useContext , useState } from 'react' ;
22import './Create.css' ;
33import Header from '../Header/Header' ;
4-
4+ import { FirebaseContext , AuthContext } from '../../store/Context' ;
5+ import firebase from 'firebase/app' ;
6+ import 'firebase/storage' ;
7+ import { useHistory } from 'react-router-dom' ;
58const Create = ( ) => {
9+ const { firebase} = useContext ( FirebaseContext )
10+ const { user} = useContext ( AuthContext )
11+ const history = useHistory ( )
12+ const [ name , setName ] = useState ( '' )
13+ const [ category , setCategory ] = useState ( '' )
14+ const [ price , setPrice ] = useState ( '' )
15+ const [ image , setImage ] = useState ( null )
16+ const date = new Date ( )
17+ const handleSubmit = ( ) => {
18+ firebase . storage ( ) . ref ( `/image/${ image . name } ` ) . put ( image ) . then ( ( { ref} ) => {
19+ ref . getDownloadURL ( ) . then ( ( url ) => {
20+ console . log ( url )
21+ firebase . firestore ( ) . collection ( 'products' ) . add ( {
22+ name,
23+ category,
24+ price,
25+ url,
26+ userId :user . uid ,
27+ createdAt :date . toDateString ( )
28+ } )
29+ history . push ( '/' )
30+ } )
31+ } )
32+ }
633 return (
734 < Fragment >
835 < Header />
936 < card >
1037 < div className = "centerDiv" >
11- < form >
38+
1239 < label htmlFor = "fname" > Name</ label >
1340 < br />
1441 < input
1542 className = "input"
1643 type = "text"
1744 id = "fname"
45+ value = { name }
46+ onChange = { ( e ) => setName ( e . target . value ) }
1847 name = "Name"
1948 defaultValue = "John"
2049 />
@@ -25,23 +54,27 @@ const Create = () => {
2554 className = "input"
2655 type = "text"
2756 id = "fname"
57+ value = { category }
58+ onChange = { ( e ) => setCategory ( e . target . value ) }
2859 name = "category"
2960 defaultValue = "John"
3061 />
3162 < br />
3263 < label htmlFor = "fname" > Price</ label >
3364 < br />
34- < input className = "input" type = "number" id = "fname" name = "Price" />
65+ < input className = "input" type = "number" id = "fname" value = { price } onChange = { ( e ) => setPrice ( e . target . value ) } name = "Price" />
3566 < br />
36- </ form >
67+
3768 < br />
38- < img alt = "Posts" width = "200px" height = "200px" src = "" > </ img >
39- < form >
69+ < img alt = "Posts" width = "200px" height = "200px" src = { image ? URL . createObjectURL ( image ) : "" } > </ img >
70+
4071 < br />
41- < input type = "file" />
72+ < input onChange = { ( e ) => {
73+ setImage ( e . target . files [ 0 ] )
74+ } } type = "file" />
4275 < br />
43- < button className = "uploadBtn" > upload and Submit</ button >
44- </ form >
76+ < button onClick = { handleSubmit } className = "uploadBtn" > upload and Submit</ button >
77+
4578 </ div >
4679 </ card >
4780 </ Fragment >
0 commit comments