Skip to content

Commit

Permalink
Merge pull request #22 from xosteve26/Review-Branch
Browse files Browse the repository at this point in the history
Subtotal Error Fix + Sort Function Implementation + Console log Statements Removal
  • Loading branch information
xosteve26 authored Jan 17, 2022
2 parents 9a70155 + a3662e5 commit c7b75b6
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 348 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const updateOrderToPaid = asyncHandler(async(req, res)=>{
"country":order.shippingAddress.country,
"pin_code":order.shippingAddress.postalCode,
"phone_no":order.phone,
"sub_total":order.orderItems.reduce((sum, item) => sum + Number(item.price), 0),
"sub_total":order.orderItems.reduce((sum, item) => sum + (Number(item.price)*Number(item.qty)), 0),
"shipping":order.shippingPrice,
"tax":order.taxPrice,
"total_price":order.totalPrice,
Expand Down
66 changes: 65 additions & 1 deletion frontend/src/actions/productActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {
PRODUCT_CATEGORY_REQUEST,
PRODUCT_CATEGORY_SUCCESS,
PRODUCT_CATEGORY_FAIL,
PRODUCT_SORT_REQUEST,
PRODUCT_SORT_SUCCESS,
PRODUCT_SORT_FAIL


} from '../constants/productConstants.js'
Expand Down Expand Up @@ -87,6 +90,67 @@ export const listProductCategory = (category, pageNumber='')=> async (dispatch)=
}
}

export const sortProducts = (sortBy, pageNumber='',category)=> async (dispatch)=>{
try{

const {data} = await axios.get(`/api/products/header/${category}?pageNumber=${pageNumber}`)
dispatch({type: PRODUCT_SORT_REQUEST})
if(sortBy === 'Price: Low to High'){
data.products.sort((a,b)=>{
return a.price - b.price
})

dispatch({type: PRODUCT_SORT_SUCCESS})
dispatch({type:PRODUCT_CATEGORY_SUCCESS, payload:data})

}

else if(sortBy === 'Price: High to Low'){
data.products.sort((a,b)=>{
return b.price - a.price
})
dispatch({type: PRODUCT_SORT_SUCCESS})
dispatch({type:PRODUCT_CATEGORY_SUCCESS, payload:data})

}
else if (sortBy === 'Best Rating'){
data.products.sort((a,b)=>{

return b.rating - a.rating
})
dispatch({type: PRODUCT_SORT_SUCCESS})
dispatch({type:PRODUCT_CATEGORY_SUCCESS, payload:data})

}
else if(sortBy === 'Newest'){
data.products.sort((a,b)=>{

const bval=(b.createdAt.slice(0,10)).replace(new RegExp('-', 'g'),'' );
const aval=(a.createdAt.slice(0,10)).replace(new RegExp('-', 'g'),'' );
return Number(bval) - Number(aval)
})
dispatch({type: PRODUCT_SORT_SUCCESS})
dispatch({type:PRODUCT_CATEGORY_SUCCESS, payload:data})

}
else if(sortBy === 'Most Popular'){
data.products.sort((a,b)=>{
return b.reviews.length - a.reviews.length
})
dispatch({type: PRODUCT_SORT_SUCCESS})
dispatch({type:PRODUCT_CATEGORY_SUCCESS, payload:data})

}
}
catch(error){
dispatch({
type:PRODUCT_SORT_FAIL,
payload:error.response && error.response.data.message ? error.response.data.message:error.message
})

}}


export const deleteProduct = (id)=> async(dispatch, getState )=>{
try{
dispatch({
Expand Down Expand Up @@ -157,7 +221,7 @@ export const updateProduct = (product)=> async(dispatch, getState )=>{
Authorization:`Bearer ${userInfo.token}`
}
}
console.log(product)

const {data} = await axios.put(`/api/products/${product._id}`,product,config)

dispatch({
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/actions/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const getUserDetails = (id)=> async(dispatch, getState )=>{
}

const {data} = await axios.get(`/api/users/${id}`, config)
console.log("GET USER DETAILS",data)



dispatch({
Expand Down Expand Up @@ -151,7 +151,7 @@ export const updateUserProfile = (user)=> async(dispatch, getState )=>{
}

const {data} = await axios.put('/api/users/profile',user, config)
console.log(data)



dispatch({
Expand Down Expand Up @@ -191,7 +191,7 @@ export const listUsers = ()=> async(dispatch, getState )=>{
}

const {data} = await axios.get('/api/users', config)
console.log(data)



dispatch({
Expand Down
13 changes: 6 additions & 7 deletions frontend/src/components/Breadcrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'

const Breadcrumbs = (props) => {
console.log(props.bc1)

return (
<nav className="flex" aria-label="Breadcrumb">
Expand All @@ -18,39 +17,39 @@ const Breadcrumbs = (props) => {

<><li>
<div className="flex items-center">
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clipRule="evenodd"></path></svg>
{props.bc2 ? <a href={`/products/header/${props.bc1}`} className="text-gray-700 hover:text-gray-900 ml-1 md:ml-2 text-sm font-medium">{props.bc1}</a> : <span className="text-gray-400 ml-1 md:ml-2 text-sm font-medium">{props.bc1}</span>}
</div>
</li><li aria-current="page">
<div className="flex items-center">
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clipRule="evenodd"></path></svg>
{props.bc2 ? <a href={`/products/header/${props.bc1}-${props.bc2}`} className="text-gray-700 hover:text-gray-900 ml-1 md:ml-2 text-sm font-medium">{props.bc2}</a> : <span className="text-gray-400 ml-1 md:ml-2 text-sm font-medium">{props.bc2}</span>}
</div>
</li>
<li aria-current="page">
<div className="flex items-center">
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clipRule="evenodd"></path></svg>
<span className="text-gray-400 ml-1 md:ml-2 text-sm font-medium">{props.bc3}</span>
</div>
</li></>
:

<><li>
<div className="flex items-center">
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clipRule="evenodd"></path></svg>
{props.bc2 ? <a href={`/products/header/${props.bc1}`} className="text-gray-700 hover:text-gray-900 ml-1 md:ml-2 text-sm font-medium">{props.bc1}</a> : <span className="text-gray-400 ml-1 md:ml-2 text-sm font-medium">{props.bc1}</span>}
</div>
</li><li aria-current="page">
<div className="flex items-center">
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clipRule="evenodd"></path></svg>
<span className="text-gray-400 ml-1 md:ml-2 text-sm font-medium">{props.bc2}</span>
</div>
</li></>

:
<li>
<div className="flex items-center">
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
<svg className="w-6 h-6 text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clipRule="evenodd"></path></svg>
{props.bc2 ? <a href={`/products/header/${props.bc1}`} className="text-gray-700 hover:text-gray-900 ml-1 md:ml-2 text-sm font-medium">{props.bc1}</a> : <span className="text-gray-400 ml-1 md:ml-2 text-sm font-medium">{props.bc1}</span>}
</div>
</li>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/CheckoutSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const CheckoutSteps = ({step1, step2, step3, step4, act1,act2, act3 }) => {
<Nav.Item className="border border-dark">
{step1 ? (
<LinkContainer to='/login'>
<Nav.Link>Sign In <i class="fas fa-check-circle"></i></Nav.Link>
<Nav.Link>Sign In <i className="fas fa-check-circle"></i></Nav.Link>
</LinkContainer>

):<Nav.Link disabled>Sign-In</Nav.Link>}
Expand All @@ -20,7 +20,7 @@ const CheckoutSteps = ({step1, step2, step3, step4, act1,act2, act3 }) => {

<LinkContainer to='/shipping'>
<Nav.Link>Shipping {act1? (<Spinner size="sm" animation="grow" />)
:act2 || act3? <i class="fas fa-check-circle"></i>
:act2 || act3? <i className="fas fa-check-circle"></i>
:''}</Nav.Link>
</LinkContainer>

Expand All @@ -31,7 +31,7 @@ const CheckoutSteps = ({step1, step2, step3, step4, act1,act2, act3 }) => {
<Nav.Item className="border border-dark">
{step3 ? (
<LinkContainer to='/payment'>
<Nav.Link>Payment {act2? (<Spinner size="sm" animation="grow" />):act3? <i class="fas fa-check-circle"></i>
<Nav.Link>Payment {act2? (<Spinner size="sm" animation="grow" />):act3? <i className="fas fa-check-circle"></i>
:'' }</Nav.Link>
</LinkContainer>

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/SearchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const SearchBox = ({history}) => {
}else{
history.push('/')
}
console.log(keyword)

}
return (
<>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/constants/productConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export const PRODUCT_TOP_FAIL = 'PRODUCT_TOP_FAIL'

export const PRODUCT_CATEGORY_REQUEST = 'PRODUCT_CATEGORY_REQUEST'
export const PRODUCT_CATEGORY_SUCCESS = 'PRODUCT_CATEGORY_SUCCESS'
export const PRODUCT_CATEGORY_FAIL = 'PRODUCT_CATEGORY_FAIL'
export const PRODUCT_CATEGORY_FAIL = 'PRODUCT_CATEGORY_FAIL'

export const PRODUCT_SORT_REQUEST = 'PRODUCT_SORT_REQUEST'
export const PRODUCT_SORT_SUCCESS = 'PRODUCT_SORT_SUCCESS'
export const PRODUCT_SORT_FAIL = 'PRODUCT_SORT_FAIL'
16 changes: 15 additions & 1 deletion frontend/src/reducers/productReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PRODUCT_LIST_REQUEST, PRODUCT_LIST_SUCCESS, PRODUCT_LIST_FAIL,
PRODUCT_UPDATE_FAIL, PRODUCT_UPDATE_RESET, PRODUCT_CREATE_REVIEW_REQUEST,
PRODUCT_CREATE_REVIEW_SUCCESS,PRODUCT_CREATE_REVIEW_FAIL,PRODUCT_CREATE_REVIEW_RESET,
PRODUCT_TOP_REQUEST,PRODUCT_TOP_SUCCESS,PRODUCT_TOP_FAIL, PRODUCT_CATEGORY_REQUEST,
PRODUCT_CATEGORY_SUCCESS, PRODUCT_CATEGORY_FAIL } from '../constants/productConstants.js'
PRODUCT_CATEGORY_SUCCESS, PRODUCT_CATEGORY_FAIL, PRODUCT_SORT_REQUEST, PRODUCT_SORT_SUCCESS, PRODUCT_SORT_FAIL } from '../constants/productConstants.js'


export const productListReducer= (state={ products:[] }, action)=>{
Expand Down Expand Up @@ -51,6 +51,20 @@ export const productCategoryReducer= (state={ products:[] }, action)=>{

}

export const productSortReducer=(state={ products:[] }, action)=>{
switch(action.type){
case PRODUCT_SORT_REQUEST:
return { loading:true, ...state}
case PRODUCT_SORT_SUCCESS:
return { loading:false}
case PRODUCT_SORT_FAIL:
return { loading:false, error:action.payload }
default:
return state
}

}


export const productDeleteReducer= (state={}, action)=>{
switch(action.type){
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/CartScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const CartScreen = (props) => {

const cart = useSelector(state=> state.cart)
const {cartItems} = cart
console.log(cartItems)

useEffect(() =>{
if(productId){
dispatch(addToCart(productId,qty))
Expand Down
Loading

0 comments on commit c7b75b6

Please sign in to comment.