123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import {useEffect, useState} from "react";
- import Typography from "@mui/material/Typography";
- import {Button, Grid, Switch, TextField} from "@mui/material";
- import {connect} from "react-redux";
- import {actionCategoryCount, actionCategoryUpsert, actionFullRootCats} from "../../../actions/ActionCategory";
- import {actionAllGoodFind} from "../../../actions/ActionGoodFind";
- import * as React from "react";
- import Autocomplete from "@mui/material/Autocomplete";
- import Box from "@mui/material/Box";
- const AddSubCategory = ({goods, onCreate}) => {
- const [subCategory, setSubCategory] = useState({});
- useEffect(() => {
- onCreate({...subCategory})
- }, [subCategory])
- return (
- <>
- <AddRootCategory goods={goods} onCreate={value => {setSubCategory({...value})}}/>
- </>
- )
- }
- const AddRootCategory = ({goods, onCreate}) => {
- const [name, setName] = useState('');
- const [products, setProducts] = useState([]);
- const [addSub, setAddSub] = useState(false);
- const [subCategory, setSubCategory] = useState({});
- useEffect(() => {
- let obj = {}
- if (Object.entries(subCategory).length > 0) {
- obj['subCategories'] = [{...subCategory}]
- }
- if (products.length > 0){
- obj["goods"] = [...products]
- }
- if (name) {
- obj["name"] = name
- onCreate(obj)
- }
- }, [name, products, subCategory])
- return (
- <>
- <Grid
- container
- border='1px dashed #616161'
- borderRadius='20px'
- padding='20px'
- >
- <Grid
- container
- justifyContent='space-between'
- alignItems='flex-end'
- >
- <Grid item xs={5}>
- <TextField
- fullWidth
- id="filled-basic"
- label="Title category"
- variant="standard"
- value={name}
- onChange={e => {
- setName(e.target.value);
- }}
- />
- </Grid>
- <Grid item xs={5}>
- {goods?.payload &&
- <Autocomplete
- multiple
- id="tags-standard"
- options={goods.payload}
- onChange={(event, newValue) => {
- setProducts([...newValue]);
- }}
- getOptionLabel={(option) => option?.name || 'no name'}
- key={option => option?.id}
- renderInput={(params) => (
- <TextField
- {...params}
- variant="standard"
- label="Select goods"
- placeholder="goods"
- />
- )}
- />
- }
- </Grid>
- </Grid>
- <Grid
- container
- justifyContent='space-between'
- marginTop='30px'
- marginBottom='10px'
- >
- <Grid item xs={5}>
- <Box
- display='flex'
- alignItems='center'
- >
- <Typography color='#616161'>Add subcategories</Typography>
- <Switch onChange={() =>
- setAddSub(!addSub)
- }/>
- </Box>
- </Grid>
- </Grid>
- {addSub &&
- <Grid
- container
- justifyContent='space-between'
- marginTop='30px'
- marginBottom='10px'
- >
- <AddSubCategory goods={goods} onCreate={value => setSubCategory({...value})}/>
- </Grid>
- }
- </Grid>
- </>
- )
- }
- const CategoryEdit = ({ category,
- categoryCount,
- goods,
- actionRootCat,
- onCountCategory,
- onGoodFind,
- onCreateCategory,
- categoryUpsert}) => {
- const [newCategory, setNewCategory] = useState({})
- const [result, setResult] = useState(false)
- useEffect(() => {
- if(!goods) onGoodFind()
- if(!category) actionRootCat()
- if(!categoryCount) onCountCategory()
- if(categoryUpsert) setResult(true)
- }, [category, categoryCount, goods, categoryUpsert])
- const handlerOnSave = () => {
- onCreateCategory(newCategory)
- onCountCategory()
- actionRootCat()
- }
- return(
- <>
- <Typography
- variant='h6'
- letterSpacing='2px'
- marginBottom='20px'
- >
- Total category: {categoryCount?.payload || 0}
- </Typography>
- {!result ?
- <>
- {goods &&
- <AddRootCategory
- goods={goods}
- onCreate={value => setNewCategory({...value})}
- />
- }
- <Grid
- container
- justifyContent='center'
- marginTop='30px'
- >
- <Grid item xs={4}>
- <Button
- disabled={Object.entries(newCategory).length === 0}
- fullWidth
- variant="outlined"
- onClick={handlerOnSave}
- >
- Save
- </Button>
- </Grid>
- </Grid>
- </>
- :
- <>
- <Typography
- variant='h5'
- textAlign='center'
- marginBottom='20px'
- >
- Category added successfully!!!
- </Typography>
- </>
- }
- </>
- )
- }
- export const CCategoryEdit = connect(state => ({
- category: state.category,
- categoryCount: state.promise['categoryCount'],
- goods: state.promise['goodAllFind'],
- categoryUpsert: state.promise['categoryUpsert']}),
- {
- actionRootCat: actionFullRootCats,
- onCountCategory: actionCategoryCount,
- onGoodFind: actionAllGoodFind,
- onCreateCategory: actionCategoryUpsert})
- (CategoryEdit)
|