genkid2020

Untitled

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