Skip to content

Scheduling infrastructure for absolutely everyone.

feat: optimistically update when deleting an availability (#4648)

Nafees Nazikf14b13a1bad9
diff --git a/apps/web/pages/availability/index.tsx b/apps/web/pages/availability/index.tsx
index 05d9895db85..f4434ea9c19 100644
--- a/apps/web/pages/availability/index.tsx
+++ b/apps/web/pages/availability/index.tsx
@@ -51,7 +51,6 @@ export function AvailabilityList({ schedules }: inferQueryOutput<"viewer.availab
key={schedule.id}
schedule={schedule}
deleteFunction={deleteMutation.mutate}
- isDeleting={deleteMutation.isLoading}
/>
))}
</ul>
diff --git a/apps/web/pages/v2/availability/index.tsx b/apps/web/pages/v2/availability/index.tsx
index 86450503ab1..dad8dca0ff4 100644
--- a/apps/web/pages/v2/availability/index.tsx
+++ b/apps/web/pages/v2/availability/index.tsx
@@ -21,16 +21,32 @@ export function AvailabilityList({ schedules }: inferQueryOutput<"viewer.availab
const meQuery = trpc.useQuery(["viewer.me"]);
const deleteMutation = trpc.useMutation("viewer.availability.schedule.delete", {
- onSuccess: async () => {
- await utils.invalidateQueries(["viewer.availability.list"]);
- showToast(t("schedule_deleted_successfully"), "success");
+ onMutate: async ({ scheduleId }) => {
+ await utils.cancelQuery(["viewer.availability.list"]);
+ const previousValue = utils.getQueryData(["viewer.availability.list"]);
+ if (previousValue) {
+ const filteredValue = previousValue.schedules.filter(({ id }) => id !== scheduleId);
+ utils.setQueryData(["viewer.availability.list"], { ...previousValue, schedules: filteredValue });
+ }
+
+ return { previousValue };
},
- onError: (err) => {
+
+ onError: (err, variables, context) => {
+ if (context?.previousValue) {
+ utils.setQueryData(["viewer.availability.list"], context.previousValue);
+ }
if (err instanceof HttpError) {
const message = `${err.statusCode}: ${err.message}`;
showToast(message, "error");
}
},
+ onSettled: () => {
+ utils.invalidateQueries(["viewer.availability.list"]);
+ },
+ onSuccess: () => {
+ showToast(t("schedule_deleted_successfully"), "success");
+ },
});
// Adds smooth delete button - item fades and old item slides into place
@@ -61,7 +77,6 @@ export function AvailabilityList({ schedules }: inferQueryOutput<"viewer.availab
key={schedule.id}
schedule={schedule}
deleteFunction={deleteMutation.mutate}
- isDeleting={deleteMutation.isLoading}
/>
))}
</ul>
diff --git a/packages/features/schedules/components/ScheduleListItem.tsx b/packages/features/schedules/components/ScheduleListItem.tsx
index 94099b07eef..254b8732227 100644
--- a/packages/features/schedules/components/ScheduleListItem.tsx
+++ b/packages/features/schedules/components/ScheduleListItem.tsx
@@ -13,12 +13,10 @@ import Button from "@calcom/ui/v2/core/Button";
export function ScheduleListItem({
schedule,
deleteFunction,
- isDeleting = false,
displayOptions,
}: {
schedule: inferQueryOutput<"viewer.availability.list">["schedules"][number];
deleteFunction: ({ scheduleId }: { scheduleId: number }) => void;
- isDeleting: boolean;
displayOptions?: {
timeZone?: string;
hour12?: boolean;
@@ -67,7 +65,6 @@ export function ScheduleListItem({
<DropdownMenuContent>
<DropdownMenuItem>
<Button
- disabled={isDeleting}
onClick={() => {
deleteFunction({
scheduleId: schedule.id,
@@ -76,9 +73,8 @@ export function ScheduleListItem({
type="button"
color="destructive"
className="w-full font-normal"
- StartIcon={isDeleting ? undefined : Icon.FiTrash}
- loading={isDeleting}>
- {isDeleting ? t("deleting") : t("delete_schedule")}
+ StartIcon={Icon.FiTrash}>
+ {t("delete_schedule")}
</Button>
</DropdownMenuItem>
</DropdownMenuContent>