implement useMutation when sending request

This commit is contained in:
Daniel Roth 2026-03-05 16:11:51 +00:00
parent d1edf1cba5
commit 0660e75af3

View file

@ -1,4 +1,5 @@
import { useState } from "react";
import { useMutation } from "@tanstack/react-query";
import {
DropdownMenu,
DropdownMenuContent,
@ -111,6 +112,15 @@ function SortableScenarioItem({
);
}
function sendCategorisationRequest(selectedScenarios: ScenarioWithPriority[], portfolioId: number) {
const payload = mapScenariosToPayload(selectedScenarios, portfolioId);
return fetch("/api/plan/categorisation", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
}
export function RecommendationsOptions({
disabled = false,
scenarios,
@ -163,29 +173,26 @@ export function RecommendationsOptions({
});
};
const handleSubmit = async () => {
if (selectedScenarios.length === 1) {
setWarning("Cannot generate recommendations for a single scenario");
return;
}
const { mutate, isPending } = useMutation({
mutationFn: () => sendCategorisationRequest(selectedScenarios, portfolioId),
// onSuccess: () => {
// },
// onError: () => {
// }
});
setWarning(null);
setIsApplying(true);
const handleSubmit = () => {
if (selectedScenarios.length === 1) {
setWarning("Cannot generate recommendations for a single scenario");
return;
}
setWarning(null);
mutate();
onSuccess();
setOpen(false);
};
const payload = mapScenariosToPayload(selectedScenarios, portfolioId);
// don't worry about waiting for this response
fetch("/api/plan/categorisation", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
onSuccess();
setIsApplying(false);
setOpen(false);
};
const handleCancel = () => {
setWarning("")
setSelectedScenarios([]);