fix: platform event-type atom team delete (#17225)

* fix: platform event-type atom team delete

* fixup! fix: platform event-type atom team delete

* fixup! fixup! fix: platform event-type atom team delete
This commit is contained in:
Morgan
2024-10-21 16:03:04 +00:00
committed by GitHub
parent 5f6032131b
commit 9087f5307b
2 changed files with 80 additions and 9 deletions
@@ -11,6 +11,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
import { SchedulingType } from "@calcom/prisma/enums";
import { useDeleteEventTypeById } from "../../hooks/event-types/private/useDeleteEventTypeById";
import { useDeleteTeamEventTypeById } from "../../hooks/event-types/private/useDeleteTeamEventTypeById";
import { useMe } from "../../hooks/useMe";
import { AtomsWrapper } from "../../src/components/atoms-wrapper";
import { useToast } from "../../src/components/ui/use-toast";
@@ -40,6 +41,7 @@ export type EventTypePlatformWrapperProps = {
customClassNames?: {
atomsWrapper?: string;
};
disableToasts?: boolean;
};
const EventType = ({
@@ -51,6 +53,7 @@ const EventType = ({
id,
allowDelete = true,
customClassNames,
disableToasts = false,
...props
}: EventTypeSetupProps & EventTypePlatformWrapperProps) => {
const { t } = useLocale();
@@ -63,17 +66,34 @@ const EventType = ({
const [slugExistsChildrenDialogOpen, setSlugExistsChildrenDialogOpen] = useState<ChildrenEventType[]>([]);
const { data: user, isLoading: isUserLoading } = useMe();
const handleDeleteSuccess = () => {
showToast(t("event_type_deleted_successfully"), "success");
isTeamEventTypeDeleted.current = true;
setSlugExistsChildrenDialogOpen([]);
setIsOpenAssignmentWarnDialog(false);
onDeleteSuccess?.();
};
const handleDeleteError = (err: Error) => {
showToast(err.message, "error");
onDeleteError?.(err.message);
};
const deleteMutation = useDeleteEventTypeById({
onSuccess: async () => {
showToast(t("event_type_deleted_successfully"), "success");
isTeamEventTypeDeleted.current = true;
setSlugExistsChildrenDialogOpen([]);
setIsOpenAssignmentWarnDialog(false);
onDeleteSuccess?.();
handleDeleteSuccess();
},
onError: (err) => {
showToast(err.message, "error");
onDeleteError?.(err.message);
handleDeleteError(err);
},
});
const deleteTeamEventTypeMutation = useDeleteTeamEventTypeById({
onSuccess: async () => {
handleDeleteSuccess();
},
onError: (err) => {
handleDeleteError(err);
},
});
@@ -111,7 +131,9 @@ const EventType = ({
const slug = form.watch("slug") ?? eventType.slug;
const showToast = (message: string, variant: "success" | "warning" | "error") => {
toast({ description: message });
if (!disableToasts) {
toast({ description: message });
}
};
const tabMap = {
@@ -190,9 +212,12 @@ const EventType = ({
const onDelete = () => {
if (allowDelete) {
isTeamEventTypeDeleted.current = true;
deleteMutation.mutate(id);
team?.id
? deleteTeamEventTypeMutation.mutate({ eventTypeId: id, teamId: team.id })
: deleteMutation.mutate(id);
}
};
const onConflict = (conflicts: ChildrenEventType[]) => {
setSlugExistsChildrenDialogOpen(conflicts);
};
@@ -0,0 +1,46 @@
import { useMutation } from "@tanstack/react-query";
import { V2_ENDPOINTS, SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, ApiSuccessResponse } from "@calcom/platform-types";
import type { EventType } from "@calcom/prisma/client";
import http from "../../../lib/http";
import { useAtomsContext } from "../../useAtomsContext";
export const QUERY_KEY = "use-delete-team-event-by-id";
export type UseDeleteTeamEventTypeProps = {
onSuccess?: (data: ResponseDataType) => void;
onError?: (err: Error) => void;
onSettled?: () => void;
};
export type ResponseDataType = NonNullable<
Pick<EventType, "id" | "slug" | "title"> & { lengthInMinutes: number }
>;
export const useDeleteTeamEventTypeById = ({
onSuccess,
onError,
onSettled,
}: UseDeleteTeamEventTypeProps) => {
const { organizationId } = useAtomsContext();
return useMutation({
onSuccess,
onError,
onSettled,
mutationFn: ({ eventTypeId, teamId }: { eventTypeId: number; teamId: number }) => {
if (!eventTypeId) throw new Error("Event type id is required");
if (!teamId) throw new Error("team id is required");
if (!organizationId) throw new Error("organization id is required");
const pathname = `/organizations/${organizationId}/teams/${teamId}/${V2_ENDPOINTS.eventTypes}/${eventTypeId}`;
return http?.delete<ApiResponse<ResponseDataType>>(pathname).then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return (res.data as ApiSuccessResponse<ResponseDataType>).data;
}
throw new Error(res.data.error.message);
});
},
});
};