Advertisement
SanderCokart

TodoTable.js

Jan 23rd, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import IconButton from '@material-ui/core/IconButton';
  2. import Table from '@material-ui/core/Table';
  3. import TableBody from '@material-ui/core/TableBody';
  4. import TableCell from '@material-ui/core/TableCell';
  5. import TableHead from '@material-ui/core/TableHead';
  6. import TableRow from '@material-ui/core/TableRow';
  7. import TextField from '@material-ui/core/TextField';
  8. import EditIcon from '@material-ui/icons/Edit';
  9. import AddIcon from '@material-ui/icons/Add';
  10. import React, {Fragment, useContext, useState} from 'react';
  11. import {TodoContext} from '../contexts/TodoContext';
  12. import DoneIcon from '@material-ui/icons/Done';
  13. import CloseIcon from '@material-ui/icons/Close';
  14. import DeleteIcon from '@material-ui/icons/Delete';
  15. import DeleteDialog from './DeleteDialog';
  16.  
  17.  
  18. function TodoTable() {
  19.     const context = useContext(TodoContext);
  20.     const [addTodo, setAddTodo] = useState('');
  21.     const [editIsShown, setEditIsShown] = useState(false);
  22.     const [editTodo, setEditTodo] = useState('');
  23.     const [deleteConfirmationIsShown, setDeleteConfirmationIsShown] = useState(false);
  24.     const [todoToBeDeleted, setTodoToBeDeleted] = useState(null);
  25.  
  26.     const onCreateSubmit = (event) => {
  27.         event.preventDefault();
  28.         context.createTodo(event, {name: addTodo});
  29.         setAddTodo('');
  30.     };
  31.  
  32.     const onEditSubmit = (todoId, event) => {
  33.         event.preventDefault();
  34.         context.updateTodo({id: todoId, name: editTodo});
  35.         setEditIsShown(false);
  36.     };
  37.  
  38.     return (
  39.         <Fragment>
  40.  
  41.  
  42.             <Table>
  43.                 <TableHead>
  44.                     <TableRow>
  45.                         <TableCell>Task</TableCell>
  46.                         <TableCell align="right">Actions</TableCell>
  47.                     </TableRow>
  48.                 </TableHead>
  49.                 <TableBody>
  50.                     <TableRow>
  51.                         <TableCell>
  52.                             <form onSubmit={onCreateSubmit}>
  53.                                 <TextField type="text" value={addTodo} onChange={(event) => {
  54.                                     setAddTodo(event.target.value);
  55.                                 }} label="New Task" fullWidth={true}/>
  56.                             </form>
  57.                         </TableCell>
  58.                         <TableCell align="right">
  59.                             <IconButton onClick={onCreateSubmit}>
  60.                                 <AddIcon/>
  61.                             </IconButton>
  62.                         </TableCell>
  63.                     </TableRow>
  64.                     {context.todos.slice().reverse().map((todo, index) => (
  65.                         <TableRow key={'todo ' + index}>
  66.                             <TableCell>
  67.  
  68.                                 {editIsShown === todo.id ?
  69.                                  <form onSubmit={onEditSubmit.bind(this, todo.id)}>
  70.  
  71.                                      <TextField
  72.                                          type="text"
  73.                                          fullWidth={true}
  74.                                          autoFocus={true}
  75.                                          value={editTodo}
  76.                                          onChange={(event) => {
  77.                                              setEditTodo(event.target.value);
  78.                                          }}
  79.                                          InputProps={{
  80.                                              endAdornment: <Fragment>
  81.                                                  <IconButton onClick={() => {
  82.                                                      setEditIsShown(false);
  83.                                                  }}><CloseIcon/></IconButton>
  84.                                                  <IconButton type="submit"><DoneIcon/></IconButton>
  85.                                              </Fragment>,
  86.                                          }}
  87.                                      />
  88.                                  </form>
  89.                                                          :
  90.                                  todo.name
  91.                                 }
  92.  
  93.  
  94.                             </TableCell>
  95.                             <TableCell align="right">
  96.  
  97.                                 <IconButton onClick={() => {
  98.                                     setEditIsShown(todo.id);
  99.                                     setEditTodo(todo.name);
  100.                                 }}>
  101.                                     <EditIcon/>
  102.                                 </IconButton>
  103.  
  104.                                 <IconButton onClick={() => {
  105.                                     setDeleteConfirmationIsShown(true);
  106.                                     setTodoToBeDeleted(todo);
  107.                                 }}>
  108.                                     <DeleteIcon/>
  109.                                 </IconButton>
  110.  
  111.                             </TableCell>
  112.                         </TableRow>
  113.                     ))}
  114.                 </TableBody>
  115.             </Table>
  116.  
  117.             {deleteConfirmationIsShown && (
  118.                 <DeleteDialog todo={todoToBeDeleted}
  119.                               open={deleteConfirmationIsShown}
  120.                               setDeleteConfirmationIsShown={setDeleteConfirmationIsShown}
  121.                 />
  122.             )}
  123.  
  124.         </Fragment>
  125.  
  126.     );
  127. }
  128.  
  129. export default TodoTable;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement