Skip to content

Commit fafa2a1

Browse files
created page to sell products
1 parent f0e101f commit fafa2a1

5 files changed

Lines changed: 94 additions & 30 deletions

File tree

src/App.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import './App.css';
33
import { BrowserRouter as Router,Route} from 'react-router-dom';
44
import Signup from './Pages/Signup'
55
import Login from './Pages/Login';
6+
import CreatePage from './Pages/Create';
7+
import View from './Pages/ViewPost'
68
import { AuthContext,FirebaseContext } from './store/Context';
79

810
/**
@@ -32,6 +34,13 @@ function App() {
3234
<Route path='/login'>
3335
<Login />
3436
</Route>
37+
38+
<Route path='/create'>
39+
<CreatePage />
40+
</Route>
41+
<Route path='/view'>
42+
<View/>
43+
</Route>
3544

3645
</Router>
3746
</div>

src/Components/Create/Create.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1-
import React, { Fragment } from 'react';
1+
import React, { Fragment, useContext, useState } from 'react';
22
import './Create.css';
33
import 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';
58
const 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>

src/Components/Posts/Posts.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
import React from 'react';
2-
1+
import React,{useEffect,useState,useContext} from 'react';
2+
import {useHistory} from 'react-router-dom'
33
import Heart from '../../assets/Heart';
44
import './Post.css';
5+
import { FirebaseContext } from '../../store/Context';
56

67
function Posts() {
8+
const {firebase}=useContext(FirebaseContext)
9+
const [products,setProducts]=useState([])
10+
useEffect(()=>{
11+
firebase.firestore().collection('products').get().then((snapshot)=>{
12+
const allPost=snapshot.docs.map((product)=>{
13+
return{
14+
...product.data(),
15+
id:product.id
16+
}
17+
718

19+
})
20+
setProducts(allPost)
21+
})
22+
})
823
return (
924
<div className="postParentDiv">
1025
<div className="moreView">
@@ -13,24 +28,29 @@ function Posts() {
1328
<span>View more</span>
1429
</div>
1530
<div className="cards">
16-
<div
17-
className="card"
18-
>
19-
<div className="favorite">
20-
<Heart></Heart>
21-
</div>
22-
<div className="image">
23-
<img src="../../../Images/R15V3.jpg" alt="" />
24-
</div>
25-
<div className="content">
26-
<p className="rate">&#x20B9; 250000</p>
27-
<span className="kilometer">Two Wheeler</span>
28-
<p className="name"> YAMAHA R15V3</p>
29-
</div>
30-
<div className="date">
31-
<span>Tue May 04 2021</span>
32-
</div>
33-
</div>
31+
{products.map(product=>{
32+
return <div
33+
className="card"
34+
>
35+
<div className="favorite">
36+
<Heart></Heart>
37+
</div>
38+
<div className="image">
39+
<img src={product.url} alt="" />
40+
</div>
41+
<div className="content">
42+
<p className="rate">&#x20B9; {product.price}</p>
43+
<span className="kilometer">{product.category}</span>
44+
<p className="name"> {product.name}</p>
45+
</div>
46+
<div className="date">
47+
<span>{product.createdAt}</span>
48+
</div>
49+
</div>
50+
51+
})
52+
53+
}
3454
</div>
3555
</div>
3656
<div className="recommendations">

src/Pages/Create.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Fragment } from 'react';
2+
23
import Header from '../Components/Header/Header';
34
import Create from '../Components/Create/Create';
45

src/firebase/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import firebase from 'firebase/app';
22
import 'firebase/auth';
33
import 'firebase/firestore';
4+
import 'firebase/storage'
45
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
56
const firebaseConfig = {
67
apiKey: "AIzaSyAkRtp7S1LrCiKGTnKK_3Jycr-tu91Zda0",

0 commit comments

Comments
 (0)