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 (
Loco Todo List
}>
} />
} />
)
}
function Layout() {
return (
);
}
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 (
);
if (isError)
return (
Could not get todo list from the server
);
return (
{data.map((todo) => (
{todo.title}
))}
);
}
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 (
{
setTodo(event.target.value);
}}
type="text"
/>
);
}
function NoMatch() {
return (
Sorry, this page not found
Go to the todo list page
);
}