Skip to content

Finished #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion frontend/components/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,73 @@
/* eslint-disable no-unused-vars */
import React from 'react'
import TodoList from "./TodoList"
import axios from "axios"

const URL = 'http://localhost:9000/api/todos'

export default class App extends React.Component {
state = {
todos: [],
error: "",
todonameinput: "",
}
fetchalltodos = () => {
axios.get(URL)
.then(res => {
this.setState({...this.state, todos: res.data.data})
})
.catch(err => {this.setState({...this.state, error: err.response.data.message})
})
};
componentDidMount(){
this.fetchalltodos()
}
oninputchange = evt => {
const {value} = evt.target
this.setState({...this.state, todoname: value})
}
postnewtodo = () => {
axios.post(URL, {name: this.state.todonameinput})
.then(res => {
this.fetchalltodos();
this.setState({...this.state, todonameinput: "" })
})
.catch(err => {this.setState({...this.state, error: err.response.data.message})
})
}
onsubmit = (e) => {
e.preventDefault();
this.postnewtodo()
}
clearcompleted = (e) => {

}
togglecompleted = id => {
axios.patch(`${URL}/${id}`)
.then(res => {
this.setState({...this.state, todos: [this.state.todos.map(td => {
if (td.id !== id) return td
else {
return res.dtat.data
}
})]})
})
.catch(err => {this.setState({...this.state, error: err.response.data.message})})
}
onclickhandler = id => e => {
this.togglecompleted(id)
}
render() {
return null
return (
<>
<h2> error: {this.state.error}</h2>
<TodoList todos ={this.state.todos} onclickhandler={this.onclickhandler} />
<form onSubmit={this.onsubmit}>
<input value={this.state.todonameinput} onChange={this.oninputchange} type="text" />
<button>ADD TODO</button>
<button onClick={this.clearcompleted}>CLEAR SPACE COMPLETED</button>
</form>
</>
)
}
}
35 changes: 33 additions & 2 deletions frontend/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
import React from 'react'

import axios from "axios"
export default class Form extends React.Component {
state = {
newTask: "",
};
completedTask = (evt) => {
evt.preventDefault();
this.props.handleCompleted();
};
handleChange = (evt) => {
evt.preventDefault();
this.setState({ ...this.state, newTask: evt.target.value });
};
handlePost = (evt) => {
evt.preventDefault();
this.props.ToDo(this.state.newTask);
this.setState({ ...this.state, newTask: "" });
};
render() {
return null
return (
<form onSubmit={this.handlePost}>
<div>
<input
type={text}
placeholder="Add to list"
onChange={this.handleChange}
value={this.state.newTask}
/>
<button onClick={this.handlePost}>Add</button>
</div>
<button onAuxClick={this.completedTask}>Remove Completed</button>
</form>
);
}
}
11 changes: 10 additions & 1 deletion frontend/components/Todo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React from 'react'

export default class Todo extends React.Component {
toggle = (evt) => {
evt.preventDefault();
this.props.toggleCompleted(this.props.todo.id);
};
render() {
return null
return (
<li onClick={this.toggle}>
{this.props.todo.name}{" "}
{this.props.todo.completed ? <span>&#10003;</span> : <span></span>}
</li>
);
}
}
10 changes: 9 additions & 1 deletion frontend/components/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import React from 'react'

export default class TodoList extends React.Component {
render() {
return null
return (
<div>
<h1>TO DO</h1>
{
this.props.todos.map((td) => { return (<div key={td.id} onClick={this.props.onclickhandler(td.id)} > {td.name} {td.completed
? " completed" : ""} </div>) })
}
</div>
)
}
}