genkid2020

Untitled

Aug 26th, 2020
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. import React, { useState, useEffect } from 'react';
  2. import history from '@history';
  3. import { makeStyles, createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
  4. import { useSelector } from 'react-redux';
  5. import MUIDataTable from 'mui-datatables';
  6. import FuseLoading from '@fuse/core/FuseLoading';
  7. import TextField from '@material-ui/core/TextField';
  8. import Paper from '@material-ui/core/Paper';
  9. import EditIcon from '@material-ui/icons/Edit';
  10. import DeleteIcon from '@material-ui/icons/Delete';
  11. import Dialog from '@material-ui/core/Dialog';
  12. import DialogActions from '@material-ui/core/DialogActions';
  13. import IconButton from '@material-ui/core/IconButton';
  14. import CloseIcon from '@material-ui/icons/Close';
  15. import DialogTitle from '@material-ui/core/DialogTitle';
  16. import store from 'app/store';
  17. import Button from '@material-ui/core/Button';
  18. import { openDialog, closeDialog, setToolbarTitle } from 'app/store/actions';
  19. import { useForm } from '@fuse/hooks';
  20. import { useLocation } from 'react-router';
  21. import * as Actions from './store/actions';
  22.  
  23.  
  24. const useStyles = makeStyles(theme => ({
  25. addButton: {
  26. position: 'absolute',
  27. top: '15px',
  28. right: '270px',
  29. width: '112px',
  30. fontSize: '14px',
  31. fontWeight: 'normal',
  32. [theme.breakpoints.down('xs')]: {
  33. position: 'static'
  34. }
  35. },
  36. closeButton: {
  37. position: 'absolute',
  38. right: theme.spacing(1),
  39. top: theme.spacing(1),
  40. color: theme.palette.grey[500]
  41. },
  42. dialogContent: {
  43. padding: '16px',
  44. width: '350px',
  45. [theme.breakpoints.down('xs')]: {
  46. width: '100%'
  47. }
  48. }
  49. }));
  50.  
  51. function TableActions(props) {
  52. const { productsList, index, openEditModal } = props;
  53. return (
  54. <div>
  55. <EditIcon className="text-black cursor-pointer" />
  56. <DeleteIcon
  57. className="md:ml-32 ml-12 text-black cursor-pointer"
  58. onClick={e => {
  59. e.stopPropagation();
  60. store.dispatch(
  61. openDialog({
  62. children: (
  63. <>
  64. <DialogTitle id="alert-dialog-title">
  65. {`Подтвердите удаление бизнес-процесса ${productsList[index].name}?`}
  66. </DialogTitle>
  67.  
  68. <DialogActions>
  69. <Button onClick={() => store.dispatch(closeDialog())} color="primary">
  70. Отмена
  71. </Button>
  72. <Button
  73. // onClick={() => deleteProto(protosList[index].id)}
  74. color="primary"
  75. autoFocus
  76. >
  77. Удалить
  78. </Button>
  79. </DialogActions>
  80. </>
  81. )
  82. })
  83. );
  84. }}
  85. />
  86. </div>
  87. );
  88. }
  89.  
  90. function Nomenclature(props) {
  91. const [isAddOpen, setIsAddOpen] = useState(false);
  92. const [isEditModalOpen, setIsEditModalOpen] = useState(false);
  93. const routeParams = useLocation();
  94. const classes = useStyles();
  95. const [currentType, setCurrentType] = useState('');
  96. const [tableTitle, setTableTitle] = useState('');
  97. useEffect(() => {
  98. const path = routeParams.pathname.split('/');
  99. const type = path[path.length - 1];
  100. if (type === 'products') {
  101. store.dispatch(Actions.getProductType());
  102. store.dispatch(setToolbarTitle('Товары'));
  103. setCurrentType('1');
  104. setTableTitle('Список товаров');
  105. } else if (type === 'materials') {
  106. store.dispatch(Actions.getMaterialType());
  107. store.dispatch(setToolbarTitle('Материалы'));
  108. setCurrentType('2');
  109. setTableTitle('Список материалов');
  110. } else if (type === 'metals') {
  111. store.dispatch(Actions.getMetalType());
  112. store.dispatch(setToolbarTitle('Металл'));
  113. setCurrentType('3');
  114. setTableTitle('Список металлов');
  115. } else if (type === 'furniture') {
  116. store.dispatch(Actions.getFurnitureType());
  117. store.dispatch(setToolbarTitle('Фурнитура'));
  118. setCurrentType('4');
  119. setTableTitle('Список фурнитуры');
  120. } else if (type === 'paint') {
  121. store.dispatch(Actions.getPaintType());
  122. store.dispatch(setToolbarTitle('Краска'));
  123. setCurrentType('5');
  124. setTableTitle('Список красок');
  125. } else if (type === 'details') {
  126. store.dispatch(Actions.getDetailType());
  127. store.dispatch(setToolbarTitle('Готовая продукция'));
  128. setCurrentType('6');
  129. setTableTitle('Список готовой продукции');
  130. }
  131. }, []);
  132.  
  133. const productsList = useSelector(({ nomenclature }) => nomenclature.nomenclature.type);
  134. const isTypesPending = useSelector(({ nomenclature }) => nomenclature.nomenclature.isTypesPending);
  135. const isDataPending = isTypesPending;
  136. const { form, handleChange, resetForm, setForm } = useForm({
  137. name: '',
  138. type: '',
  139. vendor_code: '',
  140. total_cost: ''
  141. });
  142. const openEditModal = index => {
  143. setForm(productsList[index]);
  144. setIsEditModalOpen(true);
  145. };
  146.  
  147. const closeEditModal = () => {
  148. resetForm();
  149. setIsEditModalOpen(false);
  150. };
  151. const typesName = id => {
  152. let name = id;
  153. const currentType = productsList.find(type => type.value === id);
  154. if (currentType) {
  155. name = currentType.name;
  156. }
  157. return name;
  158. };
  159.  
  160. const updateProduct = () => {
  161. const product = {
  162. name: form.name,
  163. type: form.type,
  164. vendor_code: form.vendor_code,
  165. total_cost: form.total_cost
  166. };
  167.  
  168. // store.dispatch(Actions.updateHandbook(form.key, handbook));
  169. closeEditModal();
  170. resetForm();
  171. };
  172.  
  173. const columns = [
  174. {
  175. name: '№',
  176. label: '',
  177. viewColumns: false,
  178. options: {
  179. filter: false,
  180. sort: false,
  181. customBodyRenderLite: dataIndex => {
  182. return <span>{dataIndex + 1}</span>;
  183. },
  184. setCellHeaderProps: value => {
  185. return {
  186. style: {
  187. width: '150px'
  188. }
  189. };
  190. }
  191. }
  192. },
  193. {
  194. name: 'name',
  195. label: 'Наименование',
  196. options: {
  197. filter: true,
  198. sort: false
  199. }
  200. },
  201. {
  202. name: 'type',
  203. label: 'Тип',
  204. options: {
  205. filter: true,
  206. sort: false,
  207. customBodyRender: value => typesName(value)
  208. }
  209. },
  210. {
  211. name: 'vendor_code',
  212. label: 'Код',
  213. options: {
  214. filter: true,
  215. sort: false
  216. }
  217. },
  218. {
  219. name: 'total_cost',
  220. label: 'Стоимость',
  221. options: {
  222. filter: true,
  223. sort: false
  224. }
  225. },
  226. {
  227. name: 'actions',
  228. label: 'Действия',
  229. options: {
  230. filter: false,
  231. sort: false,
  232. customBodyRenderLite: dataIndex => {
  233. return (
  234. <TableActions
  235. index={dataIndex}
  236. productsList={productsList}
  237. openEditModal={openEditModal}
  238. closeEditModal={closeEditModal}
  239. />
  240. );
  241. },
  242. setCellHeaderProps: value => {
  243. return {
  244. style: {
  245. width: '150px'
  246. }
  247. };
  248. }
  249. }
  250. }
  251. ];
  252. const options = {
  253. filter: true,
  254. filterType: 'dropdown',
  255. responsive: 'scroll',
  256. tableBodyHeight: '100%',
  257. tableBodyMaxHeight: '100%',
  258. fixedSelectColumn: false,
  259. selectableRows: 'none',
  260. customToolbar: propsA => {
  261. return (
  262. <Button
  263. variant="contained"
  264. color="primary"
  265. className={classes.addButton}
  266. onClick={() => setIsAddOpen(true)}
  267. >
  268. Добавить
  269. </Button>
  270. );
  271. },
  272.  
  273. // onRowClick: (rowIndex, rowMeta) => history.push(`/settings/handbooks/${handbooksList[rowMeta.dataIndex].key}`),
  274.  
  275. textLabels: {
  276. body: {
  277. noMatch: 'Данных нет',
  278. toolTip: 'Sort',
  279. columnHeaderTooltip: column => `Sort for ${column.label}`
  280. },
  281. pagination: {
  282. next: 'Следующая страница',
  283. previous: 'Предыдущая страница',
  284. rowsPerPage: 'Записей на странице:',
  285. displayRows: 'из'
  286. },
  287. toolbar: {
  288. search: 'Поиск',
  289. downloadCsv: 'Загрузить в облако',
  290. print: 'Печать',
  291. viewColumns: 'Посмотреть столбцы',
  292. filterTable: 'Фильтрация'
  293. },
  294. filter: {
  295. all: 'Все',
  296. title: 'ФИЛЬТРЫ',
  297. reset: 'Сбросить'
  298. },
  299. viewColumns: {
  300. title: 'Показать столбцы',
  301. titleAria: 'Show/Hide Table Columns'
  302. },
  303. selectedRows: {
  304. text: 'row(s) selected',
  305. delete: 'Delete',
  306. deleteAria: 'Delete Selected Rows'
  307. }
  308. }
  309. };
  310.  
  311. return isDataPending ? (
  312. <FuseLoading />
  313. ) : (
  314. <>
  315. <Paper className="md:m-32 m-12">
  316. <MuiThemeProvider
  317. theme={theme =>
  318. createMuiTheme({
  319. ...theme,
  320. overrides: {
  321. MUIDataTableToolbar: {
  322. root: {
  323. color: theme.palette.secondary.main
  324. }
  325. },
  326. MUIDataTableHeadCell: {
  327. root: {
  328. color: theme.palette.secondary.main
  329. }
  330. },
  331. MUIDataTableBodyCell: {
  332. root: {
  333. color: theme.palette.secondary.main
  334. }
  335. },
  336. MUIDataTableFilter: {
  337. title: {
  338. color: theme.palette.secondary.main,
  339. fontWeight: 'bold'
  340. },
  341. resetLink: {
  342. color: theme.palette.primary.main,
  343. fontWeight: 'normal'
  344. }
  345. }
  346. }
  347. })
  348. }
  349. >
  350. <MUIDataTable
  351. title="Список изделий и материалов"
  352. data={productsList}
  353. columns={columns}
  354. options={options}
  355. />
  356. </MuiThemeProvider>
  357. <Dialog open={isEditModalOpen} onClose={closeEditModal} aria-labelledby="form-dialog-title">
  358. <IconButton aria-label="close" className={classes.closeButton} onClick={closeEditModal}>
  359. <CloseIcon />
  360. </IconButton>
  361. <DialogTitle id="alert-dialog-title">Редактирование справочника</DialogTitle>
  362. <div className={classes.dialogContent}>
  363. <TextField
  364. className="mb-16"
  365. label="Наименование"
  366. autoFocus
  367. type="text"
  368. name="name"
  369. value={form.name}
  370. onChange={handleChange}
  371. variant="outlined"
  372. required
  373. fullWidth
  374. />
  375. <TextField
  376. className="mb-16"
  377. label="Тип"
  378. autoFocus
  379. type="text"
  380. name="type"
  381. value={form.type}
  382. onChange={handleChange}
  383. variant="outlined"
  384. required
  385. fullWidth
  386. />
  387. <TextField
  388. className="mb-16"
  389. label="Код"
  390. autoFocus
  391. type="text"
  392. name="vendor_code"
  393. value={form.vendor_code}
  394. onChange={handleChange}
  395. variant="outlined"
  396. required
  397. fullWidth
  398. />
  399. <TextField
  400. className="mb-16"
  401. label="Стоимость"
  402. autoFocus
  403. type="text"
  404. name="total_cost"
  405. value={form.total_cost}
  406. onChange={handleChange}
  407. variant="outlined"
  408. required
  409. fullWidth
  410. />
  411. </div>
  412. <DialogActions>
  413. <Button onClick={closeEditModal} color="primary">
  414. Отмена
  415. </Button>
  416. {/* <Button onClick={updateHandbook} color="primary" autoFocus> */}
  417. {/* Сохранить */}
  418. {/* </Button> */}
  419. </DialogActions>
  420. </Dialog>
  421. </Paper>
  422. {/* <AddHandbook isOpen={isAddOpen} close={() => setIsAddOpen(false)} /> */}
  423. </>
  424. );
  425. }
  426.  
  427. export default Nomenclature;
  428.  
Advertisement
Add Comment
Please, Sign In to add comment