Skip to content

Commit 1a75c0d

Browse files
committed
Video-13-Product-Screen
1 parent 7087cc7 commit 1a75c0d

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ module.exports = {
1313
'no-console': 0,
1414
'no-underscore-dangle': 0,
1515
'no-nested-ternary': 0,
16+
'import/prefer-default-export': 0,
1617
},
1718
};

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,16 @@ JS AMAZONA
111111
8. style div.rating, span and last span
112112
9. Edit HomeScreen
113113
10. Add div.product-rating and use Rating component
114+
13. Product Screen
115+
1. get product id from request
116+
2. implement /api/product/:id api
117+
3. send Ajax request to product api
118+
4. create back to result link
119+
5. create div.details with 3 columns
120+
6. column 1 for product image
121+
7. column 2 for product information
122+
8. column 3 form product action
123+
9. style .details and all columns
124+
10. create add to cart button with add-button id
125+
11. after_render() to add event to the button
126+
12. redirect user to cart/:product_id

backend/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ app.use(cors());
77
app.get('/api/products', (req, res) => {
88
res.send(data.products);
99
});
10+
app.get('/api/products/:id', (req, res) => {
11+
const product = data.products.find((x) => x._id === req.params.id);
12+
if (product) {
13+
res.send(product);
14+
} else {
15+
res.status(404).send({ message: 'Product Not Found!' });
16+
}
17+
});
1018

1119
app.listen(5000, () => {
1220
console.log('serve at http://localhost:5000');

frontend/src/api.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import axios from 'axios';
2+
import { apiUrl } from './config';
3+
4+
export const getProduct = async (id) => {
5+
try {
6+
const response = await axios({
7+
url: `${apiUrl}/api/products/${id}`,
8+
method: 'GET',
9+
headers: {
10+
'Content-Type': 'application/json',
11+
},
12+
});
13+
if (response.statusText !== 'OK') {
14+
throw new Error(response.data.message);
15+
}
16+
return response.data;
17+
} catch (err) {
18+
console.log(err);
19+
return { error: err.response.data.message || err.message };
20+
}
21+
};

frontend/src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const apiUrl = 'http://localhost:5000';

frontend/src/srceens/ProductScreen.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
import { parseRequestUrl } from '../utils';
2+
import { getProduct } from '../api';
3+
14
const ProductScreen = {
2-
render: () => '<div>ProductScreen</div>',
5+
render: async () => {
6+
const request = parseRequestUrl();
7+
const product = await getProduct(request.id);
8+
if (product.error) {
9+
return `<div>${product.error}</div>`;
10+
}
11+
return `<h1>${product.name}</h1>`;
12+
},
313
};
414
export default ProductScreen;

0 commit comments

Comments
 (0)