Skip to content

Commit c46a760

Browse files
[finished 187584874] seller delete item
1 parent c270bfb commit c46a760

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

src/components/product/SellerProduct.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { PuffLoader } from 'react-spinners';
1212
import { Meta } from '../Meta';
1313
import { toast } from 'react-toastify';
1414
import { getErrorMessage } from '../../utils/axios/axiosInstance';
15+
import { deleteItem } from '../../store/features/product/sellerCollectionProductsSlice';
16+
import ConfirmModal from './ConfirmModal';
1517

1618
const initialProductState: ISingleProduct = {
1719
id: "",
@@ -43,6 +45,8 @@ const SellerProduct = ({ productId }: { productId: string }) => {
4345
const [isThereAnyUpdate, setIsThereAnyUpdate] = useState<boolean>(false);
4446

4547
const [updateLoading, setUpdateLoading] = useState(false);
48+
const [showConfirm, setShowConfirm] = useState(false);
49+
const [itemToDelete, setItemToDelete] = useState<string | null>(null);
4650

4751
useEffect(() => {
4852
if (!isAdd) {
@@ -123,6 +127,14 @@ const SellerProduct = ({ productId }: { productId: string }) => {
123127
}
124128
};
125129

130+
const handleDelete = async () => {
131+
if (updatedProduct) {
132+
await dispatch(deleteItem(itemToDelete));
133+
setShowConfirm(false);
134+
navigate('/seller/products');
135+
}
136+
};
137+
126138
if (isLoading) {
127139
return (
128140
<div className="loader">
@@ -150,7 +162,7 @@ const SellerProduct = ({ productId }: { productId: string }) => {
150162
<button disabled={(!isThereAnyUpdate && !isImagesUpdated) || updateLoading} className={`edit-btn ${!isThereAnyUpdate && !isImagesUpdated && 'disabled'}`} onClick={handleSaveOrAdd}>
151163
<FaSave /> {isAdd ? "ADD" : "UPDATE"}{updateLoading && "ING..."}
152164
</button>
153-
{!isAdd && <button className='delete-btn'><FaTrash /> Delete</button>}
165+
{!isAdd && <button className='delete-btn' onClick={() => { setShowConfirm(true); setItemToDelete(productId); }}><FaTrash /> Delete</button>}
154166
</div>
155167
</div>
156168

@@ -233,6 +245,14 @@ const SellerProduct = ({ productId }: { productId: string }) => {
233245
</ContentCard>
234246
</ContentCard>
235247
</div>
248+
249+
<ConfirmModal
250+
isOpen={showConfirm}
251+
title="Are you sure"
252+
message={`Deleting this product <i>${product?.name}</i> will be permanently removed from the system. This can't be undone!`}
253+
onConfirm={handleDelete}
254+
onCancel={() => setShowConfirm(false)}
255+
/>
236256
</div>
237257
</div>
238258
</>

src/pages/seller/SellerCollection.tsx

+41-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React, { useEffect, useState } from 'react'
33
import Table from '../../components/table/Table';
44
import { useAppDispatch, useAppSelector } from '../../store/store';
5-
import { fetchSellerCollectionProduct } from '../../store/features/product/sellerCollectionProductsSlice';
5+
import { fetchSellerCollectionProduct, deleteItem, removeItem } from '../../store/features/product/sellerCollectionProductsSlice';
66
import Zoom from '@mui/material/Zoom';
77
import Tooltip from '@mui/material/Tooltip';
88
import DeleteIcon from '@mui/icons-material/Delete';
@@ -22,6 +22,13 @@ import { FaEye, FaPlusCircle } from 'react-icons/fa';
2222
import { ISingleProductInitialResponse } from '../../utils/types/store';
2323
import { resetUpdateState, updateSellerProductStatus } from '../../store/features/product/sellerProductSlice';
2424
import { toast } from 'react-toastify';
25+
import ConfirmModal from '../../components/product/ConfirmModal';
26+
27+
interface DeleteItemState {
28+
id: any;
29+
name: string;
30+
}
31+
2532
export default function SellerCollection() {
2633
const navigate = useNavigate()
2734
const dispatch = useAppDispatch();
@@ -33,6 +40,26 @@ export default function SellerCollection() {
3340
dispatch(fetchSellerCollectionProduct())
3441
}, [dispatch]);
3542

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+
3663
useEffect(() => {
3764
if (isUpdate && isUpdateSuccess) {
3865
toast.success(updateMessage);
@@ -69,7 +96,7 @@ export default function SellerCollection() {
6996
</IconButton>
7097
</Tooltip>
7198
<Tooltip TransitionComponent={Zoom} title="Delete" arrow>
72-
<IconButton>
99+
<IconButton onClick={() => { setShowConfirm(true); setItemToDelete({ id: product.id, name: product.name }); }}>
73100
<DeleteIcon className='icon__delete' />
74101
</IconButton>
75102
</Tooltip>
@@ -102,6 +129,8 @@ export default function SellerCollection() {
102129
setOpen(false);
103130
};
104131

132+
const popupMessage = `Deleting this product <i>${itemToDelete?.name}</i> will be permanently removed from the system. This can't be undone!`;
133+
105134
return (
106135
<div className='seller__main__container'>
107136
<Meta title={`Seller Products`} />
@@ -162,6 +191,16 @@ export default function SellerCollection() {
162191
</Button>
163192
</DialogActions>
164193
</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+
)}
165204
</div>
166205
)
167206
}

src/store/features/product/productService.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ const sellerGetOrderHistory = async () => {
9797
return response.data;
9898
}
9999

100+
const SellerDeleteItem = async(id: string)=>{
101+
try{
102+
const response = await axiosInstance.delete(`api/shop/seller-delete-product/${id}`);
103+
return response.data
104+
}catch(error){
105+
throw new Error("failed to delete product")
106+
}
107+
}
108+
100109
const productService = {
101110
fetchProducts,
102111
fetchSingleProduct,
@@ -109,6 +118,7 @@ const productService = {
109118
addSellerProduct,
110119
updateSellerProductStatus,
111120
sellerGetAllProducts,
112-
sellerGetOrderHistory
121+
sellerGetOrderHistory,
122+
SellerDeleteItem
113123
}
114124
export default productService;

src/store/features/product/sellerCollectionProductsSlice.tsx

+33-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,24 @@ export const sellerGetOrderHistory = createAsyncThunk('seller/seller-get-orderHi
4343
}
4444
})
4545

46+
export const deleteItem = createAsyncThunk<ISellerCollectionProductInitialResponse, string>("product/deleteProduct", async (id, thunkApi) => {
47+
try {
48+
const response = await productService.SellerDeleteItem(id)
49+
return response
50+
} catch (error) {
51+
return thunkApi.rejectWithValue(error)
52+
}
53+
})
54+
4655
const sellerCollectionProductsSlice = createSlice({
4756
name: "sellerCollectionProducts",
4857
initialState,
49-
reducers: {},
58+
reducers: {
59+
removeItem: (state, action: any) => {
60+
const itemId = action.payload
61+
state.data.products = state.data?.products.filter((item) =>item.id !== itemId)
62+
}
63+
},
5064
extraReducers: (builder) => {
5165
builder
5266
.addCase(fetchSellerCollectionProduct.pending, (state) => {
@@ -96,8 +110,24 @@ const sellerCollectionProductsSlice = createSlice({
96110
state.isError = false;
97111
state.isSuccess = false;
98112
state.message = action.payload.message || null
99-
});
113+
})
114+
.addCase(deleteItem.pending, (state) => {
115+
state.isError = false,
116+
state.isSuccess = false
117+
})
118+
.addCase(deleteItem.fulfilled, (state, action: PayloadAction<any>) => {
119+
state.isLoading = false,
120+
state.isError = false,
121+
state.isSuccess = true
122+
state.message = action.payload.message
123+
})
124+
.addCase(deleteItem.rejected, (state, action: PayloadAction<any>) => {
125+
state.isLoading = false,
126+
state.isError = true,
127+
state.message = action.payload,
128+
state.isSuccess = false
129+
});;
100130
}
101131
})
102-
132+
export const {removeItem} = sellerCollectionProductsSlice.actions
103133
export default sellerCollectionProductsSlice.reducer;

webpack.dev.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const config: Configuration = {
9797
historyApiFallback: {
9898
disableDotRule: true,
9999
},
100-
port: 5000,
100+
port: 9000,
101101
open: true,
102102
hot: true,
103103
},

0 commit comments

Comments
 (0)