-
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.
- Loading branch information
1 parent
5789299
commit f19048d
Showing
10 changed files
with
180 additions
and
17 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
client/src/components/Ecommerce/Screen/ProductListScreen.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,90 @@ | ||
import React, { useEffect } from 'react' | ||
import { Button, Col, Row, Table } from 'react-bootstrap' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { LinkContainer } from 'react-router-bootstrap' | ||
import { deleteProduct, fetchProducts } from '../../../redux/actions/productActions' | ||
import Loader from '../Loader' | ||
import Message from '../Message' | ||
import { toastErrorMessage } from '../ToastMessage' | ||
|
||
function UserList({history}) { | ||
|
||
const dispatch = useDispatch() | ||
|
||
const { userInfo: { user } } = useSelector(state => state.userLogin) | ||
|
||
const productList = useSelector(state => state.productList) | ||
const { loading, error, products } = productList | ||
const productDelete = useSelector(state => state.productDelete) | ||
const { loading:loadingDelete, error:errorDelete, success:successDelete } = productDelete | ||
|
||
useEffect(() => { | ||
if (user && user.isAdmin) { | ||
dispatch(fetchProducts()) | ||
} else { | ||
history.push("/login") | ||
} | ||
}, [dispatch,history,user,successDelete]) | ||
|
||
const handleDelete = (id) => { | ||
if (window.confirm('Are you sure you want to delete this user ?')) { | ||
dispatch(deleteProduct(id)) | ||
if(!errorDelete) | ||
toastErrorMessage("User deleted") | ||
} | ||
} | ||
const handleCreateProduct= () => {} | ||
|
||
return ( | ||
<> | ||
<Row className="align-items-center"> | ||
<Col> | ||
<h1>Products</h1> | ||
</Col> | ||
<Col className="text-right"> | ||
<Button className="my-3 block" style={{float: 'right'}}><i className="fas fa-plus" onClick={handleCreateProduct}></i> Create Product</Button> | ||
</Col> | ||
</Row> | ||
|
||
{loadingDelete && <Loader />} | ||
{errorDelete && <Message variant='danger'>{errorDelete}</Message>} | ||
|
||
{loading ? <Loader /> : error ? <Message variant='danger'>{error}</Message> : ( | ||
<> | ||
<Table striped bordered hover> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>Name</th> | ||
<th>Price</th> | ||
<th>Category</th> | ||
<th>Count In Stock</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{products.map(product=>( | ||
<tr key={product._id}> | ||
<td>{product._id}</td> | ||
<td>{product.name}</td> | ||
<td><span style={{fontSize:'1.5rem',marginRight:'0.1rem'}}>৳</span> {product.price}</td> | ||
<td>{product.category}</td> | ||
<td>{product.countInStock}</td> | ||
<td> | ||
<LinkContainer to={`users/${product._id}/edit`}> | ||
<Button ><i className='fas fa-edit'></i></Button> | ||
</LinkContainer> | ||
</td> | ||
<td> | ||
<Button variant='light' onClick={()=>handleDelete(product._id)}><i className='fas fa-trash'></i></Button> | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</Table> | ||
</> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default UserList |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import express from 'express' | ||
import { createProductReview, getProductById, getProducts } from '../controller/productController.js' | ||
import { isLoggedIn } from '../middlewares/authMiddleware.js' | ||
import { createProductReview, deleteProductById, getProductById, getProducts } from '../controller/productController.js' | ||
import { isAdmin, isLoggedIn } from '../middlewares/authMiddleware.js' | ||
|
||
const router = express.Router() | ||
|
||
router.get("/",getProducts) | ||
router.get("/:id", getProductById) | ||
router.post("/:id/reviews",isLoggedIn,createProductReview) | ||
router.delete("/:id",isLoggedIn, isAdmin,deleteProductById) | ||
|
||
export default router |
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