Fix enabled/disabled button not updating properly

This commit is contained in:
jude 2023-11-26 16:09:52 +00:00
parent c8443340f4
commit 85a9a872c9

View File

@ -1,4 +1,4 @@
import { useState } from "preact/hooks"; import { useRef, useState } from "preact/hooks";
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { patchGuildReminder } from "../../../api"; import { patchGuildReminder } from "../../../api";
import { useParams } from "wouter"; import { useParams } from "wouter";
@ -9,11 +9,16 @@ import { useFlash } from "../../App/FlashContext";
export const EditButtonRow = () => { export const EditButtonRow = () => {
const { guild } = useParams(); const { guild } = useParams();
const [reminder] = useReminder(); const [reminder, setReminder] = useReminder();
const [recentlySaved, setRecentlySaved] = useState(false); const [recentlySaved, setRecentlySaved] = useState(false);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const iconFlashTimeout = useRef(0);
console.log(reminder.enabled);
console.log(reminder.content);
const mutation = useMutation({ const mutation = useMutation({
...patchGuildReminder(guild), ...patchGuildReminder(guild),
onSuccess: () => { onSuccess: () => {
@ -21,7 +26,12 @@ export const EditButtonRow = () => {
queryKey: ["GUILD_REMINDERS", guild], queryKey: ["GUILD_REMINDERS", guild],
}); });
setRecentlySaved(true); setRecentlySaved(true);
setTimeout(() => {
if (iconFlashTimeout.current !== null) {
clearTimeout(iconFlashTimeout.current);
}
iconFlashTimeout.current = setTimeout(() => {
setRecentlySaved(false); setRecentlySaved(false);
}, ICON_FLASH_TIME); }, ICON_FLASH_TIME);
}, },
@ -54,6 +64,10 @@ export const EditButtonRow = () => {
<button <button
class="button is-warning" class="button is-warning"
onClick={() => { onClick={() => {
setReminder((r) => ({
...r,
enabled: !r.enabled,
}));
mutation.mutate({ mutation.mutate({
...reminder, ...reminder,
enabled: !reminder.enabled, enabled: !reminder.enabled,