Skip to content

Commit 4010529

Browse files
[finished 187584874] seller delete item
1 parent 8432331 commit 4010529

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

Diff for: 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
</>

Diff for: src/pages/seller/SellerCollection.tsx

+3-3
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';
@@ -47,8 +47,8 @@ export default function SellerCollection() {
4747
const handleDelete = async () => {
4848
try {
4949
setShowConfirm(false)
50-
// dispatch(removeItem(itemToDelete.id))
51-
// await dispatch(deleteItem(itemToDelete.id)).unwrap();
50+
dispatch(removeItem(itemToDelete.id))
51+
await dispatch(deleteItem(itemToDelete.id)).unwrap();
5252
dispatch(fetchSellerCollectionProduct());
5353
setItemToDelete(null)
5454
} catch (error) {

Diff for: 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;

Diff for: 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;

0 commit comments

Comments
 (0)