-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathCreate.js
More file actions
84 lines (79 loc) · 2.54 KB
/
Copy pathCreate.js
File metadata and controls
84 lines (79 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React, { Fragment, useContext, useState } from 'react';
import './Create.css';
import Header from '../Header/Header';
import { FirebaseContext,AuthContext } from '../../store/Context';
import firebase from 'firebase/app';
import 'firebase/storage';
import { useHistory } from 'react-router-dom';
const Create = () => {
const {firebase}=useContext(FirebaseContext)
const {user}=useContext(AuthContext)
const history=useHistory()
const [name,setName]=useState('')
const [category,setCategory]=useState('')
const [price,setPrice]=useState('')
const [image,setImage]=useState(null)
const date=new Date()
const handleSubmit=()=>{
firebase.storage().ref(`/image/${image.name}`).put(image).then(({ref})=>{
ref.getDownloadURL().then((url)=>{
console.log(url)
firebase.firestore().collection('products').add({
name,
category,
price,
url,
userId:user.uid,
createdAt:date.toDateString()
})
history.push('/')
})
})
}
return (
<Fragment>
<Header />
<card>
<div className="centerDiv">
<label htmlFor="fname">Name</label>
<br />
<input
className="input"
type="text"
id="fname"
value={name}
onChange={(e)=>setName(e.target.value)}
name="Name"
defaultValue="John"
/>
<br />
<label htmlFor="fname">Category</label>
<br />
<input
className="input"
type="text"
id="fname"
value={category}
onChange={(e)=>setCategory(e.target.value)}
name="category"
defaultValue="John"
/>
<br />
<label htmlFor="fname">Price</label>
<br />
<input className="input" type="number" id="fname"value={price} onChange={(e)=>setPrice(e.target.value)} name="Price" />
<br />
<br />
<img alt="Posts" width="200px" height="200px" src={image ? URL.createObjectURL(image):""}></img>
<br />
<input onChange={(e)=>{
setImage(e.target.files[0])
}} type="file" />
<br />
<button onClick={handleSubmit} className="uploadBtn">upload and Submit</button>
</div>
</card>
</Fragment>
);
};
export default Create;