Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Product Details State #43

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions frontend/src/Components/Body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import React, { useEffect } from 'react'
import { useDispatch, useSelector } from "react-redux";
import { Container } from 'react-bootstrap'
import ProductView from './Products'
import getProducts from '../actions/productListAction'
import { LinkContainer } from 'react-router-bootstrap';
import { productListAction } from '../actions/productActions'

const Body = () => {
const dispatch = useDispatch();

useEffect(() => {
dispatch(getProducts())
dispatch(productListAction())
}, [dispatch])

const productList = useSelector(state => state.productList)
Expand All @@ -19,6 +18,7 @@ const Body = () => {
return (
<div style={{ minHeight: "80vh" }}>
{(function () {

if (loading) {
return <h1> Loading...</h1 >
} else if (error) {
Expand Down
115 changes: 54 additions & 61 deletions frontend/src/Screens/ProductDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,73 @@ import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { Image, Row, Col, Container, Button } from 'react-bootstrap'
import Rating from '../Components/Rating'
import { Link } from 'react-router-dom'
import { productDetailsActions } from "../actions/productDetailsActions";
import axios from 'axios'
import { useParams } from 'react-router-dom'
import { Link, useParams } from 'react-router-dom'
import { productDetailsActions } from "../actions/productActions";

const ProductDetail = () => {
const params = useParams();
// const dispatch = useDispatch()
// useEffect(() => {
// dispatch(productDetailsActions(match.params.id));
// }, [dispatch])
const id = params.id;
const dispatch = useDispatch()

// const productDetails = useSelector(state => state.productDetails)
// const { loading, productDetail, error } = productDetails
// const product = productDetail;

const [product, setProduct] = useState({})
useEffect(() => {
const getProduct = async () => {
const { data } = await axios.get(`/api/products/${params.id}`)
setProduct(data)
}
getProduct()
}, [params.id])
dispatch(productDetailsActions(id));
}, [dispatch, id])

const productDetails = useSelector(state => state.productDetails)
const { loading, product, error } = productDetails

let loading = false;
return (
<div>
{
loading ? <h1>loading...</h1> : (
<Container fluid="md" style={{ height: "80vh" }} className="pt-2">
<Link to='/' className=' offset-md-11 px-4 py-2 btn btn-outline-dark' variant="outline-dark">Back</Link>
<Row>
<Col className="col-lg-6 col-md-12">
<Image src={product.image} alt={product.name} />
</Col>
<Col>
<Row>
<h1>{product.name}</h1>
</Row>
<Row className="mt-2">
<h5 style={{ fontSize: '15px' }}>
{product.brand} - {product.category}
</h5>
</Row>
<Row className="mt-2">
<h4 style={{ fontFamily: 'monospace', fontWeight: 'bold' }}>
Price: {product.price}
</h4>
</Row>
function () {
if (loading) return <h1>loading...</h1>
else if (error) return <h1>{error}</h1>
else return (
<Container fluid="md" style={{ height: "80vh" }} className="pt-2">
<Link to='/' className=' offset-md-11 px-4 py-2 btn btn-outline-dark' variant="outline-dark">Back</Link>
<Row>
<Col className="col-lg-6 col-md-12">
<Image src={product.image} alt={product.name} />
</Col>
<Col>
<Row>
<h1>{product.name}</h1>
</Row>
<Row className="mt-2">
<h5 style={{ fontSize: '15px' }}>
{product.brand} - {product.category}
</h5>
</Row>
<Row className="mt-2">
<h4 style={{ fontFamily: 'monospace', fontWeight: 'bold' }}>
Price: {product.price}
</h4>
</Row>

<Row className="mt-1">
<Rating value={product.rating} reviewCount={product.numReview} />
</Row>
<Row className="mt-1">
<Rating value={product.rating} reviewCount={product.numReview} />
</Row>

<Row className="mt-5">
<Row className="mt-5">

<Col>
<Button variant="outline-danger" size="lg" block="true" disabled={product.count === 0} >
<strong>Add to Cart</strong>
</Button>
</Col>
</Row>
<Col>
<Button variant="outline-danger" size="lg" block="true" disabled={product.count === 0} >
<strong>Add to Cart</strong>
</Button>
</Col>
</Row>

<Row className="mt-5">
<div className=" pr-4 ">
<h4>Details: <br /></h4>
{product.details}
</div>
</Row>
</Col>
</Row>
</Container>
)
<Row className="mt-5">
<div className=" pr-4 ">
<h4>Details: <br /></h4>
{product.details}
</div>
</Row>
</Col>
</Row>
</Container>
)
}()
}
</div>
)
Expand Down
40 changes: 40 additions & 0 deletions frontend/src/actions/productActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
PRODUCT_LIST_LOADING,
PRODUCT_LIST_SUCCESS,
PRODUCT_LIST_FAILED,

PRODUCT_DETAILS_LOADING,
PRODUCT_DETAILS_SUCCESS,
PRODUCT_DETAILS_FAILED
} from '../constants/productConsts'
import axios from 'axios'

// Product List
export const productListAction = () => async (dispatch) => {
try {
dispatch({
type: PRODUCT_LIST_LOADING,
})
const { data } = await axios.get('/api/products')
dispatch({
type: PRODUCT_LIST_SUCCESS,
payload: data
})
} catch (error) {
dispatch({
type: PRODUCT_LIST_FAILED,
payload: error.response && error.response.data.message ? error.response.data.message : error.response
})
}
}

// Product Details
export const productDetailsActions = (id) => async (dispatch) => {
try {
dispatch({ type: PRODUCT_DETAILS_LOADING })
const { data } = await axios.get(`/api/products/${id}`)
dispatch({ type: PRODUCT_DETAILS_SUCCESS, payload: data })
} catch (error) {
dispatch({ type: PRODUCT_DETAILS_FAILED, payload: error.response && error.response.data.message ? error.response.data.message : error.response })
}
}
16 changes: 0 additions & 16 deletions frontend/src/actions/productDetailsActions.js

This file was deleted.

26 changes: 0 additions & 26 deletions frontend/src/actions/productListAction.js

This file was deleted.

11 changes: 11 additions & 0 deletions frontend/src/constants/productConsts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// PRODUCT LIST CONSTANTS

export const PRODUCT_LIST_LOADING = "PRODUCT_LIST_LOADING"
export const PRODUCT_LIST_SUCCESS = "PRODUCT_LIST_SUCCESS"
export const PRODUCT_LIST_FAILED = "PRODUCT_LIST_FAILED"

// PRODUCT DETAILS CONSTANTS

export const PRODUCT_DETAILS_LOADING = "PRODUCT_DETAILS_LOADING"
export const PRODUCT_DETAILS_SUCCESS = "PRODUCT_DETAILS_SUCCESS"
export const PRODUCT_DETAILS_FAILED = "PRODUCT_DETAILS_FAILED"
3 changes: 0 additions & 3 deletions frontend/src/constants/productDetailsConst.js

This file was deleted.

3 changes: 0 additions & 3 deletions frontend/src/constants/productListConst.js

This file was deleted.

23 changes: 0 additions & 23 deletions frontend/src/reducer/productDetailsReducer..js

This file was deleted.

27 changes: 0 additions & 27 deletions frontend/src/reducer/productListReducer.js

This file was deleted.

51 changes: 51 additions & 0 deletions frontend/src/reducer/productReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
PRODUCT_LIST_LOADING,
PRODUCT_LIST_SUCCESS,
PRODUCT_LIST_FAILED,

PRODUCT_DETAILS_LOADING,
PRODUCT_DETAILS_SUCCESS,
PRODUCT_DETAILS_FAILED
} from '../constants/productConsts'

// Product List Reducer
export function productListReducer(state = { products: [] }, action) {
switch (action.type) {
case PRODUCT_LIST_LOADING:
return {
loading: true,
products: []
}
case PRODUCT_LIST_SUCCESS:
return {
loading: false,
products: action.payload,
}
case PRODUCT_LIST_FAILED:
return {
loading: false,
error: action.payload
}
default:
return state;
}
}

// Product Details Reducer
export const productDetailsReducer = (state = { product: { reviews: [] } }, action) => {
switch (action.type) {
case PRODUCT_DETAILS_LOADING: return {
loading: true,
}
case PRODUCT_DETAILS_SUCCESS: return {
loading: false,
product: action.payload
}

case PRODUCT_DETAILS_FAILED: return {
loading: false,
error: action.payload
}
default: return state;
}
}
6 changes: 3 additions & 3 deletions frontend/src/store.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createStore, applyMiddleware, combineReducers } from 'redux'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
import productListReducer from './reducer/productListReducer'
import { productDetailsReducer } from './reducer/productDetailsReducer.'
import { productListReducer, productDetailsReducer } from './reducer/productReducers'

const initialState = {}
const reducers = combineReducers({
productList: productListReducer,
productDetails: productDetailsReducer
productDetails: productDetailsReducer,
})
const middlewear = [thunk]

Expand Down