Skip to content

Commit 3af1e32

Browse files
authored
Merge pull request #6 from RGvirer/delete-x-icon-working-in-product-edit
Update EditProduct.js
2 parents c6b03d1 + 7458f35 commit 3af1e32

File tree

1 file changed

+77
-42
lines changed

1 file changed

+77
-42
lines changed

src/features/admin/EditProduct.js

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
import { useState, useEffect } from 'react';
2-
import { Box, Button, FormControl, IconButton, ImageList, ImageListItem, InputLabel, OutlinedInput, Typography } from '@mui/material';
3-
import { useNavigate, useParams } from 'react-router-dom';
4-
import { addProductsToServer, getProductByIdFromServer, updateProductByIdInServer } from '../products/productsApi.js';
5-
import { useDispatch } from 'react-redux';
6-
import { saveAllFromServer } from '../products/productsSlice.js';
7-
import CloudUploadIcon from '@mui/icons-material/CloudUpload';
8-
import CloseRoundedIcon from '@mui/icons-material/CloseRounded';
9-
import styled from 'styled-components';
10-
import { useForm } from 'react-hook-form';
1+
import { useState, useEffect } from "react";
2+
import {
3+
Box,
4+
Button,
5+
FormControl,
6+
IconButton,
7+
ImageList,
8+
ImageListItem,
9+
InputLabel,
10+
OutlinedInput,
11+
Typography,
12+
} from "@mui/material";
13+
import { useNavigate, useParams } from "react-router-dom";
14+
import {
15+
addProductsToServer,
16+
getProductByIdFromServer,
17+
updateProductByIdInServer,
18+
} from "../products/productsApi.js";
19+
import { useDispatch } from "react-redux";
20+
import { saveAllFromServer } from "../products/productsSlice.js";
21+
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
22+
import CloseRoundedIcon from "@mui/icons-material/CloseRounded";
23+
import styled from "styled-components";
24+
import { useForm } from "react-hook-form";
1125

1226
const EditProduct = ({ isDefaultValues }) => {
1327
const { id } = useParams();
@@ -26,15 +40,15 @@ const EditProduct = ({ isDefaultValues }) => {
2640
const navigate = useNavigate();
2741
const baseUrl = "https://storeserver-uoax.onrender.com/";
2842

29-
const VisuallyHiddenInput = styled('input')({
30-
clip: 'rect(0 0 0 0)',
31-
clipPath: 'inset(50%)',
43+
const VisuallyHiddenInput = styled("input")({
44+
clip: "rect(0 0 0 0)",
45+
clipPath: "inset(50%)",
3246
height: 1,
33-
overflow: 'hidden',
34-
position: 'absolute',
47+
overflow: "hidden",
48+
position: "absolute",
3549
bottom: 0,
3650
left: 0,
37-
whiteSpace: 'nowrap',
51+
whiteSpace: "nowrap",
3852
width: 1,
3953
});
4054

@@ -47,23 +61,37 @@ const EditProduct = ({ isDefaultValues }) => {
4761
setIsDirty(true);
4862
};
4963

64+
const handleDeleteImage = (index) => {
65+
// העתק את רשימת התמונות הקיימת
66+
const updatedImages = [...product.routingToImage];
67+
68+
// הסר את התמונה על ידי index
69+
updatedImages.splice(index, 1);
70+
71+
// עדכן את ה-state של המוצר
72+
setProduct((prevProduct) => ({
73+
...prevProduct,
74+
routingToImage: updatedImages,
75+
}));
76+
setIsDirty(true);
77+
};
5078

5179
const handleUpdateProduct = async () => {
5280
try {
53-
const userToken = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')).token : null;
81+
const userToken = localStorage.getItem("user")
82+
? JSON.parse(localStorage.getItem("user")).token
83+
: null;
5484
if (isDefaultValues) {
5585
await updateProductByIdInServer(id, product, userToken);
56-
}
57-
else {
86+
} else {
5887
await addProductsToServer(product, userToken);
5988
}
6089
dispatch(saveAllFromServer());
6190
alert("הנתונים התעדכנו");
6291
setIsDirty(false);
63-
navigate('/products');
64-
}
65-
catch (error) {
66-
console.error('Error updating product:', error);
92+
navigate("/products");
93+
} catch (error) {
94+
console.error("Error updating product:", error);
6795
if (error.response && error.response.data) {
6896
alert(error.response.data);
6997
} else {
@@ -72,8 +100,6 @@ const EditProduct = ({ isDefaultValues }) => {
72100
}
73101
};
74102

75-
76-
77103
useEffect(() => {
78104
const fetchProduct = async () => {
79105
try {
@@ -84,8 +110,8 @@ const EditProduct = ({ isDefaultValues }) => {
84110
setProduct(data);
85111
}
86112
} catch (error) {
87-
console.error('Error fetching product:', error);
88-
setError('לא נמצא מידע על מוצר כזה');
113+
console.error("Error fetching product:", error);
114+
setError("לא נמצא מידע על מוצר כזה");
89115
} finally {
90116
setLoading(false);
91117
}
@@ -108,12 +134,14 @@ const EditProduct = ({ isDefaultValues }) => {
108134

109135
return (
110136
<>
111-
<Typography variant="h5" sx={{ marginBottom: "-3%" }} align='center'>{isDefaultValues ? "עריכת מוצר" : "הוספת מוצר"}</Typography>
137+
<Typography variant="h5" sx={{ marginBottom: "-3%" }} align="center">
138+
{isDefaultValues ? "עריכת מוצר" : "הוספת מוצר"}
139+
</Typography>
112140
<Box style={{ margin: "3%", border: "3px solid", padding: "1%" }}>
113141
<Box
114142
component="form"
115143
sx={{
116-
'& > :not(style)': { m: 1 },
144+
"& > :not(style)": { m: 1 },
117145
direction: "rtl",
118146
}}
119147
noValidate
@@ -123,7 +151,7 @@ const EditProduct = ({ isDefaultValues }) => {
123151
>
124152
<div>
125153
<div>
126-
<FormControl style={{ marginBottom: '10px' }}>
154+
<FormControl style={{ marginBottom: "10px" }}>
127155
<InputLabel htmlFor="name">שם</InputLabel>
128156
<OutlinedInput
129157
style={{ width: "30vw" }}
@@ -137,7 +165,7 @@ const EditProduct = ({ isDefaultValues }) => {
137165
</div>
138166

139167
<div>
140-
<FormControl style={{ marginBottom: '10px' }}>
168+
<FormControl style={{ marginBottom: "10px" }}>
141169
<InputLabel htmlFor="description">תאור</InputLabel>
142170
<OutlinedInput
143171
style={{ width: "30vw" }}
@@ -152,7 +180,7 @@ const EditProduct = ({ isDefaultValues }) => {
152180
</div>
153181

154182
<div>
155-
<FormControl style={{ marginBottom: '10px' }}>
183+
<FormControl style={{ marginBottom: "10px" }}>
156184
<InputLabel htmlFor="price">מחיר</InputLabel>
157185
<OutlinedInput
158186
{...register("password", { required: "שדה חובה" })}
@@ -168,7 +196,7 @@ const EditProduct = ({ isDefaultValues }) => {
168196
</div>
169197

170198
<div>
171-
<FormControl style={{ marginBottom: '10px' }}>
199+
<FormControl style={{ marginBottom: "10px" }}>
172200
<InputLabel htmlFor="details">פרטים</InputLabel>
173201
<OutlinedInput
174202
style={{ width: "30vw" }}
@@ -193,16 +221,16 @@ const EditProduct = ({ isDefaultValues }) => {
193221
/>
194222
<IconButton
195223
style={{
196-
position: 'absolute',
197-
top: '50%',
198-
left: '50%',
199-
transform: 'translate(-50%, -50%)',
200-
backgroundColor: 'rgba(255, 255, 255, 0.8)',
201-
borderRadius: '50%',
202-
padding: '4px',
224+
position: "absolute",
225+
top: "50%",
226+
left: "50%",
227+
transform: "translate(-50%, -50%)",
228+
backgroundColor: "rgba(255, 255, 255, 0.8)",
229+
borderRadius: "50%",
230+
padding: "4px",
203231
}}
204232
>
205-
<CloseRoundedIcon />
233+
<CloseRoundedIcon onClick={() => handleDeleteImage(index)} />
206234
</IconButton>
207235
</ImageListItem>
208236
))}
@@ -218,7 +246,14 @@ const EditProduct = ({ isDefaultValues }) => {
218246
</Button>
219247
</ImageList>
220248
</Box>
221-
<Button disabled={!isDirty} variant="contained" color="primary" onClick={handleUpdateProduct}>{isDefaultValues ? "עדכן מוצר" : "הוסף מוצר"}</Button>
249+
<Button
250+
disabled={!isDirty}
251+
variant="contained"
252+
color="primary"
253+
onClick={handleUpdateProduct}
254+
>
255+
{isDefaultValues ? "עדכן מוצר" : "הוסף מוצר"}
256+
</Button>
222257
</Box>
223258
</>
224259
);

0 commit comments

Comments
 (0)