Skip to content

Commit

Permalink
admin can view all the orders
Browse files Browse the repository at this point in the history
  • Loading branch information
JaberHPranto committed Jul 13, 2021
1 parent 69d8263 commit d45ac03
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 13 deletions.
69 changes: 69 additions & 0 deletions client/src/components/Ecommerce/Screen/OrderListScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { useEffect } from 'react'
import { Button, Table } from 'react-bootstrap'
import { useDispatch, useSelector } from 'react-redux'
import { LinkContainer } from 'react-router-bootstrap'
import { getOrderList } from '../../../redux/actions/orderActions'
import Loader from '../Loader'
import Message from '../Message'

function OrderListScreen({history}) {

const dispatch = useDispatch()
const { userInfo: { user } } = useSelector(state => state.userLogin)


const orderList = useSelector(state => state.orderList)
const { loading, error, orders } = orderList

useEffect(() => {
if (user && user.isAdmin) {
dispatch(getOrderList())
} else {
history.push("/login")
}
}, [dispatch,history,user])


return (
<>
{loading ? <Loader /> : error ? <Message variant='danger'>{error}</Message> : (
<>
<h1>All Users</h1>
<Table striped bordered hover>
<thead>
<tr>
<th>USER</th>
<th>DATE</th>
<th>TOTAL</th>
<th>PAID</th>
<th>Delivered</th>
</tr>
</thead>
<tbody>
{orders.map(order=>(
<tr key={order._id}>
<td>{order.user && order.user.name}</td>
<td>{order.createdAt.substring(0, 10)}</td>
<td><span style={{fontSize:'1.5rem',marginRight:'0.1rem'}}></span>&nbsp;{order.totalPrice}</td>
<td>
{order.isPaid ? order.paidAt.substring(0,10) : <Button variant='light' ><i className='fas fa-times' style={{color:'red'}}></i></Button>}
</td>
<td>
{order.isDelivered ? order.deliveredAt.substring(0,10) : <Button variant='light' ><i className='fas fa-times' style={{color:'red'}}></i></Button>}
</td>
<td>
<LinkContainer to={`/order/${order._id}`}>
<Button >Details</Button>
</LinkContainer>
</td>
</tr>
))}
</tbody>
</Table>
</>
)}
</>
)
}

export default OrderListScreen
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Loader from '../Loader'
import Message from '../Message'
import { toastErrorMessage } from '../ToastMessage'

function UserList({history}) {
function UserListScreen({history}) {

const dispatch = useDispatch()
const userList = useSelector(state => state.userList)
Expand Down Expand Up @@ -78,4 +78,4 @@ function UserList({history}) {
)
}

export default UserList
export default UserListScreen
4 changes: 4 additions & 0 deletions client/src/constants/orderConstants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const ORDER_CREATE_REQUEST = "ORDER_CREATE_REQUEST";
export const ORDER_CREATE_SUCCESS = "ORDER_CREATE_SUCCESS";
export const ORDER_CREATE_FAIL = "ORDER_CREATE_FAIL";

export const ORDER_LIST_REQUEST = "ORDER_LIST_REQUEST";
export const ORDER_LIST_SUCCESS = "ORDER_LIST_SUCCESS";
export const ORDER_LIST_FAIL = "ORDER_LIST_FAIL";
6 changes: 4 additions & 2 deletions client/src/pages/EcommercePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CartScreen from '../components/Ecommerce/Screen/CartScreen';
import ForgetPasswordScreen from '../components/Ecommerce/Screen/ForgetPasswordScreen';
import HomeScreen from '../components/Ecommerce/Screen/HomeScreen';
import LoginScreen from '../components/Ecommerce/Screen/LoginScreen';
import OrderListScreen from "../components/Ecommerce/Screen/OrderListScreen";
import PasswordResetScreen from '../components/Ecommerce/Screen/PasswordResetScreen';
import PaymentScreen from "../components/Ecommerce/Screen/PaymentScreen";
import PlaceOrderScreen from "../components/Ecommerce/Screen/PlaceOrderScreen";
Expand All @@ -16,7 +17,7 @@ import ProductScreen from '../components/Ecommerce/Screen/ProductScreen';
import ProfileScreen from '../components/Ecommerce/Screen/ProfileScreen';
import RegisterScreen from '../components/Ecommerce/Screen/RegisterScreen';
import ShippingScreen from "../components/Ecommerce/Screen/ShippingScreen";
import UserList from "../components/Ecommerce/Screen/UserList";
import UserListScreen from "../components/Ecommerce/Screen/UserListScreen";
import '../styles/ecommerce.css';

function EcommercePage() {
Expand All @@ -34,8 +35,9 @@ function EcommercePage() {
<Route path="/forget-password" component={ForgetPasswordScreen} />
<Route path="/reset-password/:resetToken" component={PasswordResetScreen} />
<Route path="/profile" component={ProfileScreen} />
<Route path="/admin/userlist" component={UserList} />
<Route path="/admin/userlist" component={UserListScreen} />
<Route path="/admin/productlist" component={ProductListScreen} />
<Route path="/admin/orderlist" component={OrderListScreen} />
<Route path="/admin/product/:id/edit" component={ProductEditScreen} />
<Route path="/product/:id" component={ProductScreen} />
<Route path="/cart/:id?" component={CartScreen} />
Expand Down
39 changes: 38 additions & 1 deletion client/src/redux/actions/orderActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import {
ORDER_CREATE_FAIL, ORDER_CREATE_REQUEST, ORDER_CREATE_SUCCESS
ORDER_CREATE_FAIL, ORDER_CREATE_REQUEST, ORDER_CREATE_SUCCESS, ORDER_LIST_FAIL, ORDER_LIST_REQUEST, ORDER_LIST_SUCCESS
} from "../../constants/orderConstants";


Expand Down Expand Up @@ -50,3 +50,40 @@ export const createOrder = (order) => async (dispatch, getState) => {
}) */
}
};


export const getOrderList = () => async (dispatch,getState) => {

try {
dispatch({
type:ORDER_LIST_REQUEST
})

const { userLogin: { userInfo } } = getState()


const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userInfo.token}`
}
}

const { data } = await axios.get(`/api/orders`,config)

dispatch({
type: ORDER_LIST_SUCCESS,
payload:data
})

} catch (error) {
console.log(error);
dispatch({
type: ORDER_LIST_FAIL,
payload: error.response && error.response.data.message ? error.response.data.message : error.message

})

}

}
21 changes: 18 additions & 3 deletions client/src/redux/reducers/orderReducers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
ORDER_CREATE_SUCCESS,
ORDER_CREATE_REQUEST,
ORDER_CREATE_FAIL,
ORDER_CREATE_FAIL, ORDER_CREATE_REQUEST, ORDER_CREATE_SUCCESS, ORDER_LIST_FAIL, ORDER_LIST_REQUEST, ORDER_LIST_SUCCESS
} from "../../constants/orderConstants";

export const orderCreateReducer = (state = {}, action) => {
Expand Down Expand Up @@ -29,3 +27,20 @@ export const orderCreateReducer = (state = {}, action) => {
return state */
}
};

export const orderListReducer = (state = { orders: [] }, action) => {
switch (action.type) {
case ORDER_LIST_REQUEST:
return { ...state,loading: true }

case ORDER_LIST_SUCCESS:
return { loading: false, orders: action.payload }

case ORDER_LIST_FAIL:
return { loading: false, error: action.payload }


default:
return state
}
}
6 changes: 5 additions & 1 deletion client/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { applyMiddleware, combineReducers, createStore } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import thunk from "redux-thunk";
import { cartReducer } from "./redux/reducers/cartReducers";
import { orderCreateReducer } from "./redux/reducers/orderReducers";
import {
orderCreateReducer, orderListReducer
} from "./redux/reducers/orderReducers";
import {
productCreateReducer, productCreateReviewReducer, productDeleteReducer, productDetailsReducer,
productListReducer, productUpdateReducer
Expand All @@ -13,6 +15,7 @@ import {
userRegisterReducer
} from "./redux/reducers/userReducers";


const reducer = combineReducers({
productList: productListReducer,
productDetails: productDetailsReducer,
Expand All @@ -28,6 +31,7 @@ const reducer = combineReducers({
userDelete: userDeleteReducer,
cart: cartReducer,
orderCreate: orderCreateReducer,
orderList: orderListReducer
});

const cartItemsFromStorage = localStorage.getItem("cartItems")
Expand Down
10 changes: 9 additions & 1 deletion server/controller/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@ const addOrderItems = asyncHandler(async (req, res) => {
}
});

export { addOrderItems };
// @desc get all orders
// @route POST /api/orders
// @access Private/Admin
const getOrders = asyncHandler(async (req, res) => {
const orders = await Order.find({}).populate('user', '_id name')
res.json(orders)
})

export { addOrderItems, getOrders };

5 changes: 3 additions & 2 deletions server/routes/orderRoutes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import express from "express";
import { addOrderItems } from "../controller/orderController.js";
import { isLoggedIn } from "../middlewares/authMiddleware.js";
import { addOrderItems, getOrders } from "../controller/orderController.js";
import { isAdmin, isLoggedIn } from "../middlewares/authMiddleware.js";
const router = express.Router();

router.route("/").post(isLoggedIn, addOrderItems);
router.route("/").get(isLoggedIn,isAdmin,getOrders);

export default router;
2 changes: 1 addition & 1 deletion server/routes/uploadRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const checkFileType = (file, cb) => {
if (extName && mimetype) {
cb(null,true)
} else {
cb('Images Only !')
cb(new Error("Image Only !",false))
}
}

Expand Down

0 comments on commit d45ac03

Please sign in to comment.