|
@@ -1,9 +1,8 @@
|
|
|
-import { Box, Button, FormControl, InputLabel, MenuItem, Select, Stack, TextField } from '@mui/material';
|
|
|
+import { Box, Button, Checkbox, FormControl, InputLabel, ListItemText, MenuItem, OutlinedInput, Select, Stack, TextField } from '@mui/material';
|
|
|
import React, { useState } from 'react';
|
|
|
-import { useDispatch } from 'react-redux';
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
import ImageUploader from '../components/ImageUploader';
|
|
|
-import { useCreateGoodMutation, useGetCategoriesQuery } from '../store/api';
|
|
|
+import { useCreateGoodMutation, useGetCategoriesWithSubCategoriesQuery } from '../store/api';
|
|
|
|
|
|
|
|
|
const ITEM_HEIGHT = 48;
|
|
@@ -17,21 +16,23 @@ const MenuProps = {
|
|
|
},
|
|
|
};
|
|
|
|
|
|
-function EditGood({ name, category, price, description, goodImages, goodId, buttonText }) {
|
|
|
- const { data } = useGetCategoriesQuery();
|
|
|
+function EditGood({ name, categories, price, description, goodImages, goodId, buttonText }) {
|
|
|
+ const { data } = useGetCategoriesWithSubCategoriesQuery();
|
|
|
const [goodName, setGoodName] = useState(name || '');
|
|
|
- const [selectedCategory, setSelectedCategory] = useState(category || '');
|
|
|
- console.log(category)
|
|
|
+ const [selectedCategories, setSelectedCategories] = useState(categories || []);
|
|
|
const [goodPrice, setGoodPrice] = useState(price || 0);
|
|
|
const [goodDescription, setGoodDescription] = useState(description || '');
|
|
|
const [images, setImages] = useState(goodImages || []);
|
|
|
- console.log(images);
|
|
|
const navigate = useNavigate();
|
|
|
const [createGood, result] = useCreateGoodMutation();
|
|
|
|
|
|
|
|
|
- const handleCategoryChange = (event) => {
|
|
|
- setSelectedCategory(event.target.value);
|
|
|
+ const onCategoryClick = (category) => {
|
|
|
+ if (selectedCategories.find(selectedCategory => selectedCategory._id == category._id)) {
|
|
|
+ setSelectedCategories(selectedCategories.filter(selectedCategory => selectedCategory._id != category._id));
|
|
|
+ } else {
|
|
|
+ setSelectedCategories([...selectedCategories, category]);
|
|
|
+ }
|
|
|
};
|
|
|
const handlePriceChange = (event) => {
|
|
|
setGoodPrice(event.target.value);
|
|
@@ -45,7 +46,10 @@ function EditGood({ name, category, price, description, goodImages, goodId, butt
|
|
|
|
|
|
const good = {
|
|
|
name: goodName,
|
|
|
- categories: [selectedCategory],
|
|
|
+ categories: selectedCategories.map(category => ({
|
|
|
+ _id: category._id,
|
|
|
+ name: category.name
|
|
|
+ })),
|
|
|
price: +goodPrice,
|
|
|
description: goodDescription,
|
|
|
images: images.map(image => ({ _id: image._id }))
|
|
@@ -72,14 +76,16 @@ function EditGood({ name, category, price, description, goodImages, goodId, butt
|
|
|
sx={{ m: '0 auto 20px', width: '100%' }}>
|
|
|
<InputLabel>Category</InputLabel>
|
|
|
<Select
|
|
|
- value={selectedCategory}
|
|
|
- onChange={handleCategoryChange}
|
|
|
+ multiple
|
|
|
+ value={selectedCategories}
|
|
|
+ input={<OutlinedInput label="Category name" />}
|
|
|
+ renderValue={(selected) => selected.map(item => item.name).join(', ')}
|
|
|
MenuProps={MenuProps}
|
|
|
- renderValue={(selected => selectedCategory?.name)}
|
|
|
>
|
|
|
{data?.CategoryFind?.map((category) => (
|
|
|
- <MenuItem key={category._id} value={category}>
|
|
|
- {category?.name}
|
|
|
+ <MenuItem key={category._id} value={category} onClick={() => onCategoryClick(category)}>
|
|
|
+ <Checkbox checked={!!selectedCategories?.find(selectedCategory => selectedCategory._id === category._id)} />
|
|
|
+ <ListItemText primary={category.name} />
|
|
|
</MenuItem>
|
|
|
))}
|
|
|
</Select>
|