Skip to content

Commit

Permalink
chore: update API
Browse files Browse the repository at this point in the history
change IDB to CloudFlare Worker
  • Loading branch information
tuanductran committed Feb 11, 2025
1 parent 827cdec commit 658a6c8
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 257 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_URL=
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "todo-list-vite",
"version": "1.0.3",
"version": "1.0.4",
"private": true,
"packageManager": "[email protected]",
"description": "A user-friendly and responsive Todo List web application developed using React and Vite",
Expand Down Expand Up @@ -32,7 +32,7 @@
"@hookform/resolvers": "^4.0.0",
"clsx": "^2.1.1",
"dompurify": "^3.2.4",
"idb": "^8.0.2",
"got": "^14.4.6",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-hook-form": "^7.54.2",
Expand Down
71 changes: 0 additions & 71 deletions src/api.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import type { Todo } from "../schema";

interface TodoItemProps {
todo: Todo
isCompleted: boolean
onToggle: () => void
onDelete: () => void
}

function TodoItem({ todo, isCompleted, onToggle, onDelete }: TodoItemProps) {
function TodoItem({ todo, onToggle, onDelete }: TodoItemProps) {
const textClass = clsx(
"flex-1 text-sm truncate transition-colors duration-300",
isCompleted
todo.completed
? "line-through text-gray-600 dark:text-gray-500"
: "text-gray-900 dark:text-white",
);

const toggleButtonClass = clsx(
"ml-4 px-3 py-1 text-xs font-semibold rounded-md transition-colors duration-300",
isCompleted
todo.completed
? "bg-gray-700 hover:bg-gray-600 text-white dark:bg-gray-500 dark:hover:bg-gray-400"
: "bg-green-700 hover:bg-green-600 text-white dark:bg-green-500 dark:hover:bg-green-400",
);
Expand All @@ -28,7 +27,7 @@ function TodoItem({ todo, isCompleted, onToggle, onDelete }: TodoItemProps) {
<div className="flex items-center py-3">
<p className={textClass}>{todo.text}</p>
<button type="button" className={toggleButtonClass} onClick={onToggle}>
{isCompleted ? "Unmark" : "Complete"}
{todo.completed ? "Unmark" : "Complete"}
</button>
<button
type="button"
Expand Down
12 changes: 4 additions & 8 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import TodoItem from "./TodoItem";
function TodoList({
todos,
error,
completedTodos,
handleDeleteClick,
handleToggleClick,
toggleTodo,
removeTodo,
}: TodoListProps) {
const completedTodosSet = new Set(completedTodos);

if (error) {
return (
<div className="py-4 text-center text-red-500 font-medium">
Expand All @@ -27,9 +24,8 @@ function TodoList({
<TodoItem
key={todo.id}
todo={todo}
isCompleted={completedTodosSet.has(todo.id)}
onToggle={() => handleToggleClick(todo.id)}
onDelete={() => handleDeleteClick(todo.id)}
onToggle={() => toggleTodo(todo.id)}
onDelete={() => removeTodo(todo.id)}
/>
))
)
Expand Down
18 changes: 8 additions & 10 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useTodoActions from "../hooks/useTodoActions";
import { useTodoActions } from "../hooks/useTodoActions";

import TodoForm from "./TodoForm";
import TodoList from "./TodoList";
Expand All @@ -7,10 +7,9 @@ export default function TodoPage() {
const {
todos,
error,
completedTodos,
handleAddTodo,
handleDeleteClick,
handleToggleClick,
addNewTodo,
toggleTodo,
removeTodo,
} = useTodoActions();

return (
Expand All @@ -22,14 +21,13 @@ export default function TodoPage() {
Manage your tasks
</h3>
</div>
{!error && <TodoForm onAddTodo={handleAddTodo} />}
{!error && <TodoForm onAddTodo={addNewTodo} />}
</div>
<TodoList
todos={todos || []}
todos={todos}
error={error}
completedTodos={completedTodos}
handleDeleteClick={handleDeleteClick}
handleToggleClick={handleToggleClick}
removeTodo={removeTodo}
toggleTodo={toggleTodo}
/>
</div>
</div>
Expand Down
51 changes: 0 additions & 51 deletions src/context.tsx

This file was deleted.

5 changes: 5 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function fetchAPI<T>(url: string, options?: RequestInit): Promise<T> {
const response = await fetch(url, options);
if (!response.ok) throw new Error(`Failed: ${response.statusText}`);
return response.json();
}
Loading

0 comments on commit 658a6c8

Please sign in to comment.