CateforyTab.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import * as React 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. import { enableRipple } from '@syncfusion/ej2-base';
  11. import { TreeViewComponent } from '@syncfusion/ej2-react-navigations';
  12. import {useEffect, useState} from "react";
  13. enableRipple(true);
  14. const InputCategory = ({goods, setArray, countSubCat}) => {
  15. const [name, setName] = useState('');
  16. const [products, setProducts] = useState([]);
  17. const [addSub, setAddSub] = useState(false);
  18. const [subArray, setSubArray] = useState([]);
  19. const [checked, setChecked] = useState(false);
  20. const handleChange = (event) => {
  21. setChecked(event.target.checked);
  22. if (!checked){
  23. setArray([{"name": name, "goods": products, "subcategory": subArray}])
  24. }
  25. };
  26. return (
  27. <>
  28. <Grid container justifyContent='space-between'>
  29. <Grid item xs={5}>
  30. <TextField fullWidth id="filled-basic" label="Title category" variant="standard" value={name} onChange={e => {setName(e.target.value); setChecked(false)}}/>
  31. </Grid>
  32. <Grid item xs={5}>
  33. {goods?.payload &&
  34. <Autocomplete
  35. multiple
  36. id="tags-standard"
  37. options={goods.payload}
  38. onChange={(event, newValue) => {
  39. setProducts([newValue]);
  40. setChecked(false)
  41. }}
  42. getOptionLabel={(option) => option?.name || 'no name'}
  43. key={option => option?.id}
  44. renderInput={(params) => (
  45. <TextField
  46. {...params}
  47. variant="standard"
  48. label="Select goods"
  49. placeholder="goods"
  50. />
  51. )}
  52. />
  53. }
  54. </Grid>
  55. <Grid item xs={1} display='flex' justifyContent='center' alignItems='flex-end'>
  56. <Checkbox
  57. checked={checked}
  58. onChange={handleChange}
  59. inputProps={{ 'aria-label': 'controlled' }}
  60. />
  61. </Grid>
  62. </Grid>
  63. <Grid container justifyContent='space-between' marginTop='30px' marginBottom='10px'>
  64. <Grid item xs={5.5}>
  65. {countSubCat < 2 &&
  66. <Box display='flex' alignItems='center'>
  67. <Typography color='#616161'>Add subcategories</Typography>
  68. <Switch onChange={() => {setAddSub(!addSub); setChecked(false)}}/>
  69. </Box>
  70. }
  71. </Grid>
  72. </Grid>
  73. {addSub && <AddCategory goods={goods} setArray={value => setSubArray(value)} addCategory={true} countSubCat={countSubCat+1}/>}
  74. </>
  75. )
  76. }
  77. const AddCategory = ({goods, setArray, addCategory, countSubCat}) => {
  78. const [name, setName] = useState('');
  79. const [products, setProducts] = useState([]);
  80. const [addSub, setAddSub] = useState(false);
  81. const [subArray, setSubArray] = useState([]);
  82. const [addCat, setAddCat] = useState(false);
  83. const [catCount, setCatCount] = useState(0);
  84. const [subCat, setSubCat] = useState([]);
  85. useEffect(() => {
  86. setArray([{"name": name, "goods": products, "subcategory": subArray}, {...subCat}])
  87. }, [name, products, subArray, subCat])
  88. console.log(subCat)
  89. return (
  90. <Box sx={{border: '1px dashed #616161', borderRadius: '20px', padding: '20px'}}>
  91. <Grid container justifyContent='space-between'>
  92. <Grid item xs={5.5}>
  93. <TextField fullWidth id="filled-basic" label="Title category" variant="standard" value={name} onChange={e => setName(e.target.value)}/>
  94. </Grid>
  95. <Grid item xs={5.5}>
  96. {goods?.payload &&
  97. <Autocomplete
  98. multiple
  99. id="tags-standard"
  100. options={goods.payload}
  101. onChange={(event, newValue) => {
  102. setProducts([newValue]);
  103. }}
  104. getOptionLabel={(option) => option?.name || 'no name'}
  105. key={option => option?.id}
  106. renderInput={(params) => (
  107. <TextField
  108. {...params}
  109. variant="standard"
  110. label="Select goods"
  111. placeholder="goods"
  112. />
  113. )}
  114. />
  115. }
  116. </Grid>
  117. </Grid>
  118. <Grid container justifyContent='space-between' marginTop='30px' marginBottom='10px'>
  119. <Grid item xs={5.5}>
  120. {countSubCat < 2 &&
  121. <Box display='flex' alignItems='center'>
  122. <Typography color='#616161'>Add subcategories</Typography>
  123. <Switch onChange={() => setAddSub(!addSub)}/>
  124. </Box>
  125. }
  126. </Grid>
  127. <Grid item xs={5.5} display='flex' justifyContent='space-between' alignItems='center'>
  128. {addCategory &&
  129. <Box display='flex' alignItems='center'>
  130. <Typography color='#616161'>Add categories</Typography>
  131. <Switch onChange={() => {setAddCat(!addCat); setCatCount(0)}}/>
  132. </Box>
  133. }
  134. {addCat && <SetCount defaultValue={1} width={40} height={40} onCount={value => setCatCount(value)}/>}
  135. </Grid>
  136. </Grid>
  137. {addSub && <AddCategory goods={goods} setArray={value => setSubArray(value)} addCategory={true} countSubCat={countSubCat+1}/>}
  138. {catCount >= 1 && new Array(catCount).fill(0).map(item => {
  139. return <InputCategory goods={goods} setArray={value => setSubCat([...subCat, value])} countSubCat={countSubCat}/>
  140. })
  141. }
  142. </Box>
  143. )
  144. }
  145. const CategoryEdit = ({category, categoryCount, goods, variant='create', actionRootCat, getCountCategory, getGoodFind, onCreateCategory}) => {
  146. const [array, setArray] = useState([])
  147. useEffect(() => {
  148. if(!goods) getGoodFind()
  149. if(!category) actionRootCat()
  150. if(!categoryCount) getCountCategory()
  151. }, [actionRootCat, category, categoryCount, getCountCategory, getGoodFind, goods])
  152. return(
  153. <>
  154. <Typography variant='h6' letterSpacing='2px' marginBottom='20px'>Total category: {categoryCount?.payload || 0}</Typography>
  155. <AddCategory goods={goods} setArray={value => setArray(value)} countSubCat={0}/>
  156. <Grid container justifyContent='center' marginTop='30px'>
  157. <Grid item xs={4}>
  158. <Button disabled={array.length === 0} fullWidth variant="outlined" onClick={() => onCreateCategory(array)}>
  159. Save
  160. </Button>
  161. </Grid>
  162. </Grid>
  163. </>
  164. )
  165. }
  166. 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)
  167. export class CCategoryEditTree extends React.Component {
  168. constructor() {
  169. super(...arguments);
  170. this.productTeam = [
  171. {
  172. id: 1, name: 'ASP.NET MVC Team', expanded: true,
  173. child: [
  174. { id: 2, pid: 1, name: 'Smith', isSelected: true },
  175. { id: 3, pid: 1, name: 'Johnson', isSelected: true },
  176. { id: 4, pid: 1, name: 'Anderson' },
  177. ]
  178. },
  179. {
  180. id: 5, name: 'Windows Team',
  181. child: [
  182. { id: 6, pid: 5, name: 'Clark' },
  183. { id: 7, pid: 5, name: 'Wright' },
  184. { id: 8, pid: 5, name: 'Lopez' },
  185. ]
  186. },
  187. {
  188. id: 9, name: 'Web Team',
  189. child: [
  190. { id: 11, pid: 9, name: 'Joshua' },
  191. { id: 12, pid: 9, name: 'Matthew' },
  192. { id: 13, pid: 9, name: 'David' },
  193. ]
  194. },
  195. {
  196. id: 14, name: 'Build Team',
  197. child: [
  198. { id: 15, pid: 14, name: 'Ryan' },
  199. { id: 16, pid: 14, name: 'Justin' },
  200. { id: 17, pid: 14, name: 'Robert' },
  201. ]
  202. },
  203. {
  204. id: 18, name: 'WPF Team',
  205. child: [
  206. { id: 19, pid: 18, name: 'Brown' },
  207. { id: 20, pid: 18, name: 'Johnson' },
  208. { id: 21, pid: 18, name: 'Miller' },
  209. ]
  210. }
  211. ];
  212. this.fields = { dataSource: this.productTeam, id: 'id', parentID: 'pid', text: 'name', hasChildren: 'hasChild', selected: 'isSelected' };
  213. this.allowDragAndDrop = true;
  214. this.allowMultiSelection = true;
  215. }
  216. render() {
  217. return (
  218. <TreeViewComponent fields={this.fields} allowMultiSelection={this.allowMultiSelection} allowDragAndDrop={this.allowDragAndDrop}/>
  219. );
  220. }
  221. }