From 58af8f2ab7775c32aef0f8af854f472bd4e1826f Mon Sep 17 00:00:00 2001 From: Kfir Date: Sun, 12 Jan 2020 23:23:43 +0200 Subject: [PATCH] Optimaizing the Todolist Data binding --- .gitignore | 2 +- src/App.js | 7 +++- src/components/AddtodoForm.js | 23 +++++++++++ src/components/Li.js | 29 +++++--------- src/components/Main.js | 17 ++++++++ src/components/Navbar.js | 12 +++--- src/components/NavbarLi.js | 7 +--- src/components/TodoList.js | 73 ++++++++++++++++++++++++++++------- 8 files changed, 121 insertions(+), 49 deletions(-) create mode 100644 src/components/AddtodoForm.js create mode 100644 src/components/Main.js diff --git a/.gitignore b/.gitignore index 4b90444..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -./node_modules +node_modules diff --git a/src/App.js b/src/App.js index 90d1bb1..0edcd3f 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,6 @@ import React, { Component } from 'react'; -import Todolist from './components/TodoList'; import Navbar from './components/Navbar'; +import Main from './components/Main'; import './App.css'; class App extends Component { @@ -8,7 +8,10 @@ class App extends Component { return (
- +
+
+
+
); } diff --git a/src/components/AddtodoForm.js b/src/components/AddtodoForm.js new file mode 100644 index 0000000..208a028 --- /dev/null +++ b/src/components/AddtodoForm.js @@ -0,0 +1,23 @@ +import React from 'react' ; +import 'bootstrap/dist/css/bootstrap.min.css'; + +class AddTodoForm extends React.Component { + + render(){ + return( +
+
+ + + +
+
+ ) + } + + + + + } + + export default AddTodoForm diff --git a/src/components/Li.js b/src/components/Li.js index cf8aadc..d36ea09 100644 --- a/src/components/Li.js +++ b/src/components/Li.js @@ -3,39 +3,28 @@ import 'bootstrap/dist/css/bootstrap.css'; import './Li.css'; class Li extends React.Component { - constructor(props){ - super(props) - console.log(this.props); - this.markedFlag = false; - this.state = { - cssClass: 'unMarked' - } - } + setMark = () => ( + this.props.isDone ? 'marked' : 'unMarked' + ) render(){ + const {changeMark, id, isDone, at, todo, close} = this.props return (
-
  • +
  • + close
    ) } - mark = () => { - if (this.markedFlag) { - this.markedFlag = false; - this.setState({cssClass:'unmarked'}) - }else{ - this.markedFlag = true; - this.setState({cssClass:'marked'}) - } - } + } diff --git a/src/components/Main.js b/src/components/Main.js new file mode 100644 index 0000000..23c32f6 --- /dev/null +++ b/src/components/Main.js @@ -0,0 +1,17 @@ +import React from 'react'; +import TodoList from './TodoList' ; +import 'bootstrap/dist/css/bootstrap.min.css'; + + +class Main extends React.Component { + + render(){ + return( +
    + +
    + ) + } + } + + export default Main diff --git a/src/components/Navbar.js b/src/components/Navbar.js index 8767165..3749271 100644 --- a/src/components/Navbar.js +++ b/src/components/Navbar.js @@ -7,10 +7,10 @@ import 'bootstrap/dist/css/bootstrap.min.css'; super(props) this.state = { navbarLinks:[ - {navTitle: 'Home' , href: 'home'}, - {navTitle: 'Blog' , href: 'blog'}, - {navTitle: 'Contact Us', navTitle: 'contact'}, - {navTitle: 'About', navTitle: 'about'} + {navTitle: 'Home' , href: 'home',id:1}, + {navTitle: 'Blog' , href: 'blog', id:2}, + {navTitle: 'Contact Us', navTitle: 'contact', id:3}, + {navTitle: 'About', navTitle: 'about',id:60} ] } } @@ -27,8 +27,8 @@ import 'bootstrap/dist/css/bootstrap.min.css'; { this.state.navbarLinks.map((element, index)=>( )) diff --git a/src/components/NavbarLi.js b/src/components/NavbarLi.js index b63f3a0..0d20387 100644 --- a/src/components/NavbarLi.js +++ b/src/components/NavbarLi.js @@ -3,12 +3,7 @@ import 'bootstrap/dist/css/bootstrap.min.css'; class NavbarLi extends React.Component { - constructor(props) { - super(props) - this.state = { - - } - } + render(){ return(
  • diff --git a/src/components/TodoList.js b/src/components/TodoList.js index e884aba..5d3a6da 100644 --- a/src/components/TodoList.js +++ b/src/components/TodoList.js @@ -1,5 +1,7 @@ import React from 'react'; import Li from './Li.js'; +import AddTodoForm from './AddtodoForm.js'; + import 'bootstrap/dist/css/bootstrap.css'; @@ -10,21 +12,57 @@ class TodoList extends React.Component { super(props) this.state = { todoList : [ - {todo: 'Clean the bathroom' , at:'2020'}, - {todo: 'Buy Milk', at:'2019'}, - {todo: 'Walk the dog' , at:'2018'}, - ] + {id: 123, todo: 'Clean the bathroom' , at:'2020', isDone: false}, + {id: 124, todo: 'Buy Milk', at:'2019', isDone: true}, + {id: 129, todo: 'Walk the dog' , at:'2018', isDone: false} + ], + nextTodoValue: '', + isCheckAll: false, + checkAllToggleName: 'checkAll' } } + addToList = () => { - let todo = document.getElementById('user_input').value; + let todo = this.state.nextTodoValue; if (todo !== '') { - let tmp_state = this.state.todoList; - tmp_state.push({todo:todo, at:'This year'}) + let tmp_state = this.state; + tmp_state.todoList.push({id: 12900,todo:todo, at:'This year' , isDone: false}) + tmp_state.nextTodoValue = ""; this.setState({tmp_state}) } } + + changeNextTodoValue = (event) => { + let newNextTodoValue = event.target.value + this.setState({ + nextTodoValue : newNextTodoValue + }); + } + + changeMark = (id) => { + let tmp_state = this.state.todoList.map((ele) => { + if(id === ele.id) ele.isDone = !ele.isDone; ; + return ele + }); + this.setState({todoList:tmp_state}); + } + closeMark = (id) => { + let tmp_state = this.state.todoList.filter((ele, index) => ( + ele.id !== id + ) + ) + this.setState({todoList:tmp_state}) + } + checkAllToggle = () => { + let tmp_state = this.state.todoList.map((element) => { + element.isDone = !this.state.checkAllToggle + return (element) + } + ) + this.setState({todoList:tmp_state,checkAllToggle:!this.state.checkAllToggle,isCheckAll: !this.state.isCheckAll}) + } + render(){ return (
    @@ -34,17 +72,24 @@ class TodoList extends React.Component {

    Awesome Todo list

    -
    - - -
    +
      { - this.state.todoList.map( (element, index) => ( + this.state.todoList.map( (element) => (