SanderCokart

TodoTable.js

Jun 18th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //REACT
  2. import React, {Fragment, useContext, useState} from 'react';
  3. //CONTEXT
  4. import {TodoContext} from '../contexts/TodoContext';
  5. //MUI COMPONENTS
  6. import {
  7.     makeStyles,
  8.     Typography,
  9.     IconButton,
  10.     Table,
  11.     TableBody,
  12.     TableCell,
  13.     TableHead,
  14.     TableRow,
  15.     TextField,
  16. } from '@material-ui/core';
  17. //MUI ICONS
  18. import {
  19.     Add as AddIcon,
  20.     Close as CloseIcon,
  21.     Delete as DeleteIcon,
  22.     Done as DoneIcon,
  23.     Edit as EditIcon,
  24. } from '@material-ui/icons';
  25. //CUSTOM COMPONENTS
  26. import DeleteDialog from './DeleteDialog';
  27.  
  28. const useStyles = makeStyles(theme => ({
  29.     thead: {
  30.         backgroundColor: theme.palette.primary.main,
  31.     },
  32. }));
  33.  
  34.  
  35. function TodoTable() {
  36.     const context = useContext(TodoContext);
  37.     const [addTodoName, setAddTodoName] = useState('');
  38.     const [addTodoDescription, setAddTodoDescription] = useState('');
  39.     const [editIsShown, setEditIsShown] = useState(false);
  40.     const [editTodoName, setEditTodoName] = useState('');
  41.     const [editTodoDescription, setEditTodoDescription] = useState('');
  42.     const [deleteConfirmationIsShown, setDeleteConfirmationIsShown] = useState(false);
  43.     const [todoToBeDeleted, setTodoToBeDeleted] = useState(null);
  44.  
  45.     const classes = useStyles();
  46.  
  47.     const onCreateSubmit = (event) => {
  48.         event.preventDefault();
  49.         context.createTodo(event, {task: addTodoName, description: addTodoDescription});
  50.         setAddTodoName('');
  51.         setAddTodoDescription('');
  52.     };
  53.  
  54.     const onEditSubmit = (todoId, event) => {
  55.         event.preventDefault();
  56.         context.updateTodo({id: todoId, task: editTodoName, description: editTodoDescription});
  57.         setEditIsShown(false);
  58.     };
  59.  
  60.     return (
  61.         <Fragment>
  62.  
  63.             <Table size="small">
  64.                 {/*HEAD*/}
  65.                 <TableHead>
  66.                     {/*ADD*/}
  67.                     <TableRow>
  68.                         <TableCell>
  69.                             <form onSubmit={onCreateSubmit}>
  70.                                 <TextField variant="outlined"
  71.                                            type="text"
  72.                                            size="small"
  73.                                            value={addTodoName}
  74.                                            onChange={(event) => {
  75.                                                setAddTodoName(event.target.value);
  76.                                            }}
  77.                                            label="Task"
  78.                                            fullWidth={true}/>
  79.                             </form>
  80.                         </TableCell>
  81.  
  82.                         <TableCell>
  83.                             <form>
  84.                                 <TextField variant="outlined"
  85.                                            size="small"
  86.                                            type="text"
  87.                                            value={addTodoDescription}
  88.                                            onChange={(event) => {
  89.                                                setAddTodoDescription(event.target.value);
  90.                                            }}
  91.                                            label="Description"
  92.                                            fullWidth={true}
  93.                                            multiline={true}/>
  94.                             </form>
  95.                         </TableCell>
  96.  
  97.                         <TableCell width={130} align="right">
  98.                             <IconButton color="primary" onClick={onCreateSubmit}>
  99.                                 <AddIcon/>
  100.                             </IconButton>
  101.                         </TableCell>
  102.                     </TableRow>
  103.                     <TableRow className={classes.thead}>
  104.                         <TableCell width={200}>Task</TableCell>
  105.                         <TableCell>Description</TableCell>
  106.                         <TableCell align="right">Actions</TableCell>
  107.                     </TableRow>
  108.                 </TableHead>
  109.  
  110.  
  111.                 {/*BODY*/}
  112.                 <TableBody>
  113.                     {/*DATA*/}
  114.                     {context.todos.slice().reverse().map((todo, index) => (
  115.                         <TableRow key={'todo ' + index}>
  116.  
  117.                             {/*NAME*/}
  118.                             <TableCell>
  119.                                 {editIsShown === todo.id ?
  120.                                  <form onSubmit={onEditSubmit.bind(this, todo.id)}>
  121.                                      <TextField
  122.                                          type="text"
  123.                                          fullWidth={true}
  124.                                          autoFocus={true}
  125.                                          value={editTodoName}
  126.                                          onChange={(event) => {
  127.                                              setEditTodoName(event.target.value);
  128.                                          }}
  129.                                      />
  130.                                  </form>
  131.                                                          :
  132.                                  <Typography>{todo.task}</Typography>
  133.                                 }
  134.                             </TableCell>
  135.  
  136.                             {/*DESCRIPTION*/}
  137.                             <TableCell>
  138.                                 {editIsShown === todo.id ?
  139.                                  <TextField
  140.                                      type="text"
  141.                                      fullWidth={true}
  142.                                      value={editTodoDescription}
  143.                                      onChange={(event) => setEditTodoDescription(event.target.value)}
  144.                                      multiline={true}
  145.                                  />
  146.                                                          :
  147.                                  <Typography style={{whiteSpace: 'pre-wrap'}}>{todo.description}</Typography>
  148.                                 }
  149.                             </TableCell>
  150.  
  151.                             <TableCell align="right">
  152.  
  153.                                 {editIsShown === todo.id ?
  154.                                  <Fragment>
  155.                                      <IconButton onClick={onEditSubmit.bind(this, todo.id)}>
  156.                                          <DoneIcon/>
  157.                                      </IconButton>
  158.                                      <IconButton onClick={() => setEditIsShown(false)}>
  159.                                          <CloseIcon/>
  160.                                      </IconButton>
  161.                                  </Fragment>
  162.                                                          :
  163.                                  <Fragment>
  164.                                      <IconButton color="primary" onClick={() => {
  165.                                          setEditIsShown(todo.id);
  166.                                          setEditTodoName(todo.task);
  167.                                          setEditTodoDescription(todo.description);
  168.                                      }}>
  169.                                          <EditIcon/>
  170.                                      </IconButton>
  171.  
  172.                                      <IconButton color="secondary" onClick={() => {
  173.                                          setDeleteConfirmationIsShown(true);
  174.                                          setTodoToBeDeleted(todo);
  175.                                      }}>
  176.                                          <DeleteIcon/>
  177.                                      </IconButton>
  178.                                  </Fragment>
  179.                                 }
  180.  
  181.  
  182.                             </TableCell>
  183.                         </TableRow>
  184.                     ))}
  185.                 </TableBody>
  186.             </Table>
  187.  
  188.             {deleteConfirmationIsShown && (
  189.                 <DeleteDialog todo={todoToBeDeleted}
  190.                               open={deleteConfirmationIsShown}
  191.                               setDeleteConfirmationIsShown={setDeleteConfirmationIsShown}
  192.                 />
  193.             )}
  194.  
  195.         </Fragment>
  196.  
  197.     );
  198. }
  199.  
  200. export default TodoTable;
Add Comment
Please, Sign In to add comment