genkid2020

Untitled

Aug 20th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 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. items: []
  137. });
  138.  
  139. const classes = useStyles(props);
  140. const dispatch = useDispatch();
  141. const routeParams = useParams();
  142.  
  143. const handbook = useSelector(({ settings }) => settings.handbooks.handbook);
  144. // const stagesList = useSelector(({ settings }) => settings.handbooks.stageProtosList);
  145. const isGetHandbookError = useSelector(({ settings }) => settings.handbooks.isGetHandbookError);
  146. const isHandbookPending = useSelector(({ settings }) => settings.handbooks.isHandbookPending);
  147. // const isStagePending = useSelector(({ settings }) => settings.projectProto.isStagePending);
  148. const isDataPending = isHandbookPending;
  149. console.log(handbook);
  150. useEffect(() => {
  151. const { handbooksKey } = routeParams;
  152. dispatch(Actions.getProtoById(handbooksKey));
  153. dispatch(Actions.getStageProtos());
  154. }, []);
  155.  
  156. useEffect(() => {
  157. setForm(handbook);
  158. }, [handbook]);
  159.  
  160. useEffect(() => {
  161. if (isGetHandbookError) {
  162. history.push('/error-404');
  163. }
  164. }, [isGetHandbookError]);
  165.  
  166. const saveData = () => {
  167. if (form.name !== handbook.name || form.key !== handbook.key) {
  168. const data = {
  169. name: form.name,
  170. key: form.key
  171. };
  172. // dispatch(Actions.updateProjectProto(proto.id, data));
  173. }
  174. setIsEditName(false);
  175. };
  176.  
  177. const openEditForm = index => {
  178. setStageEditIndex(index);
  179. setCurrentForm('edit');
  180. };
  181.  
  182. // const deleteStageFromProto = index => {
  183. // const stageId = form.items[index].id;
  184. // dispatch(Actions.deleteStageOfProject(proto.id, stageId));
  185. // };
  186.  
  187. return isDataPending ? (
  188. <FuseLoading />
  189. ) : (
  190. <div className="md:p-32 p-12 flex flex-wrap">
  191. <div className={classes.mainBlock}>
  192. <div className={classes.cardWrapper}>
  193. <div className={classes.cardHeader}>
  194. <h4>Информация о справочнике</h4>
  195. {!isEditName || !isEditKey ? (
  196. <span
  197. className={classes.cardHeaderEditButton}
  198. onClick={() => setIsEditName(true) || setIsEditKey(true)}
  199. role="button"
  200. >
  201. <Icon>edit</Icon>
  202. Редактировать
  203. </span>
  204. ) : (
  205. <div className="flex justify-between">
  206. <span className={classes.cardHeaderSaveButton} onClick={saveData}>
  207. <Icon>check</Icon>
  208. Сохранить
  209. </span>
  210. </div>
  211. )}
  212. </div>
  213. <div className={classes.cardContent}>
  214. <TextField
  215. className="mb-20"
  216. label="Наименование справочника"
  217. type="text"
  218. name="name"
  219. value={form.name}
  220. onChange={handleChange}
  221. variant="outlined"
  222. fullWidth
  223. required
  224. disabled={!isEditName}
  225. />
  226. <TextField
  227. className="mb-20"
  228. label="Код справочника"
  229. type="text"
  230. name="key"
  231. value={form.key}
  232. onChange={handleChange}
  233. variant="outlined"
  234. fullWidth
  235. required
  236. disabled={!isEditKey}
  237. />
  238. </div>
  239. </div>
  240. <div className={classes.cardWrapper}>
  241. <div className={classes.cardHeader}>
  242. <h4>Этапы</h4>
  243. {/*<span className={classes.cardHeaderAddButton} onClick={openAddForm} role="button">*/}
  244. {/* <Icon>add</Icon>*/}
  245. {/* Добавить этап*/}
  246. {/*</span>*/}
  247. </div>
  248. <div className={classes.cardTable}>
  249. <TableContainer>
  250. <Table color="secondary" aria-label="simple table">
  251. <TableHead>
  252. <TableRow>
  253. <StyledTableCell>Наименование</StyledTableCell>
  254. <StyledTableCell>Порядок</StyledTableCell>
  255. <StyledTableCell>Действия</StyledTableCell>
  256. </TableRow>
  257. </TableHead>
  258. <TableBody>
  259. {form.items &&
  260. form.items.map((row, index) => (
  261. <TableRow key={row.id}>
  262. <StyledTableCell component="th" scope="row">
  263. {row.name}
  264. </StyledTableCell>
  265. <StyledTableCell>{row.order}</StyledTableCell>
  266.  
  267. <ActionsTableCell>
  268. <EditIcon
  269. className="w-16 text-black cursor-pointer"
  270. onClick={() => openEditForm(index)}
  271. />
  272. <DeleteIcon
  273. className="md:ml-32 ml-12 text-black w-16 cursor-pointer"
  274. onClick={() => deleteStageFromProto(index)}
  275. />
  276. </ActionsTableCell>
  277. </TableRow>
  278. ))}
  279. </TableBody>
  280. </Table>
  281. </TableContainer>
  282. </div>
  283. </div>
  284. </div>
  285. {/* {currentForm === 'addNew' && ( */}
  286. {/* <AddNewProto stages={stagesList} protoId={proto.id} setCurrentForm={setCurrentForm} /> */}
  287. {/* )} */}
  288. {/* {currentForm === 'edit' && ( */}
  289. {/* <EditProto */}
  290. {/* stage={form.items[stageEditIndex]} */}
  291. {/* protoId={proto.id} */}
  292. {/* setCurrentForm={setCurrentForm} */}
  293. {/* /> */}
  294. {/* )} */}
  295. </div>
  296. );
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment