-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathCreate.js
More file actions
92 lines (84 loc) · 2.53 KB
/
Create.js
File metadata and controls
92 lines (84 loc) · 2.53 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
85
86
87
88
89
90
91
92
import React, { Fragment, useContext, useState } from 'react';
import './Create.css';
import Header from '../Header/Header';
import { FirebaseContext,AuthContext } from '../../store/Context';
import { useHistory } from 'react-router-dom';
const Create = () => {
const history = useHistory()
const {firebase} = useContext(FirebaseContext)
const {user} = useContext(AuthContext)
const[name,setName]= useState()
const[category,setCategory]= useState()
const[price,setPrice]= useState()
const[image,setImage]= useState()
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">
<form>
<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="fcate"
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="fprice"
value={price}
onChange={(e)=> setPrice(e.target.value)}
name="Price" />
<br />
</form>
<br />
{image && <img alt='images' width="200px" height="200px" src={image ? URL.createObjectURL(image) : null}></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;