Advertisement
genkid2020

Untitled

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