Advertisement
Guest User

App.js

a guest
Nov 13th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import './App.css';
  3. import { addItem, generateId, findById, toggleTodo, updateTodo } from './components/lib/todoHelper'
  4. //import { TodoList } from './components/todo/TodoList'
  5. import { AddPopup } from './components/todo/AddPopup'
  6. import { EditPopup } from './components/todo/EditPopup'
  7. import { TodoView } from './components/todo/TodoView'
  8.  
  9.  
  10. class App extends React.Component {
  11.   constructor(props){
  12.     super(props);
  13.  
  14.   }
  15.   state = {
  16.     todos: [
  17.       { id: 1, name: 'Task 1', description: '', isComplete: false },
  18.       { id: 2, name: 'Task 2', description: '', isComplete: true },
  19.       { id: 3, name: 'Task 3', description: '', isComplete: true }
  20.     ],
  21.     currentTodo: '',
  22.     currentDsrp: '',
  23.     errorMessage: '',
  24.     showPopup: false,
  25.     showEditPopup: false,
  26.     todoToBeEdited: {},
  27.     todoView: {},
  28.     filterBy:'none'
  29.   }
  30.   handleToggle = (id) => {
  31.     const todo = findById(id, this.state.todos)
  32.     const toggled = toggleTodo(todo)
  33.     const updatedTodos = updateTodo(this.state.todos, toggled)
  34.     this.setState({
  35.       todos: updatedTodos
  36.     })
  37.   }
  38.  
  39.   handleInput = (evt) => {
  40.     this.setState({
  41.       currentTodo: evt.target.value
  42.     })
  43.   }
  44.   handleTextInput = (evt) => {
  45.     this.setState({
  46.       currentDsrp: evt.target.value
  47.     })
  48.   }
  49.   handleSubmin = (evt) => {
  50.     evt.preventDefault();
  51.     if (this.state.currentTodo !== '') {
  52.       const newId = generateId();
  53.       const newItems = { id: newId, name: this.state.currentTodo, description: this.state.currentDsrp, isComplete: 'notdone' }
  54.       const newArray = addItem(this.state.todos, newItems)
  55.       this.setState({
  56.         todos: newArray,
  57.         currentTodo: '',
  58.         errorMessage: '',
  59.         currentDsrp: ''
  60.  
  61.  
  62.       })
  63.     }
  64.  
  65.  
  66.  
  67.   }
  68.   handleError = (evt) => {
  69.     evt.preventDefault();
  70.     if (this.state.currentTodo === '') {
  71.       this.setState({
  72.         errorMessage: 'Insert something!'
  73.       })
  74.     }
  75.   }
  76.   togglePopup = (evt) => {
  77.     evt.preventDefault()
  78.     this.setState({
  79.       showPopup: !this.state.showPopup
  80.     })
  81.   }
  82.   toggleEditPopup = (index) => {
  83.  
  84.     this.setState({
  85.       showEditPopup: !this.state.showEditPopup,
  86.       todoToBeEdited: this.state.todos.find(todo => todo.id === index)
  87.     })
  88.  
  89.   }
  90.   handleDelete = (index, evt) => {
  91.     evt.preventDefault();
  92.     const modifItems = Object.assign([], this.state.todos)
  93.     modifItems.splice(index, 1)
  94.     this.setState({
  95.       todos: modifItems
  96.     })
  97.  
  98.   }
  99.   setNameUpdate = (name, key) => {
  100.     const items = this.state.todos
  101.     items.map(item => {
  102.       if (item.id === key) {
  103.         item.name = name
  104.       }
  105.     })
  106.     this.setState({
  107.       todos: items
  108.     })
  109.  
  110.   }
  111.   setDescriptionUpdate = (description, key) => {
  112.     const items = this.state.todos
  113.     items.map(item => {
  114.       if (item.id === key) {
  115.         item.description = description
  116.       }
  117.     })
  118.     this.setState({
  119.       todos: items
  120.     })
  121.   }
  122.  
  123.   toggleView = (index) => {
  124.     this.setState({
  125.       todoView: this.state.todos.find(todo => todo.id === index)
  126.     })
  127.   }
  128.   handleFilterChange(event) {
  129.     event.preventDefault();
  130.     this.setState({filterBy: event.target.value});
  131.   }
  132.  
  133.   showFilteredTodos(completed) {
  134.     console.log(completed);
  135.     const complete = completed === 'done';
  136.     return (
  137.       <ul className="list">
  138.         {this.state.todos.filter(todo => todo.isComplete === complete).map((todo, index) =>
  139.           <li key={todo.id} onClick={this.toggleView.bind(this, todo.id)}>
  140.             {todo.name}
  141.             <button onClick={this.toggleEditPopup.bind(this, todo.id)}>Edit</button>
  142.             <button onClick={this.handleDelete.bind(this, index)}>Delete</button>
  143.           </li>)}
  144.       </ul>
  145.     );
  146.   }
  147.  
  148.   handleComplete(id) {
  149.     console.log(this.state);
  150.    /* const updatedTodo = this.state.todos.find(todo => todo.id === id).map(todo => {
  151.       return {
  152.         ...todo,
  153.         isComplete: !todo.isComplete
  154.       }
  155.     });*/
  156.     this.setState({
  157.       todos: this.state.todos.map(todo => todo.id === id ? { ...todo, isComplete: !todo.isComplete} : todo)
  158.     });
  159.   }
  160.  
  161.  
  162.   render() {
  163.     return (
  164.       <div className="App">
  165.         <header className="App-header">
  166.           <button onClick={this.togglePopup}>Add To-Do</button>
  167.           <h4>To-Do List</h4>
  168.         </header>
  169.         <div className="container">
  170.           {this.state.showPopup ?
  171.             <AddPopup text='Add To-do Item'
  172.               errorMessage={this.state.errorMessage}
  173.               closePopup={this.togglePopup.bind(this)}
  174.               currentTodo={this.state.currentTodo}
  175.               currentDsrp={this.state.currentDsrp}
  176.               handleSubmin={this.handleSubmin}
  177.               handleInput={this.handleInput}
  178.               handleError={this.handleError}
  179.               handleTextInput={this.handleTextInput}
  180.             /> : null}
  181.  
  182.           <form className="selector">
  183.             <label>
  184.               <select value={this.state.value} onClick={this.handleFilterChange.bind(this)}>
  185.                 <option value="none">Default</option>
  186.                 <option value="done">By Done</option>
  187.                 <option value="notdone">By Not Done</option>
  188.               </select>
  189.             </label>
  190.           </form>
  191.  
  192.  
  193.           {this.state.filterBy === 'none' &&
  194.           <ul className="list">
  195.             {this.state.todos.map((todo, index) =>
  196.               <li key={todo.id} onClick={this.toggleView.bind(this, todo.id)}>
  197.                 {todo.name}
  198.                 <button onClick={this.toggleEditPopup.bind(this, todo.id)}>Edit</button>
  199.                 <button onClick={this.handleDelete.bind(this, index)}>Delete</button>
  200.                 {todo.isComplete === 'done' ? 'done' : 'notdone'}
  201.               </li>)}
  202.           </ul>}
  203.  
  204.           {this.showFilteredTodos(this.state.filterBy)}
  205.  
  206.           {this.state.showEditPopup ?
  207.             <EditPopup
  208.               id={this.state.todoToBeEdited.id}
  209.               name={this.state.todoToBeEdited.name}
  210.               description={this.state.todoToBeEdited.description}
  211.               setNameUpdate={this.setNameUpdate}
  212.               setDescriptionUpdate={this.setDescriptionUpdate}
  213.               errorMessage={this.state.errorMessage}
  214.               closePopup={this.toggleEditPopup.bind(this)}
  215.               handleSubmin={this.handleSubmin}
  216.               handleError={this.handleError}
  217.             />
  218.             : null}
  219.         </div>
  220.         <div className="main">
  221.           <TodoView
  222.             id={this.state.todoView.id}
  223.             name={this.state.todoView.name}
  224.             description={this.state.todoView.description}
  225.             isComplete={this.state.todoView.isComplete}
  226.             handleComplete={this.handleComplete.bind(this, this.state.todoView.id)}
  227.           />
  228.         </div>
  229.       </div>
  230.     );
  231.   }
  232. }
  233.  
  234. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement