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

85.71% Statements 12/14
75% Branches 9/12
100% Functions 1/1
92.3% Lines 12/13

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                              50x 50x 50x 50x 50x 50x     2x 2x   2x             2x 2x                                                                     2x                                                
import { useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/ahuora-design-system/ui/dialog";
import { Input } from "@/ahuora-design-system/ui/input";
import { Label } from "@/ahuora-design-system/ui/label";
import { useEconomicsStudiesCreateMutation } from "@/api/apiStore.gen";
import { useProjectId } from "@/hooks/project";
 
export function CreateEconomicsStudyButton({
  compact = false,
  onCreated,
}: {
  compact?: boolean;
  onCreated?: (studyId: number) => void;
}) {
  const flowsheetId = useProjectId();
  const [createStudy, createStudyState] = useEconomicsStudiesCreateMutation();
  const [error, setError] = useState<string | null>(null);
  const [open, setOpen] = useState(false);
  const [studyName, setStudyName] = useState("Economics study");
  const disabled = createStudyState.isLoading || !flowsheetId;
 
  const handleCreate = async () => {
    Iif (!flowsheetId) return;
    setError(null);
    try {
      const study = await createStudy({
        flowsheet: flowsheetId,
        economicsStudy: {
          name: studyName.trim() || "Economics study",
          description: "Created from the economics workspace.",
        },
      }).unwrap();
      setOpen(false);
      onCreated?.(study.id);
    } catch {
      setError("Could not create the economics study.");
    }
  };
 
  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        <Button
          type="button"
          size="sm"
          variant={compact ? "default" : "outline"}
          aria-label="Create economics study"
        >
          Create study
        </Button>
      </DialogTrigger>
      <DialogContent
        className="sm:max-w-md"
        aria-label="Create economics study dialog"
      >
        <DialogHeader>
          <DialogTitle>Create economics study</DialogTitle>
          <DialogDescription>
            Create a flowsheet-level economics study.
          </DialogDescription>
        </DialogHeader>
        <div className="space-y-4">
          <div className="space-y-2">
            <Label htmlFor="economics-study-name">Study name</Label>
            <Input
              id="economics-study-name"
              aria-label="Economics study name"
              value={studyName}
              onChange={(event) => setStudyName(event.target.value)}
            />
          </div>
          {error && (
            <p className="text-xs text-destructive" role="alert">
              {error}
            </p>
          )}
        </div>
        <DialogFooter>
          <Button
            type="button"
            size="sm"
            onClick={handleCreate}
            disabled={disabled}
            aria-label="Save economics study"
          >
            {createStudyState.isLoading ? "Creating study" : "Save study"}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}