diff --git a/src/api.ts b/src/api.ts index bb24cf8..2e745b1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -178,3 +178,22 @@ export const fetchGuildTemplates = (guild: string) => ({ >, staleTime: OTHER_STALE_TIME, }); + +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, + }, + }), +}); diff --git a/src/components/Reminder/ButtonRow/CreateButtonRow.tsx b/src/components/Reminder/ButtonRow/CreateButtonRow.tsx index ef579d2..72434f7 100644 --- a/src/components/Reminder/ButtonRow/CreateButtonRow.tsx +++ b/src/components/Reminder/ButtonRow/CreateButtonRow.tsx @@ -1,7 +1,7 @@ import { LoadTemplate } from "../LoadTemplate"; import { useReminder } from "../ReminderContext"; import { useMutation, useQueryClient } from "react-query"; -import { postGuildReminder } from "../../../api"; +import { postGuildReminder, postGuildTemplate } from "../../../api"; import { useParams } from "wouter"; import { useState } from "preact/hooks"; import { ICON_FLASH_TIME } from "../../../consts"; @@ -12,6 +12,7 @@ export const CreateButtonRow = () => { const [reminder] = useReminder(); const [recentlyCreated, setRecentlyCreated] = useState(false); + const [templateRecentlyCreated, setTemplateRecentlyCreated] = useState(false); const flash = useFlash(); const queryClient = useQueryClient(); @@ -39,6 +40,30 @@ export const CreateButtonRow = () => { }, }); + const templateMutation = useMutation({ + ...postGuildTemplate(guild), + onSuccess: (data) => { + if (data.error) { + flash({ + message: data.error, + type: "error", + }); + } else { + flash({ + message: "Template created", + type: "success", + }); + queryClient.invalidateQueries({ + queryKey: ["GUILD_TEMPLATES", guild], + }); + setTemplateRecentlyCreated(true); + setTimeout(() => { + setTemplateRecentlyCreated(false); + }, ICON_FLASH_TIME); + } + }, + }); + return (