import { useQuery } from "react-query"; import { fetchGuildChannels, fetchGuildTodos } from "../../api"; import { useGuild } from "../App/useGuild"; import { Todo } from "../Todo"; import { Loader } from "../Loader"; import { CreateTodo } from "../Todo/CreateTodo"; export const GuildTodos = () => { const guild = useGuild(); const { isFetched, data: guildTodos } = useQuery(fetchGuildTodos(guild)); const { data: channels } = useQuery(fetchGuildChannels(guild)); if (!isFetched || !channels) { return ; } const sortedTodos = guildTodos .sort((a, b) => (a.id > b.id ? -1 : 1)) .sort((a, b) => (a.channel_id < b.channel_id ? 0 : 1)); let prevChannel; return ( <> Create Todo
Todo list {sortedTodos.map((todo) => { if (prevChannel !== todo.channel_id) { prevChannel = todo.channel_id; if (todo.channel_id === null) { return ( <>

Server Todos

); } else { const channel = channels.find((ch) => ch.id === todo.channel_id); return ( <>

#{channel.name} Todos

); } } return ; })} ); };