reminder-dashboard/src/api.ts

200 lines
5.6 KiB
TypeScript
Raw Permalink Normal View History

2023-10-28 13:50:37 +00:00
import axios from "axios";
2023-10-28 18:21:13 +00:00
import { DateTime } from "luxon";
2023-10-28 13:50:37 +00:00
type UserInfo = {
name: string;
patreon: boolean;
timezone: string | null;
};
export type GuildInfo = {
patreon: boolean;
2023-10-28 13:50:37 +00:00
name: string;
error?: string;
2023-10-28 13:50:37 +00:00
};
2023-10-28 18:21:13 +00:00
type EmbedField = {
title: string;
value: string;
inline: boolean;
};
export type Reminder = {
attachment: string | null;
attachment_name: string | null;
avatar: string | null;
channel: string;
content: string;
embed_author: string;
embed_author_url: string | null;
embed_color: number;
embed_description: string;
embed_footer: string;
embed_footer_url: string | null;
embed_image_url: string | null;
embed_thumbnail_url: string | null;
embed_title: string;
embed_fields: EmbedField[] | null;
enabled: boolean;
expires: DateTime | null;
interval_seconds: number | null;
interval_days: number | null;
interval_months: number | null;
name: string;
restartable: boolean;
tts: boolean;
uid: string;
username: string;
utc_time: DateTime;
};
2023-11-05 12:41:59 +00:00
export type ChannelInfo = {
2023-10-28 18:21:13 +00:00
id: string;
name: string;
};
2023-11-03 19:17:16 +00:00
type RoleInfo = {
id: string;
name: string;
};
2023-11-01 22:10:56 +00:00
type Template = {
id: number;
name: string;
attachment: string | null;
attachment_name: string | null;
avatar: string | null;
channel: string;
content: string;
embed_author: string;
embed_author_url: string | null;
embed_color: number;
embed_description: string;
embed_footer: string;
embed_footer_url: string | null;
embed_image_url: string | null;
embed_thumbnail_url: string | null;
embed_title: string;
embed_fields: EmbedField[] | null;
};
2023-11-03 22:40:57 +00:00
const USER_INFO_STALE_TIME = 120_000;
const GUILD_INFO_STALE_TIME = 300_000;
const OTHER_STALE_TIME = 15_000;
2023-11-03 19:17:16 +00:00
export const fetchUserInfo = () => ({
queryKey: ["USER_INFO"],
queryFn: () => axios.get("/dashboard/api/user").then((resp) => resp.data) as Promise<UserInfo>,
2023-11-03 22:40:57 +00:00
staleTime: USER_INFO_STALE_TIME,
2023-11-03 19:17:16 +00:00
});
2023-10-28 13:50:37 +00:00
2023-11-05 17:01:47 +00:00
export const patchUserInfo = () => ({
2023-11-10 15:31:04 +00:00
mutationFn: (timezone: string) => axios.patch(`/dashboard/api/user`, { timezone }),
2023-11-05 17:01:47 +00:00
});
2023-11-03 19:17:16 +00:00
export const fetchUserGuilds = () => ({
queryKey: ["USER_GUILDS"],
queryFn: () =>
axios.get("/dashboard/api/user/guilds").then((resp) => resp.data) as Promise<GuildInfo[]>,
2023-11-03 22:40:57 +00:00
staleTime: USER_INFO_STALE_TIME,
});
export const fetchGuildInfo = (guild: string) => ({
queryKey: ["GUILD_INFO", guild],
queryFn: () =>
axios.get(`/dashboard/api/guild/${guild}`).then((resp) => resp.data) as Promise<GuildInfo>,
staleTime: GUILD_INFO_STALE_TIME,
});
2023-11-03 22:40:57 +00:00
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,
2023-11-03 19:17:16 +00:00
});
2023-10-28 18:21:13 +00:00
2023-11-01 22:10:56 +00:00
export const fetchGuildReminders = (guild: string) => ({
queryKey: ["GUILD_REMINDERS", guild],
queryFn: () =>
axios
.get(`/dashboard/api/guild/${guild}/reminders`)
.then((resp) => resp.data)
.then((value) =>
value.map((reminder) => ({
...reminder,
2023-11-04 18:59:39 +00:00
utc_time: DateTime.fromISO(reminder.utc_time, { zone: "UTC" }),
expires:
reminder.expires === null
? null
: DateTime.fromISO(reminder.expires, { zone: "UTC" }),
2023-11-01 22:10:56 +00:00
})),
) as Promise<Reminder[]>,
2023-11-03 22:40:57 +00:00
staleTime: OTHER_STALE_TIME,
2023-11-01 22:10:56 +00:00
});
2023-10-28 18:21:13 +00:00
2023-11-04 17:45:54 +00:00
export const patchGuildReminder = (guild: string) => ({
2023-11-03 22:40:57 +00:00
mutationFn: (reminder: Reminder) =>
axios.patch(`/dashboard/api/guild/${guild}/reminders`, {
...reminder,
utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"),
}),
2023-11-03 19:17:16 +00:00
});
2023-11-04 18:59:39 +00:00
export const postGuildReminder = (guild: string) => ({
mutationFn: (reminder: Reminder) =>
axios
.post(`/dashboard/api/guild/${guild}/reminders`, {
...reminder,
utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"),
})
.then((resp) => resp.data),
2023-11-04 18:59:39 +00:00
});
2023-11-04 17:45:54 +00:00
export const deleteGuildReminder = (guild: string) => ({
mutationFn: (reminder: Reminder) =>
axios.delete(`/dashboard/api/guild/${guild}/reminders`, {
data: {
uid: reminder.uid,
},
}),
});
2023-11-03 22:40:57 +00:00
export const fetchGuildTemplates = (guild: string) => ({
2023-11-01 22:10:56 +00:00
queryKey: ["GUILD_TEMPLATES", guild],
queryFn: () =>
2023-11-03 22:40:57 +00:00
axios.get(`/dashboard/api/guild/${guild}/templates`).then((resp) => resp.data) as Promise<
2023-11-01 22:10:56 +00:00
Template[]
>,
2023-11-03 22:40:57 +00:00
staleTime: OTHER_STALE_TIME,
2023-11-01 22:10:56 +00:00
});
2023-11-06 18:11:18 +00:00
export const postGuildTemplate = (guild: string) => ({
mutationFn: (reminder: Reminder) =>
axios
.post(`/dashboard/api/guild/${guild}/templates`, {
...reminder,
utc_time: reminder.utc_time.toFormat("yyyy-LL-dd'T'HH:mm:ss"),
})
.then((resp) => resp.data),
});
export const deleteGuildTemplate = (guild: string) => ({
mutationFn: (template: Template) =>
axios.delete(`/dashboard/api/guild/${guild}/templates`, {
data: {
id: template.id,
},
}),
});