All files / src/pages/flowsheet-page/economics/page/actions RecalculateEconomicsButton.tsx

85.71% Statements 6/7
100% Branches 6/6
100% Functions 0/0
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                              35x 35x       2x           2x 2x           35x                        
import { toast } from "sonner";
import { Button } from "@/ahuora-design-system/ui/button";
import type { EconomicsStudyRead } from "@/api/apiStore.gen";
import { useEconomicsStudiesRecalculateCreateMutation } from "@/api/apiStore.gen";
 
export function RecalculateEconomicsButton({
  study,
  canEdit,
  onRecalculated,
}: {
  study: EconomicsStudyRead;
  canEdit: boolean;
  onRecalculated?: () => void;
}) {
  const [recalculate, recalculateState] =
    useEconomicsStudiesRecalculateCreateMutation();
  const actionLabel = study.result_state?.run_id ? "Recalculate" : "Calculate";
 
  const handleRecalculate = async () => {
    try {
      await recalculate({
        id: study.id,
        recalculateRequest: {
          reason: "User recalculated economics study",
        },
      }).unwrap();
      toast.success("Economics recalculated");
      onRecalculated?.();
    } catch {
      toast.error("Could not recalculate economics");
    }
  };
 
  return (
    <Button
      type="button"
      size="sm"
      onClick={handleRecalculate}
      disabled={!canEdit || recalculateState.isLoading}
      aria-label={`${actionLabel} economics results`}
    >
      {recalculateState.isLoading ? "Calculating" : actionLabel}
    </Button>
  );
}