Skip to content

Commit

Permalink
bug fix : place order not working
Browse files Browse the repository at this point in the history
  • Loading branch information
JaberHPranto committed Jul 9, 2021
1 parent 2b2a762 commit 622d4f9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
28 changes: 17 additions & 11 deletions client/src/components/Ecommerce/Screen/PlaceOrderScreen.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useEffect } from "react";
import { Link } from "react-router-dom";
import { Button, Row, Col, ListGroup, Image, Card } from "react-bootstrap";
import React from "react";
import { Button, Card, Col, Image, ListGroup, Row } from "react-bootstrap";
import { useDispatch, useSelector } from "react-redux";
import Message from "../Message";
import CheckoutSteps from "../CheckoutSteps";
import { Link } from "react-router-dom";
import { createOrder } from "../../../redux/actions/orderActions";
import CheckoutSteps from "../CheckoutSteps";
import Message from "../Message";
import { toastSuccessMessage } from "../ToastMessage";

const PlaceOrderScreen = ({ history }) => {
const dispatch = useDispatch();
Expand All @@ -14,14 +15,17 @@ const PlaceOrderScreen = ({ history }) => {
const orderCreate = useSelector((state) => state.orderCreate);
const { order, success, error } = orderCreate;

useEffect(() => {
if (success) {
history.push(`/order/${order._id}`);
}
// eslint-disable-next-line
}, [history, success]);
// useEffect(() => {
// if (success) {
// // history.push(`/order/${order._id}`);
// // history.push("/order")
// }
// // eslint-disable-next-line
// }, [history, success]);

const placeOrderHandler = () => {
console.log(cart.cartItems);

dispatch(
createOrder({
orderItems: cart.cartItems,
Expand All @@ -32,6 +36,8 @@ const PlaceOrderScreen = ({ history }) => {
totalPrice: cart.totalPrice,
})
);
toastSuccessMessage("You're order has been placed")

};
//Calculate Price
const addDecimals = (num) => {
Expand Down
7 changes: 3 additions & 4 deletions client/src/redux/actions/orderActions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import axios from "axios";
import {
ORDER_CREATE_SUCCESS,
ORDER_CREATE_REQUEST,
ORDER_CREATE_FAIL,
ORDER_CREATE_FAIL, ORDER_CREATE_REQUEST, ORDER_CREATE_SUCCESS
} from "../../constants/orderConstants";

import axios from "axios";

export const createOrder = (order) => async (dispatch, getState) => {
try {
Expand Down Expand Up @@ -35,6 +33,7 @@ export const createOrder = (order) => async (dispatch, getState) => {
}) */
/* localStorage.removeItem('cartItems') */
} catch (error) {
console.log(error);
dispatch({
type: ORDER_CREATE_FAIL,
payload:
Expand Down
6 changes: 2 additions & 4 deletions client/src/redux/reducers/cartReducers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
CART_ADD_ITEM,
CART_REMOVE_ITEM,
CART_SAVE_SHIPPING_ADDRESS,
CART_SAVE_PAYMENT_METHOD,
CART_REMOVE_ITEM, CART_SAVE_PAYMENT_METHOD, CART_SAVE_SHIPPING_ADDRESS
} from "../../constants/cartConstants";

export const cartReducer = (
Expand Down Expand Up @@ -45,7 +43,7 @@ export const cartReducer = (
case CART_SAVE_PAYMENT_METHOD:
return {
...state,
paymentMETHOD: action.payload,
paymentMethod: action.payload,
};
default:
return state;
Expand Down
8 changes: 6 additions & 2 deletions server/controller/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ const addOrderItems = asyncHandler(async (req, res) => {
totalPrice,
} = req.body;


if (orderItems && orderItems.length === 0) {
res.status(400);
throw new Error("No ordered items");
return;
} else {
const order = new Order({
orderItems,
user: req.user._id,
orderedItems:orderItems,
user: req.userId,
shippingAddress,
paymentMethod,
itemsPrice,
shippingPrice,
totalPrice,
});

console.log(order.orderItems);

const createdOrder = await order.save();

res.status(201).json(createdOrder);
}
});

export { addOrderItems };

1 change: 1 addition & 0 deletions server/middlewares/errorMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const notFound = (req, res, next) => {

export const errorHandler = (err, req, res, next) => {
const statusCode = res.statusCode === 200 ? 500 : res.statusCode
console.log(err);
res.status(statusCode).json({
message: err.message,
stack: process.env.NODE_ENV === 'development' ? err.stack : null
Expand Down
7 changes: 3 additions & 4 deletions server/models/orderModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const orderSchema = mongoose.Schema(
qty: { type: Number, required: true },
image: { type: String, required: true },
price: { type: Number, required: true },
product: {
countInStock:{type:Number,required:true},
productId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "Product",
Expand All @@ -23,10 +24,8 @@ const orderSchema = mongoose.Schema(
shippingAddress: {
address: { type: String, required: true },
city: { type: String, required: true },

district: { type: String, required: true },
houseNumber: { type: String, required: true },
thana: { type: String, required: true },
houseNumber: { type: String, required: true },
},

paymentMethod: {
Expand Down

0 comments on commit 622d4f9

Please sign in to comment.