11/* eslint-disable */
2- import React , { useEffect } from 'react'
2+ import React , { useEffect , useState } from 'react'
33import Table from '../../components/table/Table' ;
44import { useAppDispatch , useAppSelector } from '../../store/store' ;
5- import { fetchSellerCollectionProduct } from '../../store/features/product/sellerCollectionProductsSlice' ;
5+ import { deleteItem , fetchSellerCollectionProduct } from '../../store/features/product/sellerCollectionProductsSlice' ;
66import Zoom from '@mui/material/Zoom' ;
77import Tooltip from '@mui/material/Tooltip' ;
88import DeleteIcon from '@mui/icons-material/Delete' ;
@@ -14,6 +14,8 @@ import LinearProgress from '@mui/material/LinearProgress';
1414import { useNavigate } from 'react-router-dom' ;
1515import { Meta } from '../../components/Meta' ;
1616import { FaEye } from 'react-icons/fa' ;
17+ import ConfirmModal from '../../components/product/ConfirmModal' ;
18+
1719export default function SellerCollection ( ) {
1820 const navigate = useNavigate ( )
1921 const dispatch = useAppDispatch ( ) ;
@@ -23,7 +25,23 @@ export default function SellerCollection() {
2325 dispatch ( fetchSellerCollectionProduct ( ) )
2426 } , [ dispatch ] ) ;
2527
26- const rows = data . products ? data . products
28+ const [ showConfirm , setShowConfirm ] = useState ( false ) ;
29+ const [ itemToDelete , setItemToDelete ] = useState < { id : string ; name : string } | null > ( null ) ;
30+
31+
32+ const handleDelete = async ( ) => {
33+ try {
34+ if ( itemToDelete !== null ) {
35+ await dispatch ( deleteItem ( itemToDelete . id ) ) ;
36+ setShowConfirm ( false ) ;
37+ setItemToDelete ( null )
38+ }
39+ } catch ( error ) {
40+ console . error ( 'Error deleting item:' , error ) ;
41+ }
42+ } ;
43+
44+ const rows = data ?. products ? data . products
2745 . map ( ( product , index ) => [
2846 index + 1 ,
2947 < img src = { product . images [ 0 ] } alt = "image" className = 'product-img' /> ,
@@ -40,7 +58,7 @@ export default function SellerCollection() {
4058 </ IconButton >
4159 </ Tooltip >
4260 < Tooltip TransitionComponent = { Zoom } title = "Delete" arrow >
43- < IconButton >
61+ < IconButton onClick = { ( ) => { setShowConfirm ( true ) ; setItemToDelete ( { id : product . id , name : product . name } ) ; } } >
4462 < DeleteIcon className = 'icon__delete' />
4563 </ IconButton >
4664 </ Tooltip >
@@ -52,6 +70,7 @@ export default function SellerCollection() {
5270 </ div >
5371 ] ) : [ ] ;
5472
73+ const popupMessage = `Deleting this product <i>${ itemToDelete ?. name } </i> will be permanently removed from the system. This can’t be undone!` ;
5574
5675 return (
5776 < div className = 'seller__main__container' >
@@ -64,6 +83,16 @@ export default function SellerCollection() {
6483 </ div >
6584 ) }
6685 < Table title = { 'Product List' } headers = { headers } rows = { rows } />
86+
87+ { showConfirm && (
88+ < ConfirmModal
89+ isOpen = { showConfirm }
90+ title = "Are you sure?"
91+ message = { popupMessage }
92+ onConfirm = { handleDelete }
93+ onCancel = { ( ) => setShowConfirm ( false ) }
94+ />
95+ ) }
6796 </ div >
6897 )
6998}
0 commit comments