mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
implement useMutation when sending request
This commit is contained in:
parent
d1edf1cba5
commit
0660e75af3
1 changed files with 28 additions and 21 deletions
|
|
@ -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([]);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue