From bc9ddbd7e26382702a0d92c1a8fab34757c2ed56 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Sun, 20 Jul 2025 17:34:36 +0100 Subject: [PATCH] deleted submit plan --- .../[slug]/components/SubmitPlan.tsx | 195 ------------------ .../[slug]/components/UploadCsvModal.tsx | 1 - 2 files changed, 196 deletions(-) delete mode 100644 src/app/portfolio/[slug]/components/SubmitPlan.tsx diff --git a/src/app/portfolio/[slug]/components/SubmitPlan.tsx b/src/app/portfolio/[slug]/components/SubmitPlan.tsx deleted file mode 100644 index 06b2339..0000000 --- a/src/app/portfolio/[slug]/components/SubmitPlan.tsx +++ /dev/null @@ -1,195 +0,0 @@ -"use client"; - -import { useRouter } from "next/navigation"; -import { useMutation } from "@tanstack/react-query"; -import { useSession } from "next-auth/react"; -import { useMemo } from "react"; - -function generateS3Key(userId: string, portfolioId: string, filename: string) { - const timestamp = new Date().toISOString().replace(/[:.-]/g, ""); - const key = `${userId}/${portfolioId}/${filename}-${timestamp}.csv`; - return key; -} - -interface UseCreatePlanProps { - portfolioId: string; - housingType: string; - goal: string; - goalValue: string; - file: File; -} - -interface UseCreatePlanReturn { - handlePlanBuild: () => void; - isGeneratingUrlLoading: boolean; - isUploadLoading: boolean; -} - -const useCreatePlan = ({ - portfolioId, - housingType, - goal, - goalValue, - file, -}: UseCreatePlanProps): UseCreatePlanReturn => { - const router = useRouter(); - const session = useSession(); - - // Since userId is a big int it gets coerced into a string - const userId = String(session.data?.user.dbId); - - // 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: () => { - const body = JSON.stringify({ - portfolio_id: portfolioId, - housing_type: housingType, - goal: goal, - goal_value: goalValue, - trigger_file_path: fileKey, - }); - - const response = fetch(`/api/plan/trigger`, { - method: "POST", - body: body, - }); - - return response; - }, - onError: (error) => { - console.error(error); - }, - } - ); - - const { mutate, isLoading: isGeneratingUrlLoading } = useMutation( - generatePresignedUrl, - { - onSuccess: (data) => { - try { - const response = mutateUploadCsv({ - presignedUrl: data.url, - file: file, - }); - - return response; - } catch (error) { - console.error(error); - } - }, - onError: (error) => { - console.error(error); - }, - } - ); - - const handlePlanBuild = () => { - mutate({ userId: userId, portfolioId: portfolioId, fileKey: fileKey }); - router.push(`/portfolio/${portfolioId}/plan-loading`); - }; - - return { - handlePlanBuild, - isGeneratingUrlLoading, - isUploadLoading, - }; -}; - -async function generatePresignedUrl({ - userId, - portfolioId, - fileKey, -}: { - userId: string; - portfolioId: string; - fileKey: string; -}) { - const body = JSON.stringify({ - userId: userId, - portfolioId: portfolioId, - fileKey: fileKey, - }); - - const presignedResponse = await fetch(`/api/upload/csv`, { - method: "POST", - body: body, - }); - if (!presignedResponse.ok) { - throw new Error("Network response was not ok"); - } - const presignedUrl = await presignedResponse.json(); - return presignedUrl; -} - -async function uploadCsvToS3({ - presignedUrl, - file, -}: { - presignedUrl: string; - file: File; -}) { - try { - const response = await fetch(presignedUrl, { - method: "PUT", - body: file, - headers: { "Content-Type": "text/csv" }, - }); - - if (!response.ok) { - console.error(response); - throw new Error("Network response was not ok"); - } - } catch (error) { - console.error(error); - throw new Error("Upload failed."); - } - - return { success: true }; -} - -export const SubmitPlan = ({ - buttonDisabled, - goal, - housingType, - goalValue, - file, - portfolioId, -}: { - buttonDisabled: boolean; - goal: string; - housingType: string; - goalValue: string; - file: File; - portfolioId: string; -}) => { - const { handlePlanBuild, isGeneratingUrlLoading, isUploadLoading } = - useCreatePlan({ portfolioId, housingType, goal, goalValue, file }); - - let buttonText; - if (isUploadLoading) { - buttonText = "Uploading file..."; - } else if (isGeneratingUrlLoading) { - buttonText = "Creating plan..."; - } else { - buttonText = "Create"; - } - - return ( - - ); -}; diff --git a/src/app/portfolio/[slug]/components/UploadCsvModal.tsx b/src/app/portfolio/[slug]/components/UploadCsvModal.tsx index c52d282..00c1b01 100644 --- a/src/app/portfolio/[slug]/components/UploadCsvModal.tsx +++ b/src/app/portfolio/[slug]/components/UploadCsvModal.tsx @@ -4,7 +4,6 @@ import { Dialog, Transition } from "@headlessui/react"; import { Fragment, useMemo, useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { InputFile } from "@/app/portfolio/[slug]/components/InputFile"; -import { SubmitPlan } from "@/app/portfolio/[slug]/components/SubmitPlan"; import { ScenarioSelect } from "@/app/db/schema/recommendations"; import MeasuresCheckboxes from "./MeasuresCheckboxes"; import { useForm, FormProvider } from "react-hook-form";