Skip to content

Commit 3fa78f8

Browse files
producs view page modified
1 parent fafa2a1 commit 3fa78f8

4 files changed

Lines changed: 97 additions & 52 deletions

File tree

src/App.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
1-
import React,{useEffect,useContext} from 'react';
1+
import React, { useEffect, useContext } from 'react';
22
import './App.css';
3-
import { BrowserRouter as Router,Route} from 'react-router-dom';
4-
import Signup from './Pages/Signup'
3+
import { BrowserRouter as Router, Route } from 'react-router-dom';
4+
import Signup from './Pages/Signup';
55
import Login from './Pages/Login';
66
import CreatePage from './Pages/Create';
7-
import View from './Pages/ViewPost'
8-
import { AuthContext,FirebaseContext } from './store/Context';
9-
10-
/**
11-
* ? =====Import Components=====
12-
*/
7+
import View from './Pages/ViewPost';
8+
import { AuthContext, FirebaseContext } from './store/Context';
9+
import Post, { PostContext } from './store/PostContext';
1310
import Home from './Pages/Home';
1411

15-
1612
function App() {
17-
const {setUser}=useContext(AuthContext)
18-
const {firebase}=useContext(FirebaseContext)
19-
useEffect(()=>{
20-
firebase.auth().onAuthStateChanged((user)=>{
21-
setUser(user)
22-
})
23-
})
24-
return (
25-
<div>
26-
<Router>
27-
<Route exact path='/'>
28-
<Home />
29-
</Route>
13+
const { setUser } = useContext(AuthContext);
14+
const { firebase } = useContext(FirebaseContext);
3015

31-
<Route path='/signup'>
32-
<Signup />
33-
</Route>
34-
<Route path='/login'>
35-
<Login />
36-
</Route>
16+
useEffect(() => {
17+
firebase.auth().onAuthStateChanged((user) => {
18+
setUser(user);
19+
});
20+
}, [firebase, setUser]);
3721

38-
<Route path='/create'>
39-
<CreatePage />
40-
</Route>
41-
<Route path='/view'>
42-
<View/>
43-
</Route>
44-
45-
</Router>
22+
return (
23+
<div>
24+
<Post>
25+
<Router>
26+
<Route exact path="/">
27+
<Home />
28+
</Route>
29+
<Route path="/signup">
30+
<Signup />
31+
</Route>
32+
<Route path="/login">
33+
<Login />
34+
</Route>
35+
<Route path="/create">
36+
<CreatePage />
37+
</Route>
38+
<Route path="/view">
39+
<PostContext.Consumer>
40+
{(postContext) => <View postDetails={postContext.postDetails} />}
41+
</PostContext.Consumer>
42+
</Route>
43+
</Router>
44+
</Post>
4645
</div>
4746
);
4847
}

src/Components/Posts/Posts.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import {useHistory} from 'react-router-dom'
33
import Heart from '../../assets/Heart';
44
import './Post.css';
55
import { FirebaseContext } from '../../store/Context';
6+
import { PostContext } from '../../store/PostContext';
67

78
function Posts() {
89
const {firebase}=useContext(FirebaseContext)
910
const [products,setProducts]=useState([])
11+
const {setPostDetails}=useContext(PostContext)
12+
const history=useHistory()
1013
useEffect(()=>{
1114
firebase.firestore().collection('products').get().then((snapshot)=>{
1215
const allPost=snapshot.docs.map((product)=>{
@@ -31,6 +34,10 @@ function Posts() {
3134
{products.map(product=>{
3235
return <div
3336
className="card"
37+
onClick={()=>{
38+
setPostDetails(product)
39+
history.push('/view')
40+
}}
3441
>
3542
<div className="favorite">
3643
<Heart></Heart>

src/Components/View/View.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1-
import React from 'react';
2-
1+
import React, { useEffect, useState, useContext } from 'react';
32
import './View.css';
3+
import { PostContext } from '../../store/PostContext';
4+
import { FirebaseContext } from '../../store/Context';
5+
46
function View() {
7+
const [userDetails, setUserDetails] = useState();
8+
const { postDetails } = useContext(PostContext);
9+
const { firebase } = useContext(FirebaseContext);
10+
11+
useEffect(() => {
12+
if (postDetails && postDetails.userId) {
13+
const { userId } = postDetails;
14+
firebase
15+
.firestore()
16+
.collection('users')
17+
.where('id', '==', userId)
18+
.get()
19+
.then((res) => {
20+
res.forEach((doc) => {
21+
setUserDetails(doc.data());
22+
});
23+
});
24+
}
25+
}, [firebase, postDetails]);
26+
527
return (
628
<div className="viewParentDiv">
729
<div className="imageShowDiv">
8-
<img
9-
src="../../../Images/R15V3.jpg"
10-
alt=""
11-
/>
30+
{postDetails && postDetails.url && (
31+
<img src={postDetails.url} alt="" />
32+
)}
1233
</div>
1334
<div className="rightSection">
1435
<div className="productDetails">
15-
<p>&#x20B9; 250000 </p>
16-
<span>YAMAHA R15V3</span>
17-
<p>Two Wheeler</p>
18-
<span>Tue May 04 2021</span>
19-
</div>
20-
<div className="contactDetails">
21-
<p>Seller details</p>
22-
<p>No name</p>
23-
<p>1234567890</p>
36+
<p>&#x20B9; {postDetails && postDetails.price}</p>
37+
<span>{postDetails.name}</span>
38+
<p>{postDetails.category}</p>
39+
<span>{postDetails.createdAt}</span>
2440
</div>
41+
{userDetails && (
42+
<div className="contactDetails">
43+
<p>Seller details</p>
44+
<p>{userDetails.username}</p>
45+
<p>{userDetails.phone}</p>
46+
</div>
47+
)}
2548
</div>
2649
</div>
2750
);
2851
}
52+
2953
export default View;

src/store/PostContext.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createContext, useState } from 'react';
2+
3+
export const PostContext = createContext(null);
4+
5+
function Post({ children }) {
6+
const [postDetails, setPostDetails] = useState();
7+
8+
return (
9+
<PostContext.Provider value={{ postDetails, setPostDetails }}>
10+
{children}
11+
</PostContext.Provider>
12+
);
13+
}
14+
15+
export default Post;

0 commit comments

Comments
 (0)