|
@@ -1,8 +1,10 @@
|
|
|
-import { useEffect } from "react";
|
|
|
+import { useEffect, useState } from "react";
|
|
|
import { connect } from "react-redux";
|
|
|
import{OneGood} from "../components";
|
|
|
import {createStore, combineReducers, applyMiddleware, bindActionCreators} from 'redux';
|
|
|
-import {gql, urlUpload, actionPromise, actionGoodCard, getGoods, actionGoods, actionCartAdd, actionCartDelete, actionCartClear} from "../reducers";
|
|
|
+import {gql, urlUpload, actionPromise, actionGoodCard, getGoods, actionGoods,
|
|
|
+ actionCartAdd, actionCartDelete, actionCartClear, actionOrder} from "../reducers";
|
|
|
+import { useHistory } from "react-router-dom";
|
|
|
|
|
|
|
|
|
|
|
@@ -25,22 +27,40 @@ function arrFromObj(obj) {
|
|
|
return newArr;
|
|
|
}
|
|
|
|
|
|
+function orderArr(goods) {
|
|
|
+ var orderGoods = [];
|
|
|
+ for(let key of goods){
|
|
|
+ let oneGood = {};
|
|
|
+ oneGood.count = key.count;
|
|
|
+ oneGood.good = {};
|
|
|
+ oneGood.good._id = key.id;
|
|
|
+ orderGoods.push(oneGood)
|
|
|
+ }
|
|
|
+ return orderGoods
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
state: state,
|
|
|
basket: state.basket,
|
|
|
GoodsArr: arrFromObj(ObjFilter(state.basket, "price")),
|
|
|
+ order: orderArr(arrFromObj(ObjFilter(state.basket, "price")))
|
|
|
+
|
|
|
});
|
|
|
+
|
|
|
+
|
|
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators({
|
|
|
onAdd: actionCartAdd,
|
|
|
onDel: actionCartDelete,
|
|
|
- onClear: actionCartClear
|
|
|
+ onClear: actionCartClear,
|
|
|
+ onOrder: actionOrder
|
|
|
}, dispatch);
|
|
|
+
|
|
|
|
|
|
+const BasketPage = ({state, orderDeal, basket, GoodsArr, onAdd, onDel, onClear, onOrder, order}) => {
|
|
|
+ const history = useHistory();
|
|
|
|
|
|
-const BasketPage = ({ basket, GoodsArr, onAdd, onDel, onClear}) => {
|
|
|
-
|
|
|
if(!basket.price){
|
|
|
return(
|
|
|
<div>Зайдіть у каталог щоб замовити, та повертайтесь</div>
|
|
@@ -54,9 +74,11 @@ const BasketPage = ({ basket, GoodsArr, onAdd, onDel, onClear}) => {
|
|
|
<div className = "oneGood"><OneGood key = {good._id} id = {good.id}
|
|
|
name = {good.name} price = {good.price} image = {good.image ? `${urlUpload}/${good.image[0].url}` : `https://images.ua.prom.st/2259265311_korobka-syurpriz-dlya.jpg`}/>
|
|
|
<div className = "basketButoons">
|
|
|
- <button onClick = {() => onAdd(good.name, good.price, good.id, good.description, good.image)}>+</button>
|
|
|
+ <button onClick = {
|
|
|
+ () => onAdd(good.name, good.price, good.id, good.description, good.image)}>+</button>
|
|
|
<h4>{good.count}</h4>
|
|
|
- <button onClick = {() => onDel(good.name, good.price, good.id, good.description, good.image)}>-</button>
|
|
|
+ <button onClick = {
|
|
|
+ () => onDel(good.name, good.price, good.id, good.description, good.image)}>-</button>
|
|
|
</div>
|
|
|
|
|
|
|
|
@@ -64,7 +86,17 @@ const BasketPage = ({ basket, GoodsArr, onAdd, onDel, onClear}) => {
|
|
|
)}
|
|
|
</div>
|
|
|
<div className = "basketBottom">
|
|
|
- <button>Замовити</button>
|
|
|
+ <button onClick = {() => {
|
|
|
+ if(localStorage.authToken) {
|
|
|
+ onOrder(order);
|
|
|
+ onClear(basket);
|
|
|
+ history.push("./well")
|
|
|
+ } else{
|
|
|
+ history.push("./login")
|
|
|
+ }
|
|
|
+ }}>
|
|
|
+ Замовити
|
|
|
+ </button>
|
|
|
<button onClick = {() => onClear(basket)}>ТаНуНафіг</button>
|
|
|
</div>
|
|
|
</div>
|