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

Add to cart back #58

Merged
merged 5 commits into from
Sep 11, 2019
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
97 changes: 50 additions & 47 deletions client/src/components/Product_card/index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@

import React, { Component } from 'react'
import './style.css'
import axios from 'axios';
import axios from 'axios'
import { Link } from 'react-router-dom'


class ProductCard extends React.Component {
state = {

products: [],
loading:true,
loading: true,
path1:"/shop"
}

componentDidMount() {
if(this.props.match.params.id)
{
axios.get(`/shop/${this.props.match.params.id}`)
.then( ({data}) => {
this.setState({products:data,loading:false})
})}
else{
axios.get("/shop")
.then( ({data}) => {
this.setState({products:data,loading:false})

this.setState({path1:this.props.path})
if (this.props.match.params.id) {
axios.get(`/shop/${this.props.match.params.id}`).then(({ data }) => {
this.setState({ products: data, loading: false })
})
} else {
axios.get('/shop').then(({ data }) => {
this.setState({ products: data, loading: false })
})
}
}
searchedProducts = (products)=>{
this.setState({products:products,loading:false})

searchedProducts = products => {
this.setState({ products: products, loading: false })
}

render () {
const {products,loading,searchedProducts} = this.state
const myProducts = this.props.resultAfterSerch ? this.props.resultAfterSerch:products
render() {
const { products, loading, searchedProducts } = this.state
const myProducts = this.props.resultAfterSerch
? this.props.resultAfterSerch
: products
return (
<>
{!loading ? myProducts.length>0 ?(

myProducts.map(e=>(
<Link to={ '/product/' + e.id }><div className="cards">
<div className="product-card-back" >
<div className="product-card" >
<img className="product-img" src={e.img} alt="product-img"/>
<p className="product-name">{e.name}</p>
<p className="product-price">₪ {e.price}</p>
</div>
</div>
</div>
</Link>
))) : (<h1>There is no products</h1>
):(
<img
className="loading"
src="https://media1.tenor.com/images/556e9ff845b7dd0c62dcdbbb00babb4b/tenor.gif?itemid=5300368"
alt="loading"
/>

)}
</>
{!loading ? (
myProducts.length > 0 ? (
myProducts.map(e => (
<Link to={'/product/' + e.id + this.state.path1} key = {e.id} >
<div className="cards">
<div className="product-card-back">
<div className="product-card">
<img
className="product-img"
src={e.img}
alt="product-img"
/>
<p className="product-name">{e.name}</p>
<p className="product-price">₪ {e.price}</p>
</div>
</div>
</div>
</Link>
))
) : (
<h1>There is no products</h1>
)
) : (
<img
className="loading"
src="https://media1.tenor.com/images/556e9ff845b7dd0c62dcdbbb00babb4b/tenor.gif?itemid=5300368"
alt="loading"
/>
)}
</>
)
}
}

export default ProductCard

18 changes: 16 additions & 2 deletions client/src/components/Product_page/Add_quantity_cart/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react'
import axios from 'axios'
import './style.css'
import axios from 'axios'
import swal from 'sweetalert'

class AddQuantityCart extends Component {
state = {
Expand All @@ -16,9 +17,22 @@ class AddQuantityCart extends Component {
axios
.post('/add-to-cart', productInfo)
.then(response => {
console.log(response, 'done, added to cart')
swal({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if the response in the backend first before calling the swal message

title: 'added to cart',
icon: 'success',
button: 'Continue shoping'
}).then(res => {
window.location.href = `${this.props.prePath}`
})
})
.catch(error => {
swal({
title: 'err add to cart',
icon: 'error',
button: 'return to shoping'
}).then(res => {
window.location.href = `${this.props.prePath}`
})
console.log(error, 'components/Product_page/Add_quantity_cart')
})
}
Expand Down
20 changes: 14 additions & 6 deletions client/src/components/Product_page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import ProductInfo from '../Product_page/product_info'
import AddQuantityCart from '../Product_page/Add_quantity_cart'

class ProductPage extends Component {
state = {price:0}
state = { price: 0 }

setPrice=(newPrice)=>{
this.setState({price:newPrice})
setPrice = newPrice => {
this.setState({ price: newPrice })
}
render () {
render() {
return (
<>
<NavBar />
<ProductInfo id={this.props.match.params.id} price={this.state.price} setPrice={this.setPrice} />
<AddQuantityCart id={this.props.match.params.id } price={this.state.price} />
<ProductInfo
id={this.props.match.params.id}
price={this.state.price}
setPrice={this.setPrice}
/>
<AddQuantityCart
id={this.props.match.params.id}
prePath={this.props.location.pathname.split(this.props.match.url)[1]}
price={this.state.price}
/>
</>
)
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Shop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Shop extends Component {
trigger={this.state.afterSearchTrigger}
serchArray={this.searchSetArray}
/>
<ProductCard resultAfterSerch={resultProducts} {...this.props} />
<ProductCard resultAfterSerch={resultProducts} path={this.props.location.pathname} {...this.props} />
</div>
)
}
Expand Down
7 changes: 5 additions & 2 deletions src/controller/add_to_cart.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const addToCart = require('../database/queries/add_to_cart')

exports.post = (req, res) => {
exports.post = (req, res, next) => {
const { productID, quantity, price } = req.body
const userID = 1 // user id static we will get user id from cookies
addToCart(userID, productID, quantity, price)
.then(result => {
res.send(JSON.stringify({ product: result }))
})
.catch(err => console.log(err, 'add_to_cart.js'))
.catch(err => {
console.log(err, 'controller/add_to_cart.js')
next(err)
})
}