2
2
import React , { useEffect , useState } from 'react'
3
3
import Table from '../../components/table/Table' ;
4
4
import { useAppDispatch , useAppSelector } from '../../store/store' ;
5
- import { fetchSellerCollectionProduct } from '../../store/features/product/sellerCollectionProductsSlice' ;
5
+ import { fetchSellerCollectionProduct , deleteItem , removeItem } from '../../store/features/product/sellerCollectionProductsSlice' ;
6
6
import Zoom from '@mui/material/Zoom' ;
7
7
import Tooltip from '@mui/material/Tooltip' ;
8
8
import DeleteIcon from '@mui/icons-material/Delete' ;
@@ -22,6 +22,13 @@ import { FaEye, FaPlusCircle } from 'react-icons/fa';
22
22
import { ISingleProductInitialResponse } from '../../utils/types/store' ;
23
23
import { resetUpdateState , updateSellerProductStatus } from '../../store/features/product/sellerProductSlice' ;
24
24
import { toast } from 'react-toastify' ;
25
+ import ConfirmModal from '../../components/product/ConfirmModal' ;
26
+
27
+ interface DeleteItemState {
28
+ id : any ;
29
+ name : string ;
30
+ }
31
+
25
32
export default function SellerCollection ( ) {
26
33
const navigate = useNavigate ( )
27
34
const dispatch = useAppDispatch ( ) ;
@@ -33,6 +40,26 @@ export default function SellerCollection() {
33
40
dispatch ( fetchSellerCollectionProduct ( ) )
34
41
} , [ dispatch ] ) ;
35
42
43
+ const [ showConfirm , setShowConfirm ] = useState ( false ) ;
44
+ const [ itemToDelete , setItemToDelete ] = useState < DeleteItemState | null > ( null ) ;
45
+
46
+
47
+ const handleDelete = async ( ) => {
48
+ try {
49
+ setShowConfirm ( false )
50
+ dispatch ( removeItem ( itemToDelete . id ) )
51
+ await dispatch ( deleteItem ( itemToDelete . id ) ) . unwrap ( ) ;
52
+ dispatch ( fetchSellerCollectionProduct ( ) ) ;
53
+ setItemToDelete ( null )
54
+ } catch ( error ) {
55
+ console . error ( 'Error deleting item:' , error ) ;
56
+ } finally {
57
+ if ( isSuccess )
58
+ toast . success ( message )
59
+ }
60
+ } ;
61
+
62
+
36
63
useEffect ( ( ) => {
37
64
if ( isUpdate && isUpdateSuccess ) {
38
65
toast . success ( updateMessage ) ;
@@ -69,7 +96,7 @@ export default function SellerCollection() {
69
96
</ IconButton >
70
97
</ Tooltip >
71
98
< Tooltip TransitionComponent = { Zoom } title = "Delete" arrow >
72
- < IconButton >
99
+ < IconButton onClick = { ( ) => { setShowConfirm ( true ) ; setItemToDelete ( { id : product . id , name : product . name } ) ; } } >
73
100
< DeleteIcon className = 'icon__delete' />
74
101
</ IconButton >
75
102
</ Tooltip >
@@ -102,6 +129,8 @@ export default function SellerCollection() {
102
129
setOpen ( false ) ;
103
130
} ;
104
131
132
+ const popupMessage = `Deleting this product <i>${ itemToDelete ?. name } </i> will be permanently removed from the system. This can't be undone!` ;
133
+
105
134
return (
106
135
< div className = 'seller__main__container' >
107
136
< Meta title = { `Seller Products` } />
@@ -162,6 +191,16 @@ export default function SellerCollection() {
162
191
</ Button >
163
192
</ DialogActions >
164
193
</ Dialog >
194
+
195
+ { showConfirm && (
196
+ < ConfirmModal
197
+ isOpen = { showConfirm }
198
+ title = "Are you sure?"
199
+ message = { popupMessage }
200
+ onConfirm = { handleDelete }
201
+ onCancel = { ( ) => setShowConfirm ( false ) }
202
+ />
203
+ ) }
165
204
</ div >
166
205
)
167
206
}
0 commit comments