import { useApolloClient } from "@apollo/client"; import { useDeletePostMutation } from "@hey/indexer"; import type { ApolloClientError } from "@hey/types/errors"; import { useCallback } from "react"; import { toast } from "sonner"; import { Alert } from "@/components/Shared/UI"; import errorToast from "@/helpers/errorToast"; import useTransactionLifecycle from "@/hooks/useTransactionLifecycle"; import { useDeletePostAlertStore } from "@/store/non-persisted/alert/useDeletePostAlertStore"; const DeletePost = () => { const { deletingPost, setShowPostDeleteAlert, showPostDeleteAlert } = useDeletePostAlertStore(); const { cache } = useApolloClient(); const handleTransactionLifecycle = useTransactionLifecycle(); const updateCache = () => { cache.evict({ id: `${deletingPost?.__typename}:${deletingPost?.id}` }); }; const onCompleted = () => { setShowPostDeleteAlert(false); updateCache(); toast.success("Post deleted"); }; const onError = useCallback((error: ApolloClientError) => { errorToast(error); }, []); const [deletePost, { loading }] = useDeletePostMutation({ onCompleted: async ({ deletePost }) => { if (deletePost.__typename === "DeletePostResponse") { return onCompleted(); } return await handleTransactionLifecycle({ onCompleted, onError, transactionData: deletePost }); } }); const handleDelete = async () => { return await deletePost({ variables: { request: { post: deletingPost?.id } } }); }; return ( setShowPostDeleteAlert(false)} onConfirm={handleDelete} show={showPostDeleteAlert} title="Delete Post?" /> ); }; export default DeletePost;