CateforyTab.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {useEffect, useState} from "react";
  2. import {Button, Checkbox, Grid, Switch, TextField} from "@mui/material";
  3. import Autocomplete from "@mui/material/Autocomplete";
  4. import Box from "@mui/material/Box";
  5. import Typography from "@mui/material/Typography";
  6. import {SetCount} from "../../components/SetCount";
  7. import {connect} from "react-redux";
  8. import {actionCategoryCount, actionCategoryUpsert, actionFullRootCats} from "../../actions/ActionCategory";
  9. import {actionAllGoodFind} from "../../actions/ActionGoodFind";
  10. const InputCategory = ({goods, setArray, countSubCat}) => {
  11. const [name, setName] = useState('');
  12. const [products, setProducts] = useState([]);
  13. const [addSub, setAddSub] = useState(false);
  14. const [subArray, setSubArray] = useState([]);
  15. const [checked, setChecked] = useState(false);
  16. const handleChange = (event) => {
  17. setChecked(event.target.checked);
  18. if (!checked){
  19. setArray([{"name": name, "goods": products, "subcategory": subArray}])
  20. }
  21. };
  22. return (
  23. <>
  24. <Grid container justifyContent='space-between'>
  25. <Grid item xs={5}>
  26. <TextField fullWidth id="filled-basic" label="Title category" variant="standard" value={name} onChange={e => {setName(e.target.value); setChecked(false)}}/>
  27. </Grid>
  28. <Grid item xs={5}>
  29. {goods?.payload &&
  30. <Autocomplete
  31. multiple
  32. id="tags-standard"
  33. options={goods.payload}
  34. onChange={(event, newValue) => {
  35. setProducts([newValue]);
  36. setChecked(false)
  37. }}
  38. getOptionLabel={(option) => option?.name || 'no name'}
  39. key={option => option?.id}
  40. renderInput={(params) => (
  41. <TextField
  42. {...params}
  43. variant="standard"
  44. label="Select goods"
  45. placeholder="goods"
  46. />
  47. )}
  48. />
  49. }
  50. </Grid>
  51. <Grid item xs={1} display='flex' justifyContent='center' alignItems='flex-end'>
  52. <Checkbox
  53. checked={checked}
  54. onChange={handleChange}
  55. inputProps={{ 'aria-label': 'controlled' }}
  56. />
  57. </Grid>
  58. </Grid>
  59. <Grid container justifyContent='space-between' marginTop='30px' marginBottom='10px'>
  60. <Grid item xs={5.5}>
  61. {countSubCat < 2 &&
  62. <Box display='flex' alignItems='center'>
  63. <Typography color='#616161'>Add subcategories</Typography>
  64. <Switch onChange={() => {setAddSub(!addSub); setChecked(false)}}/>
  65. </Box>
  66. }
  67. </Grid>
  68. </Grid>
  69. {addSub && <AddCategory goods={goods} setArray={value => setSubArray(value)} addCategory={true} countSubCat={countSubCat+1}/>}
  70. </>
  71. )
  72. }
  73. const AddCategory = ({goods, setArray, addCategory, countSubCat}) => {
  74. const [name, setName] = useState('');
  75. const [products, setProducts] = useState([]);
  76. const [addSub, setAddSub] = useState(false);
  77. const [subArray, setSubArray] = useState([]);
  78. const [addCat, setAddCat] = useState(false);
  79. const [catCount, setCatCount] = useState(0);
  80. const [subCat, setSubCat] = useState([]);
  81. useEffect(() => {
  82. setArray([{"name": name, "goods": products, "subcategory": subArray}, {...subCat}])
  83. }, [name, products, subArray, subCat])
  84. console.log(subCat)
  85. return (
  86. <Box sx={{border: '1px dashed #616161', borderRadius: '20px', padding: '20px'}}>
  87. <Grid container justifyContent='space-between'>
  88. <Grid item xs={5.5}>
  89. <TextField fullWidth id="filled-basic" label="Title category" variant="standard" value={name} onChange={e => setName(e.target.value)}/>
  90. </Grid>
  91. <Grid item xs={5.5}>
  92. {goods?.payload &&
  93. <Autocomplete
  94. multiple
  95. id="tags-standard"
  96. options={goods.payload}
  97. onChange={(event, newValue) => {
  98. setProducts([newValue]);
  99. }}
  100. getOptionLabel={(option) => option?.name || 'no name'}
  101. key={option => option?.id}
  102. renderInput={(params) => (
  103. <TextField
  104. {...params}
  105. variant="standard"
  106. label="Select goods"
  107. placeholder="goods"
  108. />
  109. )}
  110. />
  111. }
  112. </Grid>
  113. </Grid>
  114. <Grid container justifyContent='space-between' marginTop='30px' marginBottom='10px'>
  115. <Grid item xs={5.5}>
  116. {countSubCat < 2 &&
  117. <Box display='flex' alignItems='center'>
  118. <Typography color='#616161'>Add subcategories</Typography>
  119. <Switch onChange={() => setAddSub(!addSub)}/>
  120. </Box>
  121. }
  122. </Grid>
  123. <Grid item xs={5.5} display='flex' justifyContent='space-between' alignItems='center'>
  124. {addCategory &&
  125. <Box display='flex' alignItems='center'>
  126. <Typography color='#616161'>Add categories</Typography>
  127. <Switch onChange={() => {setAddCat(!addCat); setCatCount(0)}}/>
  128. </Box>
  129. }
  130. {addCat && <SetCount defaultValue={1} width={40} height={40} onCount={value => setCatCount(value)}/>}
  131. </Grid>
  132. </Grid>
  133. {addSub && <AddCategory goods={goods} setArray={value => setSubArray(value)} addCategory={true} countSubCat={countSubCat+1}/>}
  134. {catCount >= 1 && new Array(catCount).fill(0).map(item => {
  135. return <InputCategory goods={goods} setArray={value => setSubCat([...subCat, value])} countSubCat={countSubCat}/>
  136. })
  137. }
  138. </Box>
  139. )
  140. }
  141. const CategoryEdit = ({category, categoryCount, goods, variant='create', actionRootCat, getCountCategory, getGoodFind, onCreateCategory}) => {
  142. const [array, setArray] = useState([])
  143. useEffect(() => {
  144. if(!goods) getGoodFind()
  145. if(!category) actionRootCat()
  146. if(!categoryCount) getCountCategory()
  147. }, [actionRootCat, category, categoryCount, getCountCategory, getGoodFind, goods])
  148. return(
  149. <>
  150. <Typography variant='h6' letterSpacing='2px' marginBottom='20px'>Total category: {categoryCount?.payload || 0}</Typography>
  151. <AddCategory goods={goods} setArray={value => setArray(value)} countSubCat={0}/>
  152. <Grid container justifyContent='center' marginTop='30px'>
  153. <Grid item xs={4}>
  154. <Button disabled={array.length === 0} fullWidth variant="outlined" onClick={() => onCreateCategory(array)}>
  155. Save
  156. </Button>
  157. </Grid>
  158. </Grid>
  159. </>
  160. )
  161. }
  162. export const CCategoryEdit = connect(state => ({category: state.category, categoryCount: state.promise['categoryCount'], goods: state.promise['goodAllFind'] }), {actionRootCat: actionFullRootCats, getCountCategory: actionCategoryCount, getGoodFind: actionAllGoodFind, onCreateCategory: actionCategoryUpsert})(CategoryEdit)