Advertisement
genkid2020

Untitled

Aug 20th, 2020
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. import React, { useState, useEffect } from 'react';
  2. import history from '@history';
  3. import { useForm, useDeepCompareEffect } from '@fuse/hooks';
  4. import { makeStyles, withStyles } from '@material-ui/core/styles';
  5. import { useDispatch, useSelector } from 'react-redux';
  6. import { useParams } from 'react-router-dom';
  7. import Icon from '@material-ui/core/Icon';
  8. import TextField from '@material-ui/core/TextField';
  9. import EditIcon from '@material-ui/icons/Edit';
  10. import DeleteIcon from '@material-ui/icons/Delete';
  11. import FuseLoading from '@fuse/core/FuseLoading';
  12.  
  13. import MenuItem from '@material-ui/core/MenuItem';
  14. import { fromObject } from 'http-cache-semantics';
  15. import Table from '@material-ui/core/Table';
  16. import TableBody from '@material-ui/core/TableBody';
  17. import TableCell from '@material-ui/core/TableCell';
  18. import TableContainer from '@material-ui/core/TableContainer';
  19. import TableHead from '@material-ui/core/TableHead';
  20. import TableRow from '@material-ui/core/TableRow';
  21. import * as Actions from '../../store/actions';
  22.  
  23. // import AddNewProto from './AddNewProto';
  24. // import EditProto from './EditProto';
  25.  
  26. const useStyles = makeStyles(theme => ({
  27. mainBlock: {
  28. marginRight: '32px',
  29.  
  30. width: '50%',
  31. display: 'table',
  32. [theme.breakpoints.down('xs')]: {
  33. marginRight: '0',
  34. minWidth: '100%'
  35. }
  36. },
  37. cardWrapper: {
  38. marginBottom: '32px',
  39. flex: 1,
  40. display: 'flex',
  41. flexDirection: 'column',
  42. border: '1px solid #E0E0E0',
  43. borderRadius: '4px',
  44. backgroundColor: theme.palette.background.paper,
  45. [theme.breakpoints.down('xs')]: {
  46. marginRight: '0',
  47. minWidth: '100%'
  48. }
  49. },
  50. cardHeader: {
  51. height: '60px',
  52. padding: '18px 24px',
  53. display: 'flex',
  54. justifyContent: 'space-between',
  55. color: theme.palette.secondary.main,
  56. borderBottom: '1px solid #E0E0E0',
  57. [theme.breakpoints.down('xs')]: {
  58. height: 'fit-content',
  59. flexWrap: 'wrap'
  60. }
  61. },
  62. cardHeaderEditButton: {
  63. display: 'flex',
  64. alignItems: 'center',
  65. cursor: 'pointer',
  66. color: theme.palette.primary.main,
  67. [theme.breakpoints.down('xs')]: {
  68. marginTop: '10px'
  69. }
  70. },
  71. cardHeaderCancelButton: {
  72. marginRight: '10px',
  73. display: 'flex',
  74. alignItems: 'center',
  75. cursor: 'pointer',
  76. color: theme.palette.error.main,
  77. [theme.breakpoints.down('xs')]: {
  78. marginTop: '10px'
  79. }
  80. },
  81. cardHeaderAddButton: {
  82. display: 'flex',
  83. alignItems: 'center',
  84. cursor: 'pointer',
  85. color: theme.palette.primary.main
  86. },
  87. cardHeaderSaveButton: {
  88. display: 'flex',
  89. alignItems: 'center',
  90. cursor: 'pointer',
  91. color: theme.palette.success.main,
  92. [theme.breakpoints.down('xs')]: {
  93. marginTop: '10px'
  94. }
  95. },
  96. cardContent: {
  97. padding: '23px',
  98. display: 'flex',
  99. flexWrap: 'wrap',
  100. justifyContent: 'space-between'
  101. },
  102. cardTable: {
  103. padding: '0 23px 48px 23px',
  104. [theme.breakpoints.down('xs')]: {
  105. padding: '5px'
  106. }
  107. }
  108. }));
  109.  
  110. const StyledTableCell = withStyles(theme => ({
  111. head: {
  112. color: theme.palette.secondary.main,
  113. paddingLeft: 0
  114. },
  115. body: {
  116. color: theme.palette.secondary.main,
  117. paddingLeft: 0
  118. }
  119. }))(TableCell);
  120.  
  121. const ActionsTableCell = withStyles(theme => ({
  122. body: {
  123. color: theme.palette.secondary.main,
  124. width: '100px'
  125. }
  126. }))(TableCell);
  127.  
  128. export default function Handbook(props) {
  129. const [isEditName, setIsEditName] = useState(false);
  130. const [isEditKey, setIsEditKey] = useState(false);
  131. const [currentForm, setCurrentForm] = useState('');
  132. const [stageEditIndex, setStageEditIndex] = useState('');
  133.  
  134. const { form, handleChange, setForm, setInForm } = useForm({
  135. name: '',
  136. key: '',
  137. item: []
  138. });
  139.  
  140. const classes = useStyles(props);
  141. const dispatch = useDispatch();
  142. const routeParams = useParams();
  143.  
  144. const handbook = useSelector(({ settings }) => settings.handbooks.handbook);
  145. // const stagesList = useSelector(({ settings }) => settings.projectProto.stageProtosList);
  146. const isGetHandbookError = useSelector(({ settings }) => settings.handbooks.isGetHandbookError);
  147. const isHandbookPending = useSelector(({ settings }) => settings.handbooks.isHandbookPending);
  148. // const isStagePending = useSelector(({ settings }) => settings.projectProto.isStagePending);
  149. const isDataPending = isHandbookPending;
  150.  
  151. useEffect(() => {
  152. const { key } = routeParams;
  153. dispatch(Actions.getHandbook(key));
  154. // dispatch(Actions.getStageProtos());
  155. }, []);
  156.  
  157. useEffect(() => {
  158. setForm(handbook);
  159. }, [handbook]);
  160.  
  161. useEffect(() => {
  162. if (isGetHandbookError) {
  163. history.push('/error-404');
  164. }
  165. }, [isGetHandbookError]);
  166.  
  167. const saveData = () => {
  168. if (form.name !== handbook.name || form.key !== handbook.key) {
  169. const data = {
  170. name: form.name,
  171. key: form.key
  172. };
  173. // dispatch(Actions.updateProjectProto(proto.id, data));
  174. }
  175. setIsEditName(false);
  176. };
  177.  
  178. const openAddForm = () => {
  179. setStageEditIndex('');
  180. setCurrentForm('addNew');
  181. };
  182.  
  183. const openEditForm = index => {
  184. setStageEditIndex(index);
  185. setCurrentForm('edit');
  186. };
  187.  
  188. // const deleteStageFromProto = index => {
  189. // const stageId = form.item[index].id;
  190. // dispatch(Actions.deleteStageOfProject(proto.id, stageId));
  191. // };
  192.  
  193. return isDataPending ? (
  194. <FuseLoading />
  195. ) : (
  196. <div className="md:p-32 p-12 flex flex-wrap">
  197. <div className={classes.mainBlock}>
  198. <div className={classes.cardWrapper}>
  199. <div className={classes.cardHeader}>
  200. <h4>Информация о справочнике</h4>
  201. {!isEditName || !isEditKey ? (
  202. <span
  203. className={classes.cardHeaderEditButton}
  204. onClick={() => setIsEditName(true)}
  205. role="button"
  206. >
  207. <Icon>edit</Icon>
  208. Редактировать
  209. </span>
  210. ) : (
  211. <div className="flex justify-between">
  212. <span className={classes.cardHeaderSaveButton} onClick={saveData}>
  213. <Icon>check</Icon>
  214. Сохранить
  215. </span>
  216. </div>
  217. )}
  218. </div>
  219. <div className={classes.cardContent}>
  220. <TextField
  221. className="mb-20"
  222. label="Наименование справочника"
  223. type="text"
  224. name="name"
  225. value={form.name}
  226. onChange={handleChange}
  227. variant="outlined"
  228. fullWidth
  229. required
  230. disabled={!isEditName}
  231. />
  232. <TextField
  233. className="mb-20"
  234. label="Код справочника"
  235. type="text"
  236. name="key"
  237. value={form.key}
  238. onChange={handleChange}
  239. variant="outlined"
  240. fullWidth
  241. required
  242. disabled={!isEditKey}
  243. />
  244. </div>
  245. </div>
  246. <div className={classes.cardWrapper}>
  247. <div className={classes.cardHeader}>
  248. <h4>Этапы</h4>
  249. <span className={classes.cardHeaderAddButton} onClick={openAddForm} role="button">
  250. <Icon>add</Icon>
  251. Добавить этап
  252. </span>
  253. </div>
  254. <div className={classes.cardTable}>
  255. <TableContainer>
  256. <Table color="secondary" aria-label="simple table">
  257. <TableHead>
  258. <TableRow>
  259. <StyledTableCell>Наименование</StyledTableCell>
  260. <StyledTableCell>Порядок</StyledTableCell>
  261. <StyledTableCell>Действия</StyledTableCell>
  262. </TableRow>
  263. </TableHead>
  264. <TableBody>
  265. {form.item &&
  266. form.item.map((row, index) => (
  267. <TableRow key={row.id}>
  268. <StyledTableCell component="th" scope="row">
  269. {row.name}
  270. </StyledTableCell>
  271. <StyledTableCell>{row.order}</StyledTableCell>
  272.  
  273. <ActionsTableCell>
  274. <EditIcon
  275. className="w-16 text-black cursor-pointer"
  276. onClick={() => openEditForm(index)}
  277. />
  278. <DeleteIcon
  279. className="md:ml-32 ml-12 text-black w-16 cursor-pointer"
  280. //onClick={() => deleteStageFromProto(index)}
  281. />
  282. </ActionsTableCell>
  283. </TableRow>
  284. ))}
  285. </TableBody>
  286. </Table>
  287. </TableContainer>
  288. </div>
  289. </div>
  290. </div>
  291. {/* {currentForm === 'addNew' && ( */}
  292. {/* <AddNewProto stages={stagesList} protoId={proto.id} setCurrentForm={setCurrentForm} /> */}
  293. {/* )} */}
  294. {/* {currentForm === 'edit' && ( */}
  295. {/* <EditProto */}
  296. {/* stage={form.item[stageEditIndex]} */}
  297. {/* protoId={proto.id} */}
  298. {/* setCurrentForm={setCurrentForm} */}
  299. {/* /> */}
  300. {/* )} */}
  301. </div>
  302. );
  303. }
  304.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement