-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.jsx
163 lines (138 loc) · 3.25 KB
/
App.jsx
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {
useQuery,
useMutation,
useQueryClient,
} from 'react-query'
import axios from 'axios';
import './App.css'
import { Routes, Route, Outlet, Link } from "react-router-dom";
import { useState } from "react";
export default function App() {
return (
<div>
<h1>Loco Todo List</h1>
<Routes >
<Route path="/" element={<Layout />}>
<Route index element={<TodoList />} />
<Route path="*" element={<NoMatch />} />
</Route>
</Routes>
</div>
)
}
function Layout() {
return (
<div>
<div>
<a href="https://loco.rs" target="_blank" rel="noreferrer">
<img src="https://raw.githubusercontent.com/loco-rs/todo-list-example/4b8ade3ddfb5a2e076e5188cdc8f6cd404f3fdd1/frontend/src/assets/loco.svg" className="logo" alt="Loco logo" />
</a>
</div>
<hr />
<Outlet />
</div>
);
}
function TodoList() {
const queryClient = useQueryClient();
const fetchTodos = async () => {
const { data } = await axios.get(`api/notes`)
return data;
}
const { isLoading, isError, data = [] } = useQuery(["todos"], fetchTodos); // a hook provided by react-query, it takes a key(name) and function that returns a promise
const remove = async (id) => {
try {
const response = await axios.delete(`api/notes/${id}`);
return response.data;
} catch (error) {
console.error('Error posting todo:', error);
throw error;
}
};
const mutation = useMutation(remove, {
onSuccess: () => {
queryClient.invalidateQueries(["todos"]);
},
});
console.log(data)
if (isLoading)
return (
<div className="App">
<p>isLoading...</p>
</div>
);
if (isError)
return (
<div className="App">
<p>Could not get todo list from the server</p>
</div>
);
return (
<div>
<AddTodo />
<div className="todo-list">
{data.map((todo) => (
<div key={todo.id} className="todo" >
<div>
<div> <button onClick={() => {
mutation.mutate(todo.id);
}}>x</button> {todo.title}</div>
</div>
</div>
))}
</div>
</div>
);
}
function AddTodo() {
const [todo, setTodo] = useState("");
const queryClient = useQueryClient();
const add = async (newTodo) => {
try {
const response = await axios.post(`api/notes`, {
title: newTodo,
content: newTodo,
});
return response.data;
} catch (error) {
console.error('Error posting todo:', error);
throw error;
}
};
const mutation = useMutation(add, {
onSuccess: () => {
setTodo("")
queryClient.invalidateQueries(["todos"]);
},
});
return (
<div className='todo-add'>
<input
value={todo}
onChange={(event) => {
setTodo(event.target.value);
}}
type="text"
/>
<button
onClick={() => {
if (todo !== "") {
mutation.mutate(todo);
}
}}
>
Add
</button>
</div>
);
}
function NoMatch() {
return (
<div>
<h2>Sorry, this page not found</h2>
<p>
<Link to="/">Go to the todo list page</Link>
</p>
</div>
);
}