Skip to content

Commit f96dd7b

Browse files
committed
orderHistory done component in basket for test
1 parent d1e8ca1 commit f96dd7b

File tree

11 files changed

+230
-36
lines changed

11 files changed

+230
-36
lines changed

src/Api/database/db.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ connection.connect(function(err) {
2626
connection.query(productTable, function(err, results) {
2727
if (err) throw err;
2828
});
29+
let basketTable =
30+
"CREATE TABLE IF NOT EXISTS basket (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,category VARCHAR(30) NOT NULL, name VARCHAR(200) NOT NULL, description VARCHAR(500) NOT NULL, prices FLOAT(10) NOT NULL, url VARCHAR(200) NOT NULL, quantity INT(10) NOT NULL,id_product INT(10) NOT NULL, id_user_affiliate INT(10) NOT NULL)";
31+
connection.query(basketTable, (err) => {
32+
if (err) throw err;
33+
});
2934
});
3035

3136
module.exports = connection;

src/Api/routes/route.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,16 @@ const appRouter = async function(app, connection) {
197197
// POST /productEdit/:id => Update this specific product from the database
198198
await app.post("/productEdit/:id", auth, (req, res) => {
199199
if (req.body.idUser === req.body.id_user_affiliate) {
200-
let sql = `UPDATE products SET category = '${req.body.category.charAt(0).toUpperCase() + req.body.category.slice(1)}', name = '${req.body.name.charAt(0).toUpperCase() + req.body.name.slice(1)}', description = '${req.body.description}', prices = '${req.body.prices}',url = '${req.body.url}' WHERE id = ${req.params.id}`;
200+
let sql = `UPDATE products SET category = '${req.body.category
201+
.charAt(0)
202+
.toUpperCase() +
203+
req.body.category.slice(1)}', name = '${req.body.name
204+
.charAt(0)
205+
.toUpperCase() + req.body.name.slice(1)}', description = '${
206+
req.body.description
207+
}', prices = '${req.body.prices}',url = '${req.body.url}' WHERE id = ${
208+
req.params.id
209+
}`;
201210
connection.query(sql, (err) => {
202211
if (err) {
203212
console.log(err);
@@ -236,5 +245,44 @@ const appRouter = async function(app, connection) {
236245
res.send("Id incorrect");
237246
}
238247
});
248+
249+
/**************************** API BASKET ***********************************/
250+
/*****************************************************************************/
251+
252+
/* Get All Order History where id_user_affiliate = */
253+
await app.get("/basketHistory/:id", (req, res) => {
254+
let getHistory = `SELECT * FROM basket where id_user_affiliate = ${req.params.id};`
255+
connection.query(getHistory, (err, results)=>{
256+
if (err) throw err
257+
res.send(results);
258+
})
259+
});
260+
261+
262+
263+
await app.post("/panier/", (req, res) => {
264+
let update = `DELETE FROM basket WHERE id_user_affiliate = ${req.body[0].id_user_affiliate}`;
265+
connection.query(update, (err) => {
266+
if (err) console.log(err);
267+
});
268+
let sql =
269+
"INSERT INTO basket (category,name,description,prices,url,quantity,id_product,id_user_affiliate) VALUES (?)";
270+
for (i = 0; i < req.body.length; i++) {
271+
let product = [
272+
req.body[i].category,
273+
req.body[i].name,
274+
req.body[i].description,
275+
req.body[i].prices,
276+
req.body[i].url,
277+
req.body[i].quantity,
278+
req.body[i].id,
279+
req.body[i].id_user_affiliate,
280+
];
281+
connection.query(sql, [product], (err) => {
282+
if (err) console.log(err);
283+
});
284+
}
285+
res.send("Stockés");
286+
});
239287
};
240288
module.exports = appRouter;

src/Components/Basket/BasketComponent.js

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import React, { Component } from "react";
22
import {
33
deleteProductFromCart,
44
increaseCounter,
5-
decreaseCounter, resetCart
5+
decreaseCounter,
66
} from "../../store/actions/cart";
77
import { connect } from "react-redux";
88
import "../../Styles/BasketComponent.css";
9+
import Buttton from "../Small/ButtonComponent";
10+
import OrderComponent from '../OrderComponent'
11+
import axios from "axios";
912
import {
1013
Card,
1114
Col,
@@ -15,21 +18,29 @@ import {
1518
Form,
1619
ListGroup,
1720
} from "react-bootstrap";
18-
1921
class BasketComponent extends Component {
2022
deleteClick(index) {
2123
this.props.deleteProductFromCart(this.props.productBasket[index]);
22-
alert(`${this.props.productBasket[index].name} has been deleted from your basket`)
24+
alert(
25+
`${this.props.productBasket[index].name} has been deleted from your basket`
26+
);
2327
}
2428

25-
increase(product){
26-
this.props.increaseCounter(product)
29+
increase(product) {
30+
this.props.increaseCounter(product);
31+
}
32+
decrease(product) {
33+
this.props.decreaseCounter(product);
2734
}
28-
decrease(product){
29-
this.props.decreaseCounter(product)
35+
storeBasket(e) {
36+
console.log(this.props.productBasket);
37+
e.preventDefault();
38+
axios.post("http://localhost:8080/panier", this.props.productBasket);
39+
setTimeout(function() {
40+
alert("Basket Validate");
41+
}, 1000);
3042
}
3143
renderProduct(product, index) {
32-
console.log(product);
3344
return (
3445
<Container className="basketCard" key={index}>
3546
<Row>
@@ -55,8 +66,18 @@ class BasketComponent extends Component {
5566
</ListGroup.Item>
5667
</ListGroup>
5768
<Form inline>
58-
<Button variant="info" onClick={this.increase.bind(this,product)}>+1</Button>
59-
<Button variant="warning" onClick={this.decrease.bind(this,product)}>-1</Button>
69+
<Button
70+
variant="info"
71+
onClick={this.increase.bind(this, product)}
72+
>
73+
+1
74+
</Button>
75+
<Button
76+
variant="warning"
77+
onClick={this.decrease.bind(this, product)}
78+
>
79+
-1
80+
</Button>
6081
<Button variant="danger">
6182
<img
6283
className="trashBasket"
@@ -74,32 +95,34 @@ class BasketComponent extends Component {
7495
}
7596

7697
render() {
77-
console.log(this.props);
78-
if(this.props.productBasket){
79-
return (
80-
<div>
81-
{this.props.productBasket.map((product, index) =>
82-
this.renderProduct(product, index)
83-
)}
84-
</div>
85-
);} else {
86-
return(
87-
<div></div>
88-
)
98+
if (this.props.productBasket.length !== 0) {
99+
return (
100+
<div>
101+
{this.props.productBasket.map((product, index) =>
102+
this.renderProduct(product, index)
103+
)}
104+
<Buttton
105+
text="Save Basket"
106+
link="/paiement"
107+
click={this.storeBasket.bind(this)}
108+
/>
109+
<OrderComponent/>
110+
</div>
111+
);
112+
} else {
113+
return <div></div>;
89114
}
90-
91115
}
92116
}
93117

94118
const mapDispatchToProps = {
95119
deleteProductFromCart,
96120
increaseCounter,
97121
decreaseCounter,
98-
resetCart
99122
};
100123

101124
const mapStateToProps = (state) => ({
102125
productBasket: state.cartReducer.productBasket,
103126
});
104127

105-
export default connect(mapStateToProps, mapDispatchToProps)(BasketComponent);
128+
export default connect(mapStateToProps, mapDispatchToProps)(BasketComponent);

src/Components/OrderComponent.css

Whitespace-only changes.

src/Components/OrderComponent.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React, { Component } from "react";
2+
import { Card, Col, Row, Button } from "react-bootstrap";
3+
import axios from "axios";
4+
import "./OrderComponent.css";
5+
import "../Styles/ListOfProduct.css";
6+
import { setHistoryOrders, resetHistoryOrders } from "../store/actions/orders";
7+
import { connect } from "react-redux";
8+
9+
class OrderComponent extends Component {
10+
componentDidMount() {
11+
axios
12+
.get(`http://localhost:8080/basketHistory/${20}`)
13+
.then((result) => {
14+
console.log("ICI", result.data);
15+
this.props.setHistoryOrders(result.data);
16+
console.log(this.props.old_Orders);
17+
})
18+
.catch((err) => {
19+
console.log(err);
20+
});
21+
}
22+
23+
ButtonIsClick() {
24+
this.props.resetHistoryOrders();
25+
}
26+
27+
renderProduct(product, index) {
28+
console.log(product);
29+
return (
30+
<div className="login-box-Edit" key={index}>
31+
<Row className="changeUrlP">
32+
<Col sm={2}>
33+
<img
34+
className="basketImg2"
35+
src={product.url}
36+
alt={product.name}
37+
></img>
38+
</Col>
39+
<Col sm={5}>
40+
<Card.Body>
41+
<Card.Title className="productNameTitle">
42+
Commande n°:{product.id}
43+
</Card.Title>
44+
<Card.Text>
45+
Category:
46+
<br />
47+
{product.name}
48+
</Card.Text>
49+
</Card.Body>
50+
</Col>
51+
<Col sm={5}>
52+
<Card.Body>
53+
<Card.Text className="productNameTitle">
54+
Price: {product.prices} $
55+
</Card.Text>
56+
<Button onClick={this.ButtonIsClick.bind(this)} />
57+
</Card.Body>
58+
</Col>
59+
</Row>
60+
</div>
61+
);
62+
}
63+
64+
render() {
65+
return (
66+
<div>
67+
{this.props.old_Orders.map((product, index) =>
68+
this.renderProduct(product, index)
69+
)}
70+
</div>
71+
);
72+
}
73+
}
74+
const mapDispatchToProps = {
75+
setHistoryOrders,
76+
resetHistoryOrders,
77+
};
78+
79+
const mapStateToProps = (state) => ({
80+
old_Orders: state.ordersReducer.old_Orders,
81+
});
82+
83+
export default connect(mapStateToProps, mapDispatchToProps)(OrderComponent);

src/Components/Product/ProductCardComponent2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ class ProductCardComponent2 extends Component {
1212
axios
1313
.get(`http://localhost:8080/products/${this.props.id}`)
1414
.then((result) => {
15+
result.data[0].id_user_affiliate = this.props.idUser
1516
this.props.setProduct(result.data);
1617
})
17-
.catch(() => {
18-
console.log("Oops, request failed!");
18+
.catch((err) => {
19+
console.log(err);
1920
});
2021
}
2122
addButtonIsClick(e) {
@@ -109,6 +110,7 @@ const mapStateToProps = (state) => ({
109110
counter: state.cartReducer.counter,
110111
product: state.productReducer.product,
111112
id: state.productReducer.id,
113+
idUser: state.userReducer.id,
112114
productBasket: state.cartReducer.productBasket,
113115
});
114116

src/Components/Router.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,49 +34,49 @@ class MyRouter extends Component {
3434
</Route>
3535
<Route path="/addProduct">
3636
{this.props.auth === false || undefined ? (
37-
<Redirect to="/sign-in" />
37+
<Redirect to="/sign-up" />
3838
) : (
3939
<CreateProductPage />
4040
)}
4141
</Route>
4242
<Route path="/Users-List">
4343
{this.props.auth === false || undefined ? (
44-
<Redirect to="/sign-in" />
44+
<Redirect to="/sign-up" />
4545
) : (
4646
<UserListComponent />
4747
)}
4848
</Route>
4949
<Route path="/editProfil">
5050
{this.props.auth === false || undefined ? (
51-
<Redirect to="/sign-in" />
51+
<Redirect to="/sign-up" />
5252
) : (
5353
<EditProfileComponent />
5454
)}
5555
</Route>
5656
<Route path="/edit-product">
5757
{this.props.auth === false || undefined ? (
58-
<Redirect to="/sign-in" />
58+
<Redirect to="/sign-up" />
5959
) : (
6060
<EditProductComponent />
6161
)}
6262
</Route>
6363
<Route path="/list-of-products">
6464
{this.props.auth === false || undefined ? (
65-
<Redirect to="/sign-in" />
65+
<Redirect to="/sign-up" />
6666
) : (
6767
<ListOfProducts />
6868
)}
6969
</Route>
7070
<Route path="/productCard2">
7171
{this.props.auth === false || undefined ? (
72-
<Redirect to="/sign-in" />
72+
<Redirect to="/sign-up" />
7373
) : (
7474
<ProductCardComponent2 />
7575
)}
7676
</Route>
7777
<Route path="/basket">
7878
{this.props.auth === false || undefined ? (
79-
<Redirect to="/sign-in" />
79+
<Redirect to="/sign-up" />
8080
) : (
8181
<BasketComponent />
8282
)}

src/Styles/BasketComponent.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ Form Button{
2626
margin-top: 20px;
2727
margin-right: 20px;
2828
}
29+
30+
31+
/* ARNAUD */

src/store/actions/orders.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const setHistoryOrders = (orders) => ({
2+
type: "SET_HISTORY_ORDERS",
3+
old_Orders: orders,
4+
});
5+
6+
export const resetHistoryOrders = () => ({
7+
type: " RESET_HYSTORY_ORDERS",
8+
});

src/store/reducer/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { combineReducers } from "redux";
22
import userReducer from "./user";
33
import productReducer from "./product";
44
import cartReducer from "./cart"
5+
import ordersReducer from './orders'
56

67
export default combineReducers({
78
userReducer,
89
productReducer,
9-
cartReducer
10+
cartReducer,
11+
ordersReducer
1012
});

0 commit comments

Comments
 (0)