Skip to content

Commit

Permalink
Merge pull request #25 from tnamdevnote/fix/revalidate-on-submit
Browse files Browse the repository at this point in the history
Fix/Revalidate Appointment List on Submit
  • Loading branch information
tnamdevnote authored May 10, 2024
2 parents ce0d5f8 + 04aa5f0 commit 95038f0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { cn, formatToDisplayDate, formatToDisplayTime } from "@/lib/utils";
import AppointmentDateTimePicker from "../appointmentDateTimePicker/appointmentDateTimePicker";
import { APPOINTMENT_QUERYResult } from "@/lib/sanity/sanity.types";
import { SplitContainer } from "@/components/templates/container";
import { useRouter } from "next/navigation";

/**
* Extracted async calls into its own functions to manage them separate from rendering logic.
Expand Down Expand Up @@ -97,6 +98,7 @@ export const AppointmentForm = ({
}: AppointmentFormProps) => {
const submitBtnLabel = (mode === "create" ? "Book" : "Edit") + " Appointment";
const { toast } = useToast();
const router = useRouter();
const form = useForm<z.infer<typeof FormSchema>>({
defaultValues:
mode === "edit" && !!appointment
Expand Down Expand Up @@ -125,6 +127,7 @@ export const AppointmentForm = ({
await sendEmail(values);

onClose ? onClose() : null;
router.refresh();
toast({
title: `Your appointment has been successfully ${mode === "create" ? "booked" : "updated"}!`,
intent: "success",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { useToast } from "@/components/molecules/toast";
import { Button } from "@/components/atoms/button";
import cancel_img from "./cancel_img.svg";
import { useRouter } from "next/navigation";

const cancelAppointment = async (
url: string,
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function CancelDialog({
}: CancelDialogProps) {
const { toast } = useToast();
const [open, setOpen] = useState(false);
const router = useRouter();
const { trigger: swrTrigger, isMutating } = useSWRMutation(
"/api/my-appointments",
cancelAppointment,
Expand All @@ -54,6 +56,7 @@ export default function CancelDialog({
try {
await swrTrigger({ appointmentId });
if (!isMutating) {
router.refresh();
setOpen(false);
toast({
title: "Your appointment has been cancelled!",
Expand Down
3 changes: 0 additions & 3 deletions lib/sanity/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ export const getAppointments = async (userId = "") => {
stylist->{"id": _id, firstName, lastName}
}
`,
{},
// this will let next js trigger revalidation when revalidate function is called
{ next: { tags: ["appointments"] } },
);

return res;
Expand Down

0 comments on commit 95038f0

Please sign in to comment.