Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (60 sloc) 1.85 KB
import React, { useEffect, useState } from 'react';
function App() {
const [newTodo, setNewTodo] = useState("");
const [todos, setTodos] = useState([]);
async function getTodos() {
const result = await fetch("/api/todos");
const todos = await result.json();
setTodos(todos);
}
async function createTodo(e) {
e.preventDefault();
await fetch('/api/todos', {
method: "POST",
body: JSON.stringify({ name: newTodo })
});
setNewTodo("");
await getTodos();
}
async function updateCompleted(todo, isComplete) {
await fetch(`/api/todos/${todo.id}`, {
method: "POST",
body: JSON.stringify({ ...todo, isComplete: isComplete })
});
}
async function deleteTodo(id) {
await fetch(`/api/todos/${id}`, {
method: "DELETE"
});
await getTodos();
}
useEffect(() => {
getTodos();
}, []);
return (
<section className="todoapp">
<header className="header">
<h1>todos</h1>
<form onSubmit={createTodo}>
<input className="new-todo" placeholder="What needs to be done?" value={newTodo} onChange={(e) => setNewTodo(e.target.value)} />
</form>
</header>
<section className="main" style={{ display: "block" }}>
<ul className="todo-list">
{todos.map(todo => {
return (
<li className={todo.IsComplete ? "completed" : ""} key={todo.id}>
<div className="view">
<input className="toggle" type="checkbox" defaultChecked={todo.isComplete} onChange={(e) => updateCompleted(todo, e.target.checked)} />
<label>{todo.name}</label>
<button className="destroy" onClick={() => deleteTodo(todo.id)}></button>
</div>
</li>
);
})}
</ul>
</section>
</section >
);
}
export default App;
You can’t perform that action at this time.