Advertisement
SanderCokart

TodoTable.js

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