Skip to content

Commit 12402b6

Browse files
committed
updated
1 parent be1fa95 commit 12402b6

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/components/TodoForm.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default function TodoForm() {
77
const [newTodo, setNewTodo] = useState("");
88

99
const handleCreateTodo = async () => {
10+
if (newTodo.length === 0) return alert("Todo input must not be empty");
1011
try {
1112
setLoading(true);
1213
const todo = { title: newTodo };

src/components/TodoItem.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ const TodoItem: React.FC<TodoItemProps> = ({ todo }) => {
3939
</span>
4040
<button
4141
disabled={loading}
42-
className="px-2 py-1 bg-red-500 text-white rounded"
42+
className={`px-2 py-1 text-white rounded ${
43+
loading ? "bg-gray-400" : "bg-red-500"
44+
}`}
4345
onClick={handleDelete}
4446
>
4547
Delete

src/store.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ type TodoStore = {
1818
deleteTodo: (id: number) => void;
1919
};
2020

21+
const URL = process.env.NEXT_PUBLIC_VERCEL_URL
22+
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}/api`
23+
: "http://localhost:3000/api";
24+
2125
export const useStore = create<TodoStore>((set) => ({
2226
todos: [],
2327
fetchTodos: async () => {
2428
try {
25-
const response = await fetch("/api/todos");
29+
const response = await fetch(`${URL}/todos`);
2630
const todos = await response.json();
2731
set({ todos });
2832
} catch (error) {
@@ -31,7 +35,7 @@ export const useStore = create<TodoStore>((set) => ({
3135
},
3236
addTodo: async (todo) => {
3337
try {
34-
const response = await fetch("/api/todos", {
38+
const response = await fetch(`${URL}/todos`, {
3539
method: "POST",
3640
headers: {
3741
"Content-Type": "application/json",
@@ -46,7 +50,7 @@ export const useStore = create<TodoStore>((set) => ({
4650
},
4751
updateTodo: async (updatedTodo) => {
4852
try {
49-
const response = await fetch(`/api/todos/${updatedTodo.id}`, {
53+
const response = await fetch(`${URL}/todos/${updatedTodo.id}`, {
5054
method: "PATCH",
5155
headers: {
5256
"Content-Type": "application/json",
@@ -65,7 +69,7 @@ export const useStore = create<TodoStore>((set) => ({
6569
},
6670
deleteTodo: async (id) => {
6771
try {
68-
await fetch(`/api/todos/${id}`, {
72+
await fetch(`${URL}/todos/${id}`, {
6973
method: "DELETE",
7074
});
7175
set((state) => ({

0 commit comments

Comments
 (0)