-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
admin can now update/create a product
- Loading branch information
1 parent
d751b14
commit f161c91
Showing
7 changed files
with
299 additions
and
16 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
client/src/components/Ecommerce/Screen/ProductEditScreen.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { Button, Form } from 'react-bootstrap' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { Link } from 'react-router-dom' | ||
import { PRODUCT_UPDATE_RESET } from '../../../constants/productConstants' | ||
import { fetchProductById, updateProduct } from '../../../redux/actions/productActions' | ||
import FormContainer from '../FormContainer' | ||
import Loader from '../Loader' | ||
import Message from '../Message' | ||
import { toastSuccessMessage } from '../ToastMessage' | ||
|
||
const ProductEditScreen = ({ match, history }) => { | ||
const productId = match.params.id | ||
|
||
const [name, setName] = useState('') | ||
const [description, setDescription] = useState('') | ||
const [category, setCategory] = useState('') | ||
const [price, setPrice] = useState(0) | ||
const [countInStock, setCountInStock] = useState(0) | ||
const [image, setImage] = useState('') | ||
|
||
const dispatch = useDispatch() | ||
|
||
const productDetails = useSelector((state) => state.productDetails) | ||
const { loading, error, product } = productDetails | ||
const productUpdate = useSelector((state) => state.productUpdate) | ||
const { loading:loadingUpdate, error:errorUpdate, success:successUpdate } = productUpdate | ||
|
||
|
||
useEffect(() => { | ||
if (successUpdate) { | ||
dispatch({ type: PRODUCT_UPDATE_RESET }) | ||
toastSuccessMessage("Product Updated") | ||
history.push("/admin/productlist") | ||
|
||
} else { | ||
if (!product.name || product._id !== productId) { | ||
dispatch(fetchProductById(productId)) | ||
} else { | ||
setName(product.name) | ||
setDescription(product.description) | ||
setCategory(product.category) | ||
setImage(product.image) | ||
setPrice(product.price) | ||
setCountInStock(product.countInStock) | ||
} | ||
} | ||
|
||
}, [dispatch,product,productId,successUpdate,history]) | ||
|
||
const submitHandler = (e) => { | ||
e.preventDefault() | ||
dispatch(updateProduct({ | ||
_id: product._id,name,description,image,category,price,countInStock | ||
})) | ||
} | ||
|
||
return ( | ||
<> | ||
<Link to='/admin/productlist' className='btn btn-light my-3'> | ||
Go Back | ||
</Link> | ||
<FormContainer> | ||
<h1>Edit Product</h1> | ||
{loadingUpdate && <Loader />} | ||
{errorUpdate && <Message variant='danger'>{errorUpdate}</Message>} | ||
{loading ? ( | ||
<Loader /> | ||
) : error ? ( | ||
<Message variant='danger'>{error}</Message> | ||
) : ( | ||
<Form onSubmit={submitHandler}> | ||
<Form.Group controlId='name'> | ||
<Form.Label>Name</Form.Label> | ||
<Form.Control | ||
type='name' | ||
placeholder='Enter name' | ||
value={name} | ||
onChange={(e) => setName(e.target.value)} | ||
></Form.Control> | ||
</Form.Group> | ||
|
||
<Form.Group controlId='description'> | ||
<Form.Label>Description</Form.Label> | ||
<Form.Control | ||
type='text' | ||
placeholder='Write Description' | ||
value={description} | ||
onChange={(e) => setDescription(e.target.value)} | ||
></Form.Control> | ||
</Form.Group> | ||
|
||
<Form.Group controlId='category'> | ||
<Form.Label>Category</Form.Label> | ||
<Form.Control | ||
type='text' | ||
placeholder='Enter Category' | ||
value={category} | ||
onChange={(e) => setCategory(e.target.value)} | ||
></Form.Control> | ||
</Form.Group> | ||
|
||
<Form.Group controlId='image'> | ||
<Form.Label>Image</Form.Label> | ||
<Form.Control | ||
type='text' | ||
placeholder='Enter image url' | ||
value={image} | ||
onChange={(e) => setImage(e.target.value)} | ||
></Form.Control> | ||
</Form.Group> | ||
|
||
<Form.Group controlId='price'> | ||
<Form.Label>Price</Form.Label> | ||
<Form.Control | ||
type='number' | ||
placeholder='Enter Price' | ||
value={price} | ||
onChange={(e) => setPrice(e.target.value)} | ||
></Form.Control> | ||
</Form.Group> | ||
|
||
<Form.Group controlId='countInStock'> | ||
<Form.Label>Count In Stock</Form.Label> | ||
<Form.Control | ||
type='number' | ||
placeholder='Enter Stock' | ||
value={countInStock} | ||
onChange={(e) => setCountInStock(e.target.value)} | ||
></Form.Control> | ||
</Form.Group> | ||
<Button type='submit' variant='primary'> | ||
Update | ||
</Button> | ||
</Form> | ||
)} | ||
</FormContainer> | ||
</> | ||
) | ||
} | ||
|
||
export default ProductEditScreen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.