Skip to content

Commit

Permalink
Video-41-Implement-Update-Product
Browse files Browse the repository at this point in the history
  • Loading branch information
basir committed Mar 8, 2022
1 parent 4db9dd9 commit 2745013
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,6 @@
1. create edit button
2. create edit product ui
3. dispaly product info in the input boxes
41. Implement Update Product
1. create edit product backend api
2. handle update click
24 changes: 24 additions & 0 deletions backend/routes/productRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ productRouter.post(
})
);

productRouter.put(
'/:id',
isAuth,
isAdmin,
expressAsyncHandler(async (req, res) => {
const productId = req.params.id;
const product = await Product.findById(productId);
if (product) {
product.name = req.body.name;
product.slug = req.body.slug;
product.price = req.body.price;
product.image = req.body.image;
product.category = req.body.category;
product.brand = req.body.brand;
product.countInStock = req.body.countInStock;
product.description = req.body.description;
await product.save();
res.send({ message: 'Product Updated' });
} else {
res.status(404).send({ message: 'Product Not Found' });
}
})
);

const PAGE_SIZE = 3;

productRouter.get(
Expand Down
48 changes: 45 additions & 3 deletions frontend/src/screens/ProductEditScreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useContext, useEffect, useReducer, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { toast } from 'react-toastify';
import axios from 'axios';
import { Store } from '../Store';
import { getError } from '../utils';
Expand All @@ -18,17 +19,24 @@ const reducer = (state, action) => {
return { ...state, loading: false };
case 'FETCH_FAIL':
return { ...state, loading: false, error: action.payload };
case 'UPDATE_REQUEST':
return { ...state, loadingUpdate: true };
case 'UPDATE_SUCCESS':
return { ...state, loadingUpdate: false };
case 'UPDATE_FAIL':
return { ...state, loadingUpdate: false };
default:
return state;
}
};
export default function ProductEditScreen() {
const navigate = useNavigate();
const params = useParams(); // /product/:id
const { id: productId } = params;

const { state } = useContext(Store);
const { userInfo } = state;
const [{ loading, error }, dispatch] = useReducer(reducer, {
const [{ loading, error, loadingUpdate }, dispatch] = useReducer(reducer, {
loading: true,
error: '',
});
Expand Down Expand Up @@ -66,6 +74,37 @@ export default function ProductEditScreen() {
fetchData();
}, [productId]);

const submitHandler = async (e) => {
e.preventDefault();
try {
dispatch({ type: 'UPDATE_REQUEST' });
await axios.put(
`/api/products/${productId}`,
{
_id: productId,
name,
slug,
price,
image,
category,
brand,
countInStock,
description,
},
{
headers: { Authorization: `Bearer ${userInfo.token}` },
}
);
dispatch({
type: 'UPDATE_SUCCESS',
});
toast.success('Product updated successfully');
navigate('/admin/products');
} catch (err) {
toast.error(getError(err));
dispatch({ type: 'UPDATE_FAIL' });
}
};
return (
<Container className="small-container">
<Helmet>
Expand All @@ -78,7 +117,7 @@ export default function ProductEditScreen() {
) : error ? (
<MessageBox variant="danger">{error}</MessageBox>
) : (
<Form>
<Form onSubmit={submitHandler}>
<Form.Group className="mb-3" controlId="name">
<Form.Label>Name</Form.Label>
<Form.Control
Expand Down Expand Up @@ -144,7 +183,10 @@ export default function ProductEditScreen() {
/>
</Form.Group>
<div className="mb-3">
<Button type="submit">Update</Button>
<Button disabled={loadingUpdate} type="submit">
Update
</Button>
{loadingUpdate && <LoadingBox></LoadingBox>}
</div>
</Form>
)}
Expand Down

0 comments on commit 2745013

Please sign in to comment.