Skip to content

Commit 7688fbb

Browse files
authored
Video-31-Display-Orders-History (basir#31)
1 parent 36bb281 commit 7688fbb

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,11 @@ JS AMAZONA
263263
8. create payOrder in api.js
264264
9. create route for /:id/pay in orderRouter.js
265265
10. rerender after pay order
266-
31. User Order History
267-
1. Create order history api
268-
2. Show orders in profile screen
266+
31. Display Orders History
267+
1. create customer orders api
268+
2. create api for getMyOrders
269+
3. show orders in profile screen
270+
4. style orders
269271
32. Admin Products
270272
1. create Admin Order menu in header
271273
2. create ProductListScreen.js

backend/routers/orderRouter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { isAuth } from '../utils';
44
import Order from '../models/orderModel';
55

66
const orderRouter = express.Router();
7+
orderRouter.get(
8+
'/mine',
9+
isAuth,
10+
expressAsyncHandler(async (req, res) => {
11+
const orders = await Order.find({ user: req.user._id });
12+
res.send(orders);
13+
})
14+
);
715
orderRouter.get(
816
'/:id',
917
isAuth,

frontend/src/api.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,24 @@ export const getOrder = async (id) => {
128128
return { error: err.message };
129129
}
130130
};
131-
131+
export const getMyOrders = async () => {
132+
try {
133+
const { token } = getUserInfo();
134+
const response = await axios({
135+
url: `${apiUrl}/api/orders/mine`,
136+
headers: {
137+
'Content-Type': 'application/json',
138+
Authorization: `Bearer ${token}`,
139+
},
140+
});
141+
if (response.statusText !== 'OK') {
142+
throw new Error(response.data.message);
143+
}
144+
return response.data;
145+
} catch (err) {
146+
return { error: err.response ? err.response.data.message : err.message };
147+
}
148+
};
132149
export const getPaypalClientId = async () => {
133150
const response = await axios({
134151
url: `${apiUrl}/api/paypal/clientId`,

frontend/src/srceens/ProfileScreen.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { update } from '../api';
1+
import { update, getMyOrders } from '../api';
22
import { getUserInfo, setUserInfo, clearUser } from '../localStorage';
33
import { showLoading, hideLoading, showMessage } from '../utils';
44

@@ -27,13 +27,16 @@ const ProfileScreen = {
2727
}
2828
});
2929
},
30-
render: () => {
30+
render: async () => {
3131
const { name, email } = getUserInfo();
3232
if (!name) {
3333
document.location.hash = '/';
3434
}
35+
const orders = await getMyOrders();
3536
return `
36-
<div class="form-container">
37+
<div class="content profile">
38+
<div class="profile-info">
39+
<div class="form-container">
3740
<form id="profile-form">
3841
<ul class="form-items">
3942
<li>
@@ -56,11 +59,50 @@ const ProfileScreen = {
5659
</li>
5760
<li>
5861
<button type="button" id="signout-button" >Sign Out</button>
59-
</li>
60-
62+
</li>
6163
</ul>
6264
</form>
6365
</div>
66+
</div>
67+
<div class="profile-orders">
68+
<h2>Order History</h2>
69+
<table>
70+
<thead>
71+
<tr>
72+
<th>ORDER ID</th>
73+
<th>DATE</th>
74+
<th>TOTAL</th>
75+
<th>PAID</th>
76+
<th>DELIVERED</th>
77+
<th>ACTIONS</th>
78+
</tr>
79+
</thead>
80+
<tbody>
81+
${
82+
orders.length === 0
83+
? `<tr><td colspan="6">No Order Found.</tr>`
84+
: orders
85+
.map(
86+
(order) => `
87+
<tr>
88+
<td>${order._id}</td>
89+
<td>${order.createdAt}</td>
90+
<td>${order.totalPrice}</td>
91+
<td>${order.paidAt || 'No'}</td>
92+
<td>${order.deliveryAt || 'No'}</td>
93+
<td><a href="/#/order/${order._id}">DETIALS</a> </td>
94+
</tr>
95+
`
96+
)
97+
.join('\n')
98+
}
99+
</tbody>
100+
</table>
101+
</div>
102+
</div>
103+
104+
105+
64106
`;
65107
},
66108
};

frontend/style.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,29 @@ footer {
346346
font-weight: bold;
347347
color: #c04000;
348348
}
349+
/* Profile */
350+
.profile {
351+
display: flex;
352+
flex-wrap: wrap;
353+
align-items: flex-start;
354+
}
355+
.profile-info {
356+
flex: 1 1 20rem;
357+
}
358+
.profile-orders {
359+
flex: 3 1 60rem;
360+
margin-left: 1rem;
361+
}
362+
363+
table {
364+
width: 100%;
365+
}
366+
th {
367+
text-align: left;
368+
}
369+
tbody > tr:nth-child(odd) {
370+
background-color: #f0f0f0;
371+
}
372+
td {
373+
padding: 0.5rem;
374+
}

0 commit comments

Comments
 (0)