-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathTodoList.tsx
More file actions
71 lines (69 loc) · 2.39 KB
/
Copy pathTodoList.tsx
File metadata and controls
71 lines (69 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { Data } from "@batijs/shared-todo/pages/todo/+data";
import { onNewTodo } from "@batijs/telefunc/pages/todo/TodoList.telefunc";
import { trpc } from "@batijs/trpc/trpc/client";
import { client } from "@batijs/ts-rest/ts-rest/client";
import { useState } from "react";
import { useData } from "vike-react/useData";
export function TodoList() {
const { todoItemsInitial } = useData<Data>();
const [todoItems, setTodoItems] = useState<{ text: string }[]>(todoItemsInitial);
const [newTodo, setNewTodo] = useState("");
return (
<>
<ul>
{todoItems.map((todoItem, index) => (
// biome-ignore lint: example
<li key={index}>{todoItem.text}</li>
))}
</ul>
<div>
<form
onSubmit={async (ev) => {
ev.preventDefault();
const text = newTodo;
setTodoItems((prev) => [...prev, { text }]);
setNewTodo("");
if (BATI.hasServer) {
if (BATI.has("telefunc")) {
await onNewTodo({ text });
} else if (BATI.has("trpc")) {
await trpc.onNewTodo.mutate(text);
} else if (BATI.has("ts-rest")) {
await client.createTodo({ body: { text } });
} else {
const response = await fetch("/api/todo/create", {
method: "POST",
body: JSON.stringify({ text }),
headers: {
"Content-Type": "application/json",
},
});
await response.blob();
}
}
}}
>
<input
type="text"
aria-label="New to-do"
onChange={(ev) => setNewTodo(ev.target.value)}
value={newTodo}
//# BATI.has("tailwindcss")
className={
"bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 w-full sm:w-auto p-2 mr-1 mb-1"
}
/>
<button
type="submit"
//# BATI.has("tailwindcss")
className={
"text-white bg-blue-700 hover:bg-blue-800 focus:ring-2 focus:outline-hidden focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto p-2"
}
>
Add to-do
</button>
</form>
</div>
</>
);
}