Skip to content

Commit dbe0ead

Browse files
authored
Video-33-Admin-Products-UI (#33)
1 parent 24bed6b commit dbe0ead

6 files changed

Lines changed: 97 additions & 18 deletions

File tree

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,32 +294,36 @@ JS AMAZONA
294294
1. update ProductListScreen.js
295295
2. handle delete button
296296
3. rerender after deletion
297-
37. Admin Orders
297+
37. Show Categories In Sidebar Menu
298+
1. update ProductListScreen.js
299+
2. handle delete button
300+
3. rerender after deletion
301+
38. Admin Orders
298302
1. create Admin Order menu in header
299303
2. create AdminOrder.js
300304
3. load orders from backend
301305
4. list them in the screen
302306
5. show delete and edit button
303307
6. redirect to order details on edit action
304-
38. Edit Order
308+
39. Edit Order
305309
1. if order is payed show deliver button for admin
306310
2. handle click on deliver button
307311
3. set state to delivered
308-
39. Delete Order
312+
40. Delete Order
309313
1. update OrderListScreen.js
310314
2. handle delete button
311315
3. rerender after deletion
312-
40. Show Summary Report in Dashboard
316+
41. Show Summary Report in Dashboard
313317
1. create summary section
314318
2. style summary
315319
3. create summary backend
316320
4. create getSummary in api.js
317321
5. load data in dashboard screen
318322
6. show 3 boxes for Users, Orders and Sales
319-
41. Show Chart in Dashboard
323+
42. Show Chart in Dashboard
320324
1. import chartist
321325
2. add chartist css to index.html
322326
3. create linear chart for daily sales
323327
4. create pie chart for product categories
324-
42. Publish heroku
328+
43. Publish heroku
325329
1. publish steps

frontend/src/api.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ import axios from 'axios';
22
import { apiUrl } from './config';
33
import { getUserInfo } from './localStorage';
44

5+
export const getProducts = async () => {
6+
try {
7+
const response = await axios({
8+
url: `${apiUrl}/api/products`,
9+
method: 'GET',
10+
headers: {
11+
'Content-Type': 'application/json',
12+
},
13+
});
14+
if (response.statusText !== 'OK') {
15+
throw new Error(response.data.message);
16+
}
17+
return response.data;
18+
} catch (err) {
19+
console.log(err);
20+
return { error: err.response.data.message || err.message };
21+
}
22+
};
23+
524
export const getProduct = async (id) => {
625
try {
726
const response = await axios({

frontend/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import PaymentScreen from './srceens/PaymentScreen';
1212
import PlaceOrderScreen from './srceens/PlaceOrderScreen';
1313
import OrderScreen from './srceens/OrderScreen';
1414
import DashboardScreen from './srceens/DashboardScreen';
15+
import ProductListScreen from './srceens/ProductListScreen';
1516

1617
const routes = {
1718
'/': HomeScreen,
@@ -26,6 +27,7 @@ const routes = {
2627
'/payment': PaymentScreen,
2728
'/placeorder': PlaceOrderScreen,
2829
'/dashboard': DashboardScreen,
30+
'/productlist': ProductListScreen,
2931
};
3032
const router = async () => {
3133
showLoading();

frontend/src/srceens/HomeScreen.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import axios from 'axios';
22
import Rating from '../components/Rating';
3-
import { hideLoading, showLoading } from '../utils';
3+
import { getProducts } from '../api';
44

55
const HomeScreen = {
66
render: async () => {
7-
showLoading();
8-
const response = await axios({
9-
url: 'http://localhost:5000/api/products',
10-
headers: {
11-
'Content-Type': 'application/json',
12-
},
13-
});
14-
hideLoading();
15-
if (!response || response.statusText !== 'OK') {
16-
return '<div>Error in getting data</div>';
7+
const products = await getProducts();
8+
if (products.error) {
9+
return `<div class="error">${products.error}</div>`;
1710
}
18-
const products = response.data;
1911

2012
return `
2113
<ul class="products">
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import DashboardMenu from '../components/DashboardMenu';
2+
import { getProducts } from '../api';
3+
4+
const ProductListScreen = {
5+
after_render: () => {},
6+
render: async () => {
7+
const products = await getProducts();
8+
return `
9+
<div class="dashboard">
10+
${DashboardMenu.render({ selected: 'products' })}
11+
<div class="dashboard-content">
12+
<h1>Products</h1>
13+
<button id="create-product-button" class="primary">
14+
Create Product
15+
</button>
16+
<div class="product-list">
17+
<table>
18+
<thead>
19+
<tr>
20+
<th>ID</th>
21+
<th>NAME</th>
22+
<th>PRICE</th>
23+
<th>CATEGORY</th>
24+
<th>BRAND</th>
25+
<th class="tr-action">ACTION</th>
26+
<tr>
27+
</thead>
28+
<tbody>
29+
${products
30+
.map(
31+
(product) => `
32+
<tr>
33+
<td>${product._id}</td>
34+
<td>${product.name}</td>
35+
<td>${product.price}</td>
36+
<td>${product.category}</td>
37+
<td>${product.brand}</td>
38+
<td>
39+
<button id="${product._id}" class="edit-button">Edit</button>
40+
<button id="${product._id}" class="delete-button">Delete</button>
41+
</td>
42+
</tr>
43+
`
44+
)
45+
.join('\n')}
46+
</tbody>
47+
</table>
48+
</div>
49+
</div>
50+
</div>
51+
`;
52+
},
53+
};
54+
export default ProductListScreen;

frontend/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,11 @@ td {
408408
flex: 4 1 80rem;
409409
padding: 1rem;
410410
}
411+
412+
.product-list button {
413+
font-size: 1.3rem;
414+
padding: 0.5rem;
415+
}
416+
.tr-action {
417+
width: 10rem;
418+
}

0 commit comments

Comments
 (0)