Advertisement
genkid2020

Untitled

Aug 20th, 2020
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 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(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 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. const handbooksList = useSelector(({ settings }) => settings.handbooks.handbooks);
  101. const handbooksList1 = useSelector(({ settings }) => settings.handbooks.handbook);
  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 handbooks = {
  127. name: form.name,
  128. key: form.key
  129. };
  130.  
  131. store.dispatch(Actions.updateProjectProto(form.key, handbooks));
  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. {
  174. name: 'actions',
  175. label: 'Действия',
  176. options: {
  177. filter: false,
  178. sort: false,
  179. customBodyRenderLite: dataIndex => {
  180. return (
  181. <TableActions
  182. index={dataIndex}
  183. handbooksList={handbooksList}
  184. openEditModal={openEditModal}
  185. closeEditModal={closeEditModal}
  186. // deleteProto={deleteProto}
  187. />
  188. );
  189. },
  190. setCellHeaderProps: value => {
  191. return {
  192. style: {
  193. width: '150px'
  194. }
  195. };
  196. }
  197. }
  198. }
  199. ];
  200. const options = {
  201. filter: true,
  202. filterType: 'dropdown',
  203. responsive: 'scroll',
  204. tableBodyHeight: '100%',
  205. tableBodyMaxHeight: '100%',
  206. fixedSelectColumn: false,
  207. selectableRows: 'none',
  208. customToolbar: propsA => {
  209. return (
  210. <Button
  211. variant="contained"
  212. color="primary"
  213. className={classes.addButton}
  214. onClick={() => setIsAddOpen(true)}
  215. >
  216. Добавить
  217. </Button>
  218. );
  219. },
  220.  
  221. onRowClick: (rowIndex, rowMeta) => history.push(`/settings/handbooks/${handbooksList[rowMeta.dataIndex].key}`),
  222.  
  223. textLabels: {
  224. body: {
  225. noMatch: 'Данных нет',
  226. toolTip: 'Sort',
  227. columnHeaderTooltip: column => `Sort for ${column.label}`
  228. },
  229. pagination: {
  230. next: 'Следующая страница',
  231. previous: 'Предыдущая страница',
  232. rowsPerPage: 'Записей на странице:',
  233. displayRows: 'из'
  234. },
  235. toolbar: {
  236. search: 'Поиск',
  237. downloadCsv: 'Загрузить в облако',
  238. print: 'Печать',
  239. viewColumns: 'Посмотреть столбцы',
  240. filterTable: 'Фильтрация'
  241. },
  242. filter: {
  243. all: 'Все',
  244. title: 'ФИЛЬТРЫ',
  245. reset: 'Сбросить'
  246. },
  247. viewColumns: {
  248. title: 'Показать столбцы',
  249. titleAria: 'Show/Hide Table Columns'
  250. },
  251. selectedRows: {
  252. text: 'row(s) selected',
  253. delete: 'Delete',
  254. deleteAria: 'Delete Selected Rows'
  255. }
  256. }
  257. };
  258.  
  259. return isDataPending ? (
  260. <FuseLoading />
  261. ) : (
  262. <>
  263. <Paper className="md:m-32 m-12">
  264. <MuiThemeProvider
  265. theme={theme =>
  266. createMuiTheme({
  267. ...theme,
  268. overrides: {
  269. MUIDataTableToolbar: {
  270. root: {
  271. color: theme.palette.secondary.main
  272. }
  273. },
  274. MUIDataTableHeadCell: {
  275. root: {
  276. color: theme.palette.secondary.main
  277. }
  278. },
  279. MUIDataTableBodyCell: {
  280. root: {
  281. color: theme.palette.secondary.main
  282. }
  283. },
  284. MUIDataTableFilter: {
  285. title: {
  286. color: theme.palette.secondary.main,
  287. fontWeight: 'bold'
  288. },
  289. resetLink: {
  290. color: theme.palette.primary.main,
  291. fontWeight: 'normal'
  292. }
  293. }
  294. }
  295. })
  296. }
  297. >
  298. <MUIDataTable
  299. title="Список справочников"
  300. data={handbooksList}
  301. columns={columns}
  302. options={options}
  303. />
  304. </MuiThemeProvider>
  305. <Dialog open={isEditModalOpen} onClose={closeEditModal} aria-labelledby="form-dialog-title">
  306. <IconButton aria-label="close" className={classes.closeButton} onClick={closeEditModal}>
  307. <CloseIcon />
  308. </IconButton>
  309. <DialogTitle id="alert-dialog-title">Редактирование справочника</DialogTitle>
  310. <div className={classes.dialogContent}>
  311. <TextField
  312. className="mb-16"
  313. label="Название"
  314. autoFocus
  315. type="text"
  316. name="name"
  317. value={form.name}
  318. onChange={handleChange}
  319. variant="outlined"
  320. required
  321. fullWidth
  322. />
  323. <TextField
  324. className="mb-16"
  325. label="Код"
  326. autoFocus
  327. type="text"
  328. name="key"
  329. value={form.key}
  330. onChange={handleChange}
  331. variant="outlined"
  332. required
  333. fullWidth
  334. />
  335. </div>
  336. <DialogActions>
  337. <Button onClick={closeEditModal} color="primary">
  338. Отмена
  339. </Button>
  340. <Button onClick={updateHandbook} color="primary" autoFocus>
  341. Сохранить
  342. </Button>
  343. </DialogActions>
  344. </Dialog>
  345. </Paper>
  346. <AddHandbook isOpen={isAddOpen} close={() => setIsAddOpen(false)} />
  347. </>
  348. );
  349. }
  350.  
  351. export default Handbooks;
  352.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement