import { useMutation } from "@apollo/client/react";
import { DELETE_ALLOCATION } from "../../graphql/allocations.queries";
import AllocationsLabelComponent from "./allocations-employee-label.component";
import { useTranslation } from "react-i18next";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";

export default function AllocationsLabelContainer({ allocation, refetch }) {
  const [deleteAllocation] = useMutation(DELETE_ALLOCATION);
  const { t } = useTranslation();
  const notification = useNotification();

  const handleClick = (e) => {
    e.preventDefault();
    deleteAllocation({ variables: { id: allocation.id } })
      .then(() => {
        notification.success({
          title: t("allocations.successes.deleted")
        });
        if (refetch) refetch();
      })
      .catch(() => {
        notification.error({ title: t("allocations.errors.deleting") });
      });
  };

  return <AllocationsLabelComponent allocation={allocation} handleClick={handleClick} />;
}
