CategoryEdit.jsx 7.1 KB

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