Sfoglia il codice sorgente

orderGoods is done

maryluis 3 anni fa
parent
commit
749216b878

+ 12 - 3
shop/src/App.css

@@ -186,7 +186,7 @@ content {
   display: flex;
   width: 170px;
   flex-direction: column;
-  font-size: 15px;
+  font-size: 14px;
   align-items: center;
   margin: 5px;
   padding: 25px;
@@ -200,7 +200,7 @@ content {
   height: 75px;
 }
 .forOneGoodImg {
-  height: 120px;
+  height: 170px;
 }
 .smallGoodInner {
   display: flex;
@@ -273,9 +273,12 @@ button {
 } */
 
 
-.oneGoodInner {
+/* .oneGoodInner {
   position: relative;
 }
+.oneGoodInner span{
+  position: relative;
+} */
 .basketButoons {
   display: flex;
 }
@@ -367,6 +370,9 @@ button {
   .MainImg content {
     width: 100%;
   }
+  .subCatalog .subLink li {
+    width: 80px;
+  }
   .goods {
     padding: 0%;
     background: none;
@@ -388,6 +394,9 @@ button {
     font-size: 10px;
     height: 40px;
   }
+  .forOneGoodImg {
+    height: 120px;
+  }
   .oneGood img {
     width: 80px;
   }

+ 40 - 8
shop/src/components/basket.js

@@ -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>

+ 41 - 0
shop/src/reducers/actionOrder.js

@@ -0,0 +1,41 @@
+import {actionPromise, gql} from "./index";
+
+
+
+
+
+const actionOrder = (order) => {
+
+    return async dispatch => {
+        const queryJson = JSON.stringify(
+            {
+                order: 
+                {orderGoods: order
+                    //  [
+                    // {
+                    //     "count": 2, 
+                    //     "good": {
+                    //         "_id": "5e203bb256d8f720513e6cbc" 
+                    //     }
+                    // },
+                    // {
+                    //     "count": 3, 
+                    //     "good": {
+                    //         "_id": "5dcabac96d09c45440d14cef"}
+                    // }] 
+                }}
+        )
+
+        await dispatch(actionPromise(`order`, gql(
+            `mutation newOrder($order: OrderInput) {
+                OrderUpsert(order: $order) {
+                  _id
+                  createdAt
+                  total
+                }
+              }`,  queryJson
+        )))
+    }
+}
+
+export default actionOrder;

+ 0 - 3
shop/src/reducers/cartReducer.js

@@ -59,9 +59,6 @@ function cartReducer(state, { type, count, id, image,  name, description,  price
     }
     if (type === "CART_CLEAR") {
         state = {}
-
-
-
     }
     if(!state.price){
         localStorage.removeItem("basket");

+ 1 - 1
shop/src/reducers/getGoods.js

@@ -2,7 +2,7 @@
 
 
   const getGoods = (state, key, funk) => {
-  
+  //debugger
     if(state.promiseRed[key] && state.promiseRed[key].payload) {
         return state.promiseRed[key].payload.data[funk]
     }

+ 2 - 1
shop/src/reducers/index.js

@@ -11,6 +11,7 @@ import {actionAuthLogin, actionAuthLogout} from "./actionAuthLog";
 import actionGoodCard from "./actionGoodCard";
 import updateAction from "./actionUpdateImg";
 import {cartReducer, actionCartClear, actionCartDelete, actionCartAdd} from "./cartReducer";
+import actionOrder from "./actionOrder";
 
 
 
@@ -36,7 +37,7 @@ function promiseReducer(state={}, action){
 
 export {actionPromise, gql, actionGoods, getGoods, promiseReducer, store, actionSearch,
    actionLogin, authReducer, actionAuthLogin, actionAuthLogout, urlUpload, actionGoodCard, 
-   updateAction, actionCartAdd, actionCartDelete, actionCartClear};
+   updateAction, actionCartAdd, actionCartDelete, actionCartClear, actionOrder};