GoodEdit.jsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import React from 'react';
  2. import {useEffect, useState} from "react";
  3. import {useDropzone} from "react-dropzone";
  4. import {sortableContainer, sortableElement} from "react-sortable-hoc";
  5. import Box from "@mui/material/Box";
  6. import {backURL} from "../../../actions/PathDB";
  7. import {Button, CircularProgress, Grid, TextField} from "@mui/material";
  8. import {arrayMoveImmutable} from "array-move";
  9. import Typography from "@mui/material/Typography";
  10. import Autocomplete from "@mui/material/Autocomplete";
  11. import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline";
  12. import {Link} from "react-router-dom";
  13. import {connect} from "react-redux";
  14. import {actionAllCategory} from "../../../actions/ActionCategory";
  15. import {actionGoodUpsert} from "../../../actions/ActionCreateGood";
  16. import {actionGoodCount} from "../../../actions/ActionGoodFind";
  17. import {actionUploadFiles} from "../../../actions/ActionUploadFile";
  18. import {actionClearPromise} from "../../../reducers/PromiseReducer";
  19. const GoodEdit = ({entity={images: [], categories: []},
  20. onSave,
  21. onFileDrop,
  22. fileStatus,
  23. categoryState,
  24. actionRootCat,
  25. goodCount,
  26. goods,
  27. actionClear,
  28. result}) => {
  29. const [state, setState] = useState(entity)
  30. const {getRootProps, getInputProps, isDragActive} = useDropzone({
  31. accept: 'image/*', onDrop: acceptedFiles => {
  32. // acceptedFiles.forEach(async file => {
  33. // await onFileDrop(file)
  34. // })
  35. onFileDrop(acceptedFiles)
  36. }})
  37. const SortableItem = sortableElement(({value}) => {
  38. return (
  39. <Box
  40. key={value?._id}
  41. sx={{
  42. display: 'flex',
  43. justifyContent: 'center',
  44. borderRadius: 2,
  45. border: '1px solid #eaeaea',
  46. marginBottom: 2,
  47. width: 200,
  48. height: 200,
  49. padding: '5px',
  50. boxSizing: 'border-box'
  51. }}
  52. >
  53. <Box
  54. sx={{
  55. display: 'flex',
  56. justifyContent: 'center',
  57. minWidth: 0,
  58. overflow: 'hidden',
  59. position: 'relative'
  60. }}
  61. >
  62. {value?.url ?
  63. <>
  64. <img
  65. src={backURL+ '/' + value.url}
  66. style={{
  67. display: 'block',
  68. width: 'auto',
  69. height: '100%',
  70. objectFit: 'cover',
  71. objectPosition: 'center center'
  72. }}
  73. alt={value.name}
  74. />
  75. </> :
  76. <Box
  77. sx={{
  78. display: 'flex',
  79. justifyContent: 'center',
  80. alignItems: 'center'
  81. }}
  82. >
  83. <CircularProgress />
  84. </Box>
  85. }
  86. </Box>
  87. </Box>
  88. )
  89. });
  90. const SortableContainer = sortableContainer(({children}) => {
  91. return (
  92. <aside
  93. style={{
  94. display:'flex',
  95. justifyContent: 'space-between',
  96. flexWrap: 'wrap'
  97. }}
  98. >
  99. {children}
  100. </aside>
  101. )
  102. })
  103. const onSortEnd = ({oldIndex, newIndex}) => {
  104. setState(({images}) => ({
  105. ...state,
  106. images: arrayMoveImmutable(images, oldIndex, newIndex),
  107. }));
  108. }
  109. const handleClear = () => {
  110. setState(entity)
  111. }
  112. const handleOnSave = () => {
  113. let query = {...state}
  114. state.images?.length > 0 ?
  115. query.images = state.images.map(item => {return {'_id': item['_id']}})
  116. :
  117. delete query.images
  118. state.categories?.length > 0 ?
  119. query.categories = state.categories.map(item => {return {'_id': item['_id'], 'name': item['name']}})
  120. :
  121. delete query.categories
  122. onSave(query)
  123. goodCount()
  124. }
  125. const handleFullClear = () => {
  126. goodCount()
  127. setState(entity)
  128. actionClear('goodUpsert')
  129. actionClear('uploadFile')
  130. }
  131. useEffect(() => {
  132. if(!categoryState) actionRootCat()
  133. if(!goods) goodCount()
  134. if(fileStatus?.status === 'RESOLVED'){
  135. state.images?.length > 0 ?
  136. setState({...state, images: [...state.images, fileStatus?.payload]})
  137. :
  138. setState({...state, images: [fileStatus?.payload]})
  139. }
  140. },[categoryState, goods, fileStatus])
  141. return (
  142. <>
  143. {!result ?
  144. <>
  145. <Typography
  146. variant='h6'
  147. letterSpacing='2px'
  148. marginBottom='20px'
  149. >
  150. Total products: {goods?.payload || 0}
  151. </Typography>
  152. <Box
  153. style={{
  154. minHeight: "200px",
  155. border: '1px dashed #616161',
  156. borderRadius: '20px',
  157. padding: '20px'
  158. }}
  159. {...getRootProps()}
  160. >
  161. <input {...getInputProps()} />
  162. {isDragActive ?
  163. <Typography
  164. variant='body1'
  165. textAlign='center'
  166. color='#616161'
  167. >
  168. Drop the file here ...
  169. </Typography>
  170. :
  171. <Typography
  172. variant='body1'
  173. textAlign='center'
  174. color='#616161'
  175. marginBottom='20px'
  176. >
  177. Drag 'n' drop image files here, or click to select file
  178. </Typography>
  179. }
  180. <SortableContainer
  181. axis="xy"
  182. onSortEnd={onSortEnd}
  183. >
  184. {state.images?.length > 0 && state.images.map((value, index) => (
  185. <SortableItem
  186. key={`item-${value?._id || index}`}
  187. index={index}
  188. value={value}
  189. />
  190. ))}
  191. </SortableContainer>
  192. </Box>
  193. <Grid
  194. container
  195. justifyContent='space-between'
  196. alignItems='flex-end'
  197. marginTop='30px'
  198. >
  199. <Grid item xs={5.5}>
  200. <TextField
  201. fullWidth
  202. id="filled-basic"
  203. label="Title product"
  204. variant="standard"
  205. value={state?.name || ''}
  206. onChange={e => setState({...state, name: e.target.value})}
  207. />
  208. </Grid>
  209. <Grid item xs={5.5}>
  210. {categoryState && categoryState?.payload && categoryState.payload?.length > 0 &&
  211. <>
  212. {state.categories?.length > 0 ?
  213. <Autocomplete
  214. multiple
  215. id="tags-standard"
  216. options={Object.values(categoryState.payload)}
  217. defaultValue={state.categories}
  218. onChange={(event, newValue) => {
  219. setState({...state, categories: [...newValue]})
  220. }}
  221. getOptionLabel={(option) => option?.name || 'no name'}
  222. key={option => option?.id}
  223. renderInput={(params) => (
  224. <TextField
  225. {...params}
  226. variant="standard"
  227. label="Select categories"
  228. placeholder="categories"
  229. />
  230. )}
  231. /> :
  232. <Autocomplete
  233. multiple
  234. id="tags-standard"
  235. options={Object.values(categoryState.payload)}
  236. onChange={(event, newValue) => {
  237. setState({...state, categories: [...newValue]})
  238. }}
  239. getOptionLabel={(option) => option?.name || 'no name'}
  240. key={option => option?.id}
  241. renderInput={(params) => (
  242. <TextField
  243. {...params}
  244. variant="standard"
  245. label="Select categories"
  246. placeholder="categories"
  247. />
  248. )}
  249. />
  250. }
  251. </>
  252. }
  253. </Grid>
  254. </Grid>
  255. <Grid
  256. container
  257. justifyContent='space-between'
  258. alignItems='flex-end'
  259. marginTop='30px'
  260. >
  261. <Grid item xs={5.5}>
  262. <TextField fullWidth
  263. id='Price'
  264. type='number'
  265. label='Price'
  266. variant='standard'
  267. value={state?.price || ''}
  268. onChange={e => setState({...state,
  269. price: parseFloat(e.target.value < 0 ? 0 : e.target.value)})
  270. }
  271. />
  272. </Grid>
  273. <Grid item xs={5.5}>
  274. <TextField fullWidth
  275. id='filled-basic'
  276. label='Description product'
  277. variant='standard'
  278. multiline
  279. value={state?.description || ''}
  280. onChange={e => setState({...state, description: e.target.value})}
  281. />
  282. </Grid>
  283. </Grid>
  284. <Grid
  285. container
  286. justifyContent='space-between'
  287. marginTop='30px'
  288. >
  289. <Grid
  290. item xs={5.5}
  291. display='flex'
  292. justifyContent='center'
  293. >
  294. <Button
  295. fullWidth
  296. onClick={handleClear}
  297. variant="outlined"
  298. color='warning'
  299. >
  300. Clear
  301. </Button>
  302. </Grid>
  303. <Grid
  304. item xs={5.5}
  305. display='flex'
  306. justifyContent='center'
  307. >
  308. <Button
  309. fullWidth
  310. variant="outlined"
  311. color='primary'
  312. onClick={handleOnSave}
  313. >
  314. Save
  315. </Button>
  316. </Grid>
  317. </Grid>
  318. </> :
  319. result?.payload?._id ?
  320. <>
  321. <Box
  322. display='flex'
  323. alignItems='center'
  324. flexDirection='column'
  325. >
  326. <Typography
  327. variant='h5'
  328. letterSpacing='2px'
  329. textAlign='center'
  330. color='#616161'
  331. marginBottom='20px'
  332. >
  333. Product successfully created!
  334. </Typography>
  335. <CheckCircleOutlineIcon sx={{marginBottom: '20px'}}/>
  336. <Link
  337. to={`/good/${result.payload._id}`}
  338. style={{
  339. color:'#616161',
  340. marginBottom:'20px'
  341. }}
  342. >
  343. <Typography
  344. variant='h5'
  345. letterSpacing='2px'
  346. textAlign='center'
  347. color='#616161'
  348. >
  349. View results
  350. </Typography>
  351. </Link>
  352. <Button
  353. variant='outlined'
  354. onClick={handleFullClear}
  355. >
  356. Add more
  357. </Button>
  358. </Box>
  359. </> :
  360. result?.error ?
  361. <Box
  362. display='flex'
  363. alignItems='center'
  364. flexDirection='column'
  365. >
  366. <Typography
  367. variant='h5'
  368. letterSpacing='2px'
  369. textAlign='center'
  370. color='#f00'
  371. marginBottom='20px'
  372. >
  373. Fatal error, try again!
  374. </Typography>
  375. <Button
  376. variant='outlined'
  377. onClick={handleFullClear}
  378. >
  379. Add more
  380. </Button>
  381. </Box>
  382. :
  383. <Box sx={{ display: 'flex' }}>
  384. <CircularProgress />
  385. </Box>
  386. }
  387. </>
  388. )
  389. }
  390. export const CGoodEdit = connect(state => ({
  391. fileStatus: state.promise['uploadFile'],
  392. categoryState: state.promise['allCategory'],
  393. goods: state.promise['goodCount'],
  394. result: state.promise['goodUpsert']}),
  395. {
  396. actionRootCat: actionAllCategory,
  397. onSave: actionGoodUpsert,
  398. goodCount: actionGoodCount,
  399. onFileDrop: actionUploadFiles,
  400. actionClear: actionClearPromise})
  401. (GoodEdit)