From 70ee0b35709933d7a1db4424026646db3c011630 Mon Sep 17 00:00:00 2001 From: data Date: Thu, 20 Jun 2019 15:58:56 -0400 Subject: [PATCH] tasks as objects, separate completed tasks in view - tasks are list of objects - add button to complete task - show completed tasks separatedly - move filter from navbar to tasks component --- src/App.js | 32 ++++++++++++++++++++------------ src/components/navbar.js | 4 ---- src/components/tasks.js | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/App.js b/src/App.js index 45d818c..3e11063 100644 --- a/src/App.js +++ b/src/App.js @@ -1,10 +1,11 @@ import React from 'react' import { About, Tasks, Navbar } from './components' -const defaultState = { loading: true, tasks: [], search: '', newTask: '' } +const defaultState = { user: {}, loading: true, tasks: [], search: '', newTask: '' } // hard-coded for now, but can be passed anything and set on state -const data = { user: { name: 'Scott', role: 'manager' }, component: 'tasks', tasks: ['arf', 'bruf', 'crap'] } +const data = { user: { name: 'Scott', role: 'manager' }, component: 'tasks', + tasks: [{ id: 0, desc: 'arf'}, { id:1, desc: 'bruf' }, { id: 2, desc: 'crap' }] } class App extends React.Component { constructor() { @@ -15,6 +16,7 @@ class App extends React.Component { this.handleSubmit = this.handleSubmit.bind(this) this.addTask = this.addTask.bind(this) this.deleteTask = this.deleteTask.bind(this) + this.completeTask = this.completeTask.bind(this) } fetchData() { @@ -44,12 +46,18 @@ class App extends React.Component { addTask(e) { e.preventDefault() - this.setState( { tasks: this.state.tasks.concat(this.state.newTask), newTask: '' }) + this.setState( { tasks: this.state.tasks.concat( { id: this.state.tasks.length, desc: this.state.newTask } ), newTask: '' }) } - deleteTask(task) { - this.setState( { tasks: this.state.tasks.filter(t => t !== task) }) + deleteTask(id) { + this.setState( { tasks: this.state.tasks.filter(t => t.id !== id) }) + } + completeTask(id) { + const otherTasks = this.state.tasks.filter(t=> t.id !== id) + const completedTask = this.state.tasks.find(t => t.id === id) + completedTask.completed = ! completedTask.completed + console.log(completedTask) + this.setState( { tasks: otherTasks.concat(completedTask) }) } - renderLoading() { return
Loading...
@@ -60,22 +68,22 @@ class App extends React.Component { renderAbout() { return } - renderTasks(filtered) { + renderTasks(filtered, completed) { // {...this.state} passes everything on state to the component - return + return } render() { - const filtered = this.state.tasks.filter(task => this.state.search === task.slice(0, this.state.search.length)) + const completed = this.state.tasks.filter(task => task.completed === true) + const filtered = this.state.tasks.filter(task => task.completed !== true && this.state.search === task.desc.slice(0, this.state.search.length)) const renderComponent = () => this.state.loading ? this.renderLoading() - : this.state.component === 'tasks' ? this.renderTasks(filtered) + : this.state.component === 'tasks' ? this.renderTasks(filtered, completed) : this.state.component === 'about' ? this.renderAbout() : this.renderError() - return (
- + { renderComponent() }
) } diff --git a/src/components/navbar.js b/src/components/navbar.js index 2b4a3b1..a54e672 100644 --- a/src/components/navbar.js +++ b/src/components/navbar.js @@ -22,10 +22,6 @@ return ( hi -
- props.handleChange(e)} /> - -
) diff --git a/src/components/tasks.js b/src/components/tasks.js index 9770b7b..bc78a9e 100644 --- a/src/components/tasks.js +++ b/src/components/tasks.js @@ -3,17 +3,41 @@ import React from 'react' function Tasks(props) { // props are passed from App as {...this.state} const filtered = props.filtered || [] + const completed = props.completed || [] return (

{`Hello, ${props.user.name}!`}

-

Your tasks:

-
    {filtered.map(task => -
  • - {task}
  • )}
-
- - -
+ +

Your open tasks

+
    +
  • +
    + props.handleChange(e)} /> + +
    +
    Add task: + +
    +
  • + {filtered.map(task => +
  • + + {task.desc} + +
  • ) + } +
+ + { completed.length === 0 ? null :

Completed

} +
    + {completed.map(task => +
  • + + {task.desc} + +
  • ) + } +
) }