Don't show empty channels

This commit is contained in:
jude 2024-04-16 11:42:19 +01:00
parent d52b8b26f2
commit e38c63f5ba
2 changed files with 64 additions and 25 deletions

View File

@ -1,7 +1,8 @@
import { useQuery } from "react-query"; import { useQuery } from "react-query";
import { fetchGuildChannels, fetchGuildTodos } from "../../api"; import { ChannelInfo, fetchGuildChannels, fetchGuildTodos } from "../../api";
import { useGuild } from "../App/useGuild"; import { useGuild } from "../App/useGuild";
import { Todo } from "../Todo"; import { Todo } from "../Todo";
import { Todo as TodoT } from "../../api";
import { Loader } from "../Loader"; import { Loader } from "../Loader";
import { CreateTodo } from "../Todo/CreateTodo"; import { CreateTodo } from "../Todo/CreateTodo";
@ -15,34 +16,47 @@ export const GuildTodos = () => {
return <Loader />; return <Loader />;
} }
const sortedTodos = guildTodos.sort((a, b) => (a.id > b.id ? -1 : 1)); const sortedTodos = guildTodos.sort((a, b) => (a.id < b.id ? -1 : 1));
const globalTodos = sortedTodos.filter((todo) => todo.channel_id === null);
return ( return (
<> <>
<h2>Server</h2> <strong>Create todo list</strong>
{sortedTodos <CreateTodo channel={null} showSelector={true} />
.filter((todo) => todo.channel_id === null) <strong>Todo lists</strong>
.map((todo) => ( {globalTodos.length > 0 && (
<> <>
<Todo todo={todo} key={todo.id} /> <h2>Server</h2>
</> {globalTodos.map((todo) => (
))} <>
<CreateTodo channel={null} /> <Todo todo={todo} key={todo.id} />
{channels.map((channel) => { </>
return ( ))}
<> <CreateTodo channel={null} />
<h2>#{channel.name}</h2> </>
{sortedTodos )}
.filter((todo) => todo.channel_id === channel.id) {channels
.map((todo) => ( .map(
(channel) =>
[channel, sortedTodos.filter((todo) => todo.channel_id === channel.id)] as [
ChannelInfo,
TodoT[],
],
)
.filter(([_, todos]) => todos.length > 0)
.map(([channel, todos]) => {
return (
<>
<h2>#{channel.name}</h2>
{todos.map((todo) => (
<> <>
<Todo todo={todo} key={todo.id} /> <Todo todo={todo} key={todo.id} />
</> </>
))} ))}
<CreateTodo channel={channel.id} /> <CreateTodo channel={channel.id} />
</> </>
); );
})} })}
</> </>
); );
}; };

View File

@ -1,11 +1,11 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQuery, useQueryClient } from "react-query";
import { postGuildTodo } from "../../api"; import { fetchGuildChannels, postGuildTodo } from "../../api";
import { useGuild } from "../App/useGuild"; import { useGuild } from "../App/useGuild";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
import { useFlash } from "../App/FlashContext"; import { useFlash } from "../App/FlashContext";
import { ICON_FLASH_TIME } from "../../consts"; import { ICON_FLASH_TIME } from "../../consts";
export const CreateTodo = ({ channel }) => { export const CreateTodo = ({ showSelector = false, channel }) => {
const guild = useGuild(); const guild = useGuild();
const [recentlyCreated, setRecentlyCreated] = useState(false); const [recentlyCreated, setRecentlyCreated] = useState(false);
@ -13,6 +13,8 @@ export const CreateTodo = ({ channel }) => {
const flash = useFlash(); const flash = useFlash();
const { isSuccess, data: channels } = useQuery(fetchGuildChannels(guild));
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const mutation = useMutation({ const mutation = useMutation({
...postGuildTodo(guild), ...postGuildTodo(guild),
@ -44,6 +46,29 @@ export const CreateTodo = ({ channel }) => {
class="input todo-input" class="input todo-input"
onInput={(ev) => setNewTodo((todo) => ({ ...todo, value: ev.currentTarget.value }))} onInput={(ev) => setNewTodo((todo) => ({ ...todo, value: ev.currentTarget.value }))}
/> />
{showSelector && (
<div class="control has-icons-left">
<div class="select">
<select
name="channel"
class="channel-selector"
onInput={(ev) =>
setNewTodo((todo) => ({
...todo,
channel_id: ev.currentTarget.value || null,
}))
}
>
<option value="">(None)</option>
{isSuccess &&
channels.map((c) => <option value={c.id}>{c.name}</option>)}
</select>
</div>
<div class="icon is-small is-left">
<i class="fas fa-hashtag"></i>
</div>
</div>
)}
<button onClick={() => mutation.mutate(newTodo)} class="button is-success save-btn"> <button onClick={() => mutation.mutate(newTodo)} class="button is-success save-btn">
<span class="icon"> <span class="icon">
{mutation.isLoading ? ( {mutation.isLoading ? (