diff --git a/src/api.ts b/src/api.ts index f83afc4..1b1c12d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,6 @@ import axios from "axios"; import { DateTime } from "luxon"; +import { QueryClient } from "react-query"; type UserInfo = { name: string; @@ -77,15 +78,39 @@ type Template = { embed_fields: EmbedField[] | null; }; +const USER_INFO_STALE_TIME = 120_000; +const GUILD_INFO_STALE_TIME = 300_000; +const OTHER_STALE_TIME = 15_000; + export const fetchUserInfo = () => ({ queryKey: ["USER_INFO"], queryFn: () => axios.get("/dashboard/api/user").then((resp) => resp.data) as Promise, + staleTime: USER_INFO_STALE_TIME, }); export const fetchUserGuilds = () => ({ queryKey: ["USER_GUILDS"], queryFn: () => axios.get("/dashboard/api/user/guilds").then((resp) => resp.data) as Promise, + staleTime: USER_INFO_STALE_TIME, +}); + +export const fetchGuildChannels = (guild: string) => ({ + queryKey: ["GUILD_CHANNELS", guild], + queryFn: () => + axios.get(`/dashboard/api/guild/${guild}/channels`).then((resp) => resp.data) as Promise< + ChannelInfo[] + >, + staleTime: GUILD_INFO_STALE_TIME, +}); + +export const fetchGuildRoles = (guild: string) => ({ + queryKey: ["GUILD_ROLES", guild], + queryFn: () => + axios.get(`/dashboard/api/guild/${guild}/roles`).then((resp) => resp.data) as Promise< + RoleInfo[] + >, + staleTime: GUILD_INFO_STALE_TIME, }); export const fetchGuildReminders = (guild: string) => ({ @@ -101,30 +126,22 @@ export const fetchGuildReminders = (guild: string) => ({ expires: reminder.expires === null ? null : DateTime.fromISO(reminder.expires), })), ) as Promise, + staleTime: OTHER_STALE_TIME, }); -export const fetchGuildChannels = (guild: string) => ({ - queryKey: ["GUILD_CHANNELS", guild], - queryFn: () => - axios.get(`/dashboard/api/guild/${guild}/channels`).then((resp) => resp.data) as Promise< - ChannelInfo[] - >, - staleTime: 300, +export const mutateGuildReminder = (guild: string) => ({ + mutationFn: (reminder: Reminder) => + axios.patch(`/dashboard/api/guild/${guild}/reminders`, { + ...reminder, + utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"), + }), }); -export const fetchGuildRoles = (guild: string) => ({ - queryKey: ["GUILD_ROLES", guild], - queryFn: () => - axios.get(`/dashboard/api/guild/${guild}/roles`).then((resp) => resp.data) as Promise< - RoleInfo[] - >, - staleTime: 300, -}); - -export const guildTemplatesQuery = (guild: string) => ({ +export const fetchGuildTemplates = (guild: string) => ({ queryKey: ["GUILD_TEMPLATES", guild], queryFn: () => - axios.get(`/dashboard/api/guild/${guild}/channels`).then((resp) => resp.data) as Promise< + axios.get(`/dashboard/api/guild/${guild}/templates`).then((resp) => resp.data) as Promise< Template[] >, + staleTime: OTHER_STALE_TIME, }); diff --git a/src/components/Reminder/Content.tsx b/src/components/Reminder/Content.tsx index a648209..c0f359d 100644 --- a/src/components/Reminder/Content.tsx +++ b/src/components/Reminder/Content.tsx @@ -3,7 +3,7 @@ export const Content = ({ value, onChange }) => ( diff --git a/src/components/Reminder/Embed/Color.tsx b/src/components/Reminder/Embed/Color.tsx index 2137910..7f582f5 100644 --- a/src/components/Reminder/Embed/Color.tsx +++ b/src/components/Reminder/Embed/Color.tsx @@ -1,10 +1,19 @@ import { useState } from "preact/hooks"; import { HexColorPicker } from "react-colorful"; import { Modal } from "../../Modal"; +import { Reminder } from "../../../api"; -export const Color = ({ color: colorProp, onChange }) => { +type Props = { + color: string; + setReminder: (r: (reminder: Reminder) => void) => void; +}; + +function colorToInt(hex: string) { + return parseInt(hex.substring(1), 16); +} + +export const Color = ({ color, setReminder }: Props) => { const [modalOpen, setModalOpen] = useState(false); - const [color, setColor] = useState(colorProp); return ( <> @@ -12,7 +21,7 @@ export const Color = ({ color: colorProp, onChange }) => { )}