CategoryEdit.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {useEffect, useState} from "react";
  2. import Typography from "@mui/material/Typography";
  3. import {Button, Checkbox, Grid, Switch, TextField} from "@mui/material";
  4. import {connect} from "react-redux";
  5. import {actionCategoryCount, actionCategoryUpsert, actionFullRootCats} from "../../../actions/ActionCategory";
  6. import {actionAllGoodFind} from "../../../actions/ActionGoodFind";
  7. import * as React from "react";
  8. import Autocomplete from "@mui/material/Autocomplete";
  9. import Box from "@mui/material/Box";
  10. import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline";
  11. import {AccordionItem} from "../../MyOrdersPage/AccordionItem";
  12. const AddSubCategory = ({goods, onCreate}) => {
  13. const [subCategory, setSubCategory] = useState({});
  14. useEffect(() => {
  15. onCreate({...subCategory})
  16. }, [subCategory])
  17. return (
  18. <>
  19. <AddRootCategory goods={goods} onCreate={value => {setSubCategory({...value})}}/>
  20. </>
  21. )
  22. }
  23. const AddRootCategory = ({goods, onCreate}) => {
  24. const [name, setName] = useState('');
  25. const [products, setProducts] = useState([]);
  26. const [addSub, setAddSub] = useState(false);
  27. const [addCat, setAddCat] = useState(false);
  28. const [subCategory, setSubCategory] = useState({});
  29. useEffect(() => {
  30. let obj = {}
  31. if (Object.entries(subCategory).length > 0) {
  32. obj['subCategories'] = [{...subCategory}]
  33. }
  34. if (products.length > 0){
  35. obj["goods"] = [...products]
  36. }
  37. if (name) {
  38. obj["name"] = name
  39. onCreate(obj)
  40. }
  41. }, [name, products, subCategory])
  42. return (
  43. <>
  44. <Grid
  45. container
  46. border='1px dashed #616161'
  47. borderRadius='20px'
  48. padding='20px'
  49. >
  50. <Grid
  51. container
  52. justifyContent='space-between'
  53. alignItems='flex-end'
  54. >
  55. <Grid item xs={5}>
  56. <TextField
  57. fullWidth
  58. id="filled-basic"
  59. label="Title category"
  60. variant="standard"
  61. value={name}
  62. onChange={e => {
  63. setName(e.target.value);
  64. }}
  65. />
  66. </Grid>
  67. <Grid item xs={5}>
  68. {goods?.payload &&
  69. <Autocomplete
  70. multiple
  71. id="tags-standard"
  72. options={goods.payload}
  73. onChange={(event, newValue) => {
  74. setProducts([...newValue]);
  75. }}
  76. getOptionLabel={(option) => option?.name || 'no name'}
  77. key={option => option?.id}
  78. renderInput={(params) => (
  79. <TextField
  80. {...params}
  81. variant="standard"
  82. label="Select goods"
  83. placeholder="goods"
  84. />
  85. )}
  86. />
  87. }
  88. </Grid>
  89. </Grid>
  90. <Grid
  91. container
  92. justifyContent='space-between'
  93. marginTop='30px'
  94. marginBottom='10px'
  95. >
  96. <Grid item xs={5}>
  97. <Box
  98. display='flex'
  99. alignItems='center'
  100. >
  101. <Typography color='#616161'>Add subcategories</Typography>
  102. <Switch onChange={() =>
  103. setAddSub(!addSub)
  104. }/>
  105. </Box>
  106. </Grid>
  107. </Grid>
  108. {addSub &&
  109. <Grid
  110. container
  111. justifyContent='space-between'
  112. marginTop='30px'
  113. marginBottom='10px'
  114. >
  115. <AddSubCategory goods={goods} onCreate={value => setSubCategory({...value})}/>
  116. </Grid>
  117. }
  118. </Grid>
  119. </>
  120. )
  121. }
  122. const CategoryEdit = ({category,
  123. categoryCount,
  124. goods,
  125. actionRootCat,
  126. onCountCategory,
  127. onGoodFind,
  128. onCreateCategory,
  129. categoryUpsert}) => {
  130. const [newCategory, setNewCategory] = useState({})
  131. const [result, setResult] = useState(false)
  132. useEffect(() => {
  133. if(!goods) onGoodFind()
  134. if(!category) actionRootCat()
  135. if(!categoryCount) onCountCategory()
  136. if(categoryUpsert) setResult(true)
  137. }, [category, categoryCount, goods, categoryUpsert])
  138. const handlerOnSave = () => {
  139. onCreateCategory(newCategory)
  140. onCountCategory()
  141. actionRootCat()
  142. }
  143. return(
  144. <>
  145. <Typography
  146. variant='h6'
  147. letterSpacing='2px'
  148. marginBottom='20px'
  149. >
  150. Total category: {categoryCount?.payload || 0}
  151. </Typography>
  152. {!result ?
  153. <>
  154. {goods &&
  155. <AddRootCategory
  156. goods={goods}
  157. onCreate={value => setNewCategory({...value})}
  158. />
  159. }
  160. <Grid
  161. container
  162. justifyContent='center'
  163. marginTop='30px'
  164. >
  165. <Grid item xs={4}>
  166. <Button
  167. disabled={Object.entries(newCategory).length === 0}
  168. fullWidth
  169. variant="outlined"
  170. onClick={handlerOnSave}
  171. >
  172. Save
  173. </Button>
  174. </Grid>
  175. </Grid>
  176. </>
  177. :
  178. <>
  179. <Typography
  180. variant='h5'
  181. textAlign='center'
  182. marginBottom='20px'
  183. >
  184. Category added successfully!!!
  185. </Typography>
  186. </>
  187. }
  188. </>
  189. )
  190. }
  191. export const CCategoryEdit = connect(state => ({category: state.category,
  192. categoryCount: state.promise['categoryCount'], goods: state.promise['goodAllFind'],
  193. categoryUpsert: state.promise['categoryUpsert'] }),
  194. {actionRootCat: actionFullRootCats, onCountCategory: actionCategoryCount,
  195. onGoodFind: actionAllGoodFind, onCreateCategory: actionCategoryUpsert})(CategoryEdit)