'Display mongoDB data in a table in React in Chronological Order
I have a table in react created from data stored in mongodb. Currently, my table of past orders are displayed with the most recent date at the bottom of the screen. I would like to have my data displayed in chronological order. I would really appreciate any help or advice on how to do this. Thank you!
OrderHistoryScreen.js
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { listOrderMine, detailsOrder } from '../actions/orderActions';
import LoadingBox from '../components/LoadingBox';
import MessageBox from '../components/MessageBox';
import { createReturn } from '../actions/returnActions';
export default function OrderHistoryScreen(props) {
const order = useSelector((state) => state.order);
const orderMineList = useSelector((state) => state.orderMineList);
const { loading, error, orders } = orderMineList;
console.log({ orders });
const dispatch = useDispatch();
useEffect(() => {
dispatch(listOrderMine());
//dispatch(detailsOrder(orderId));
}, [dispatch]);
return (
<div>
<h1>Order History</h1>
{loading ? (
<LoadingBox></LoadingBox>
) : error ? (
<MessageBox variant="danger">{error}</MessageBox>
) : (
<table className="table">
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>TOTAL</th>
<th>PAID</th>
<th>DELIVERED</th>
<th>SHIPPED</th>
<th>ACTIONS</th>
</tr>
</thead>
<tbody>
{orders.map((order) => {
return (
<tr key={order._id}>
<td>{order._id}</td>
<td>{order.createdAt.substring(0, 10)}</td>
<td>
<button
type="button"
className="small"
onClick={() => {
props.history.push(`/order/${order._id}`);
}}
>
Details
</button>
</td>
</tr>
)
})}
</tbody>
</table>
)}
</div>
);
}
OrderAction.js
import Axios from 'axios';
import { CART_EMPTY } from '../constants/cartConstants';
import {
ORDER_CREATE_FAIL,
ORDER_CREATE_REQUEST,
ORDER_CREATE_SUCCESS,
ORDER_DETAILS_FAIL,
ORDER_DETAILS_REQUEST,
ORDER_DETAILS_SUCCESS,
ORDER_MINE_LIST_REQUEST,
ORDER_MINE_LIST_FAIL,
ORDER_MINE_LIST_SUCCESS,
ORDER_LIST_REQUEST,
ORDER_LIST_SUCCESS,
ORDER_LIST_FAIL,
ORDER_DELETE_REQUEST,
ORDER_DELETE_SUCCESS,
ORDER_DELETE_FAIL,
} from '../constants/orderConstants';
import { BASE_URL } from '../constants/app.constants';
export const createOrder = (order) => async (dispatch, getState) => {
dispatch({ type: ORDER_CREATE_REQUEST, payload: order });
try {
const {
userSignin: { userInfo },
} = getState();
const { data } = await Axios.post('/api/orders', order, {
headers: {
Authorization: `Bearer ${userInfo.token}`,
},
});
dispatch({ type: ORDER_CREATE_SUCCESS, payload: data.order });
dispatch({ type: CART_EMPTY });
localStorage.removeItem('cartItems');
} catch (error) {
dispatch({
type: ORDER_CREATE_FAIL,
payload: error.response && error.response.data.message ? error.response.data.message : error.message,
});
}
};
export const detailsOrder = (orderId) => async (dispatch, getState) => {
dispatch({ type: ORDER_DETAILS_REQUEST, payload: orderId });
const {
userSignin: { userInfo },
} = getState();
try {
const { data } = await Axios.get(`/api/orders/${orderId}`, {
headers: { Authorization: `Bearer ${userInfo.token}` },
});
dispatch({ type: ORDER_DETAILS_SUCCESS, payload: data });
} catch (error) {
const message = error.response && error.response.data.message ? error.response.data.message : error.message;
dispatch({ type: ORDER_DETAILS_FAIL, payload: message });
}
};
export const listOrderMine = () => async (dispatch, getState) => {
dispatch({ type: ORDER_MINE_LIST_REQUEST });
const {
userSignin: { userInfo },
} = getState();
try {
const { data } = await Axios.get(`${BASE_URL}/api/orders/mine`, {
headers: {
Authorization: `Bearer ${userInfo.token}`,
},
});
dispatch({ type: ORDER_MINE_LIST_SUCCESS, payload: data });
} catch (error) {
const message = error.response && error.response.data.message ? error.response.data.message : error.message;
dispatch({ type: ORDER_MINE_LIST_FAIL, payload: message });
}
};
export const listOrders =
({ seller = '' }) =>
async (dispatch, getState) => {
dispatch({ type: ORDER_LIST_REQUEST });
const {
userSignin: { userInfo },
} = getState();
try {
const { data } = await Axios.get(`/api/orders?seller=${seller}`, {
headers: { Authorization: `Bearer ${userInfo.token}` },
});
console.log(data);
dispatch({ type: ORDER_LIST_SUCCESS, payload: data });
} catch (error) {
const message = error.response && error.response.data.message ? error.response.data.message : error.message;
dispatch({ type: ORDER_LIST_FAIL, payload: message });
}
};
export const deleteOrder = (orderId) => async (dispatch, getState) => {
dispatch({ type: ORDER_DELETE_REQUEST, payload: orderId });
const {
userSignin: { userInfo },
} = getState();
try {
const { data } = Axios.delete(`/api/orders/${orderId}`, {
headers: { Authorization: `Bearer ${userInfo.token}` },
});
dispatch({ type: ORDER_DELETE_SUCCESS, payload: data });
} catch (error) {
const message = error.response && error.response.data.message ? error.response.data.message : error.message;
dispatch({ type: ORDER_DELETE_FAIL, payload: message });
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
