import { Button } from "antd";
import Axios from "axios";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";

export default function JobCalculateTotals({ job, disabled, refetch }) {
  const { t } = useTranslation();
  const [loading, setLoading] = useState(false);
  const notification = useNotification();

  const handleCalculate = async () => {
    try {
      setLoading(true);

      await Axios.post("/job/totalsssu", {
        id: job.id
      });

      if (refetch) refetch();
    } catch (error) {
      notification.error({
        title: t("jobs.errors.updating", {
          error: JSON.stringify(error)
        })
      });
    } finally {
      setLoading(false);
    }
  };

  return (
    <div>
      <Button loading={loading} onClick={handleCalculate} disabled={disabled}>
        {t("jobs.actions.recalculate")}
      </Button>
    </div>
  );
}
