From 4c10682de8c2f4e306fa5a0ca98bcf5b03687906 Mon Sep 17 00:00:00 2001 From: jude Date: Mon, 27 Nov 2023 18:32:53 +0000 Subject: [PATCH] Add sort options --- src/components/Guild/GuildReminders.tsx | 83 +++++++++++++++++-- .../Reminder/ButtonRow/EditButtonRow.tsx | 27 +++--- src/components/Reminder/EditReminder.tsx | 17 +++- 3 files changed, 104 insertions(+), 23 deletions(-) diff --git a/src/components/Guild/GuildReminders.tsx b/src/components/Guild/GuildReminders.tsx index 1b330e4..7d3aaa4 100644 --- a/src/components/Guild/GuildReminders.tsx +++ b/src/components/Guild/GuildReminders.tsx @@ -1,13 +1,26 @@ import { useParams } from "wouter"; import { useQuery } from "react-query"; -import { fetchGuildReminders } from "../../api"; +import { fetchGuildChannels, fetchGuildReminders } from "../../api"; import { EditReminder } from "../Reminder/EditReminder"; import { CreateReminder } from "../Reminder/CreateReminder"; +import { useState } from "preact/hooks"; + +enum Sort { + Time = "time", + Name = "name", + Channel = "channel", +} export const GuildReminders = () => { const { guild } = useParams(); const { isSuccess, data: guildReminders } = useQuery(fetchGuildReminders(guild)); + const { data: channels } = useQuery(fetchGuildChannels(guild)); + + const [collapsed, setCollapsed] = useState(false); + const [sort, setSort] = useState(Sort.Time); + + let prevReminder = null; return (
@@ -24,12 +37,21 @@ export const GuildReminders = () => {
- { + setSort(ev.currentTarget.value as Sort); + }} + > + - - + +
@@ -40,7 +62,16 @@ export const GuildReminders = () => {
- { + if (ev.currentTarget.value === "expand") { + setCollapsed(false); + } else if (ev.currentTarget.value === "collapse") { + setCollapsed(true); + } + }} + > @@ -56,9 +87,43 @@ export const GuildReminders = () => {
{isSuccess && - guildReminders.map((reminder) => ( - - ))} + guildReminders + .sort((r1, r2) => { + if (sort === Sort.Time) { + return r1.utc_time > r2.utc_time ? 1 : -1; + } else if (sort === Sort.Name) { + return r1.name > r2.name ? 1 : -1; + } else { + return r1.channel > r2.channel ? 1 : -1; + } + }) + .map((reminder) => { + let breaker = <>; + if (sort === Sort.Channel && channels) { + if ( + prevReminder === null || + prevReminder.channel !== reminder.channel + ) { + const channel = channels.find( + (ch) => ch.id === reminder.channel, + ); + breaker =
#{channel.name}
; + } + } + + prevReminder = reminder; + + return ( + <> + {breaker} + + + ); + })}
); diff --git a/src/components/Reminder/ButtonRow/EditButtonRow.tsx b/src/components/Reminder/ButtonRow/EditButtonRow.tsx index 8500966..a47b74f 100644 --- a/src/components/Reminder/ButtonRow/EditButtonRow.tsx +++ b/src/components/Reminder/ButtonRow/EditButtonRow.tsx @@ -16,24 +16,33 @@ export const EditButtonRow = () => { const iconFlashTimeout = useRef(0); - console.log(reminder.enabled); - console.log(reminder.content); - + const flash = useFlash(); const mutation = useMutation({ ...patchGuildReminder(guild), - onSuccess: () => { + onSuccess: (response) => { queryClient.invalidateQueries({ queryKey: ["GUILD_REMINDERS", guild], }); - setRecentlySaved(true); if (iconFlashTimeout.current !== null) { clearTimeout(iconFlashTimeout.current); } - iconFlashTimeout.current = setTimeout(() => { + if (response.data.errors.length > 0) { setRecentlySaved(false); - }, ICON_FLASH_TIME); + + for (const error of response.data.errors) { + flash({ message: error, type: "error" }); + } + } else { + setRecentlySaved(true); + + iconFlashTimeout.current = setTimeout(() => { + setRecentlySaved(false); + }, ICON_FLASH_TIME); + + setReminder(response.data.reminder); + } }, }); @@ -64,10 +73,6 @@ export const EditButtonRow = () => {