genkid2020

Untitled

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