not awaiting response from fastapi before redirecting user

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-24 10:59:49 +01:00
parent cb7ba9e0b3
commit f758996707
2 changed files with 34 additions and 21 deletions

View file

@ -33,24 +33,36 @@ export async function POST(request: NextRequest) {
Authorization: `Bearer ${
request.cookies.get("next-auth.session-token")?.value
}`,
"Content-Type": "application/json",
};
const response = await fetch(
`${process.env.FASTAPI_API_URL}/v1/plan/trigger`,
{
method: "POST",
headers: headers,
body: JSON.stringify(validatedBody),
}
);
// const response = await fetch(
// `${process.env.FASTAPI_API_URL}/v1/plan/trigger`,
// {
// method: "POST",
// headers: headers,
// body: JSON.stringify(validatedBody),
// }
// );
if (!response.ok) {
throw new Error("API request failed");
}
// if (!response.ok) {
// throw new Error("API request failed");
// }
const responseData = await response.json();
// const responseData = await response.json();
return new NextResponse(JSON.stringify(responseData), {
// return new NextResponse(JSON.stringify(responseData), {
// status: 200,
// });
// For the moment, we don't await any response from the backend, since we haven't split out the trigger
fetch(`${process.env.FASTAPI_API_URL}/v1/plan/trigger`, {
method: "POST",
headers: headers,
body: JSON.stringify(validatedBody),
});
return new NextResponse(JSON.stringify({ msg: "Job started" }), {
status: 200,
});
} catch (error) {

View file

@ -3,6 +3,7 @@
import { useRouter } from "next/navigation";
import { useMutation } from "@tanstack/react-query";
import { useSession } from "next-auth/react";
import { useMemo } from "react";
function generateS3Key(userId: number, portfolioId: number, filename: string) {
const timestamp = new Date().toISOString().replace(/[:.-]/g, "");
@ -36,16 +37,17 @@ const useCreatePlan = ({
const userId = session.data?.user.dbId;
const fileKey = generateS3Key(
userId,
portfolioId,
"portfolio_plan_properties"
// Every time the component is re-rendered, a new file key will be generated. To prevent this,
// we use useMemo to only generate a new file key when the userId or portfolioId changes.
const fileKey = useMemo(
() => generateS3Key(userId, portfolioId, "portfolio_plan_properties"),
[userId, portfolioId]
);
const { mutate: mutateUploadCsv, isLoading: isUploadLoading } = useMutation(
uploadCsvToS3,
{
onSuccess: (data) => {
onSuccess: () => {
const body = JSON.stringify({
portfolio_id: portfolioId,
housing_type: housingType,
@ -108,6 +110,8 @@ async function generatePresignedUrl({
portfolioId: number;
fileKey: string;
}) {
console.log("fileKey: ", fileKey);
const body = JSON.stringify({
userId: userId,
portfolioId: portfolioId,
@ -166,9 +170,6 @@ export const SubmitPlan = ({
file: File;
portfolioId: number;
}) => {
const router = useRouter();
const session = useSession();
const { handlePlanBuild, isGeneratingUrlLoading, isUploadLoading } =
useCreatePlan({ portfolioId, housingType, goal, goalValue, file });