Skip to content
Open
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
56 changes: 56 additions & 0 deletions frontend/Todo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { useState, useEffect } from "react";

let todos = [];

export const Todo = () => {
const [inputValue, setInputValue] = useState("");
const [error, setError] = useState("");
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
console.log("Todo was updated:", todos);
document.title = `${todos.length} todos`;
}, []);

const addTodo = () => {
todos.push(inputValue);
setInputValue("");
setIsLoading(true);

setTimeout(() => {
setIsLoading(false);
}, 1000);
};

return (
<div style={{ padding: "20px", border: "1px solid black" }}>
<h1>Todo List</h1>

{isLoading ? (
<p>Loading...</p>
) : error ? (
<p style={{ color: "red" }}>{error}</p>
) : null}

<input
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyPress={(e) => e.key === "Enter" && addTodo()}
/>
<button onClick={addTodo}>Add Todo</button>

<div>
{todos.map((todo, index) => (
<div
key={index}
onClick={() => {
todos.splice(index, 1);
}}
>
{todo}
</div>
))}
</div>
</div>
);
};