-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38c8c1c
commit 2b2a762
Showing
10 changed files
with
182 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ORDER_CREATE_REQUEST = "ORDER_CREATE_REQUEST"; | ||
export const ORDER_CREATE_SUCCESS = "ORDER_CREATE_SUCCESS"; | ||
export const ORDER_CREATE_FAIL = "ORDER_CREATE_FAIL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
ORDER_CREATE_SUCCESS, | ||
ORDER_CREATE_REQUEST, | ||
ORDER_CREATE_FAIL, | ||
} from "../../constants/orderConstants"; | ||
|
||
import axios from "axios"; | ||
|
||
export const createOrder = (order) => async (dispatch, getState) => { | ||
try { | ||
dispatch({ | ||
type: ORDER_CREATE_REQUEST, | ||
}); | ||
|
||
const { | ||
userLogin: { userInfo }, | ||
} = getState(); | ||
|
||
const config = { | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${userInfo.token}`, | ||
}, | ||
}; | ||
|
||
const { data } = await axios.post(`/api/orders`, order, config); | ||
|
||
dispatch({ | ||
type: ORDER_CREATE_SUCCESS, | ||
payload: data, | ||
}); | ||
/* dispatch({ | ||
type: CART_CLEAR_ITEMS, | ||
payload: data, | ||
}) */ | ||
/* localStorage.removeItem('cartItems') */ | ||
} catch (error) { | ||
dispatch({ | ||
type: ORDER_CREATE_FAIL, | ||
payload: | ||
error.response && error.response.data.message | ||
? error.response.data.message | ||
: error.message, | ||
}); | ||
/* if (message === 'Not authorized, token failed') { | ||
dispatch(logout()) | ||
} | ||
dispatch({ | ||
type: ORDER_CREATE_FAIL, | ||
payload: message, | ||
}) */ | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
ORDER_CREATE_SUCCESS, | ||
ORDER_CREATE_REQUEST, | ||
ORDER_CREATE_FAIL, | ||
} from "../../constants/orderConstants"; | ||
|
||
export const orderCreateReducer = (state = {}, action) => { | ||
switch (action.type) { | ||
case ORDER_CREATE_REQUEST: | ||
return { | ||
loading: true, | ||
}; | ||
case ORDER_CREATE_SUCCESS: | ||
return { | ||
loading: false, | ||
success: true, | ||
order: action.payload, | ||
}; | ||
case ORDER_CREATE_FAIL: | ||
return { | ||
loading: false, | ||
error: action.payload, | ||
}; | ||
default: | ||
return state; | ||
/* case ORDER_CREATE_RESET: | ||
return {} | ||
default: | ||
return state */ | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import asyncHandler from "express-async-handler"; | ||
import Order from "../models/orderModel.js"; | ||
|
||
// @desc Create new order | ||
// @route POST /api/orders | ||
// @access Private | ||
const addOrderItems = asyncHandler(async (req, res) => { | ||
const { | ||
orderItems, | ||
shippingAddress, | ||
paymentMethod, | ||
itemsPrice, | ||
shippingPrice, | ||
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, | ||
shippingAddress, | ||
paymentMethod, | ||
itemsPrice, | ||
shippingPrice, | ||
totalPrice, | ||
}); | ||
|
||
const createdOrder = await order.save(); | ||
|
||
res.status(201).json(createdOrder); | ||
} | ||
}); | ||
|
||
export { addOrderItems }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from "express"; | ||
import { addOrderItems } from "../controller/orderController.js"; | ||
import { isLoggedIn } from "../middlewares/authMiddleware.js"; | ||
const router = express.Router(); | ||
|
||
router.route("/").post(isLoggedIn, addOrderItems); | ||
|
||
export default router; |