From 6a3ee8dee4ad241913afa088191bb79d4150f8ab Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 14 Jul 2023 15:28:33 +0100 Subject: [PATCH] got csv upload working --- src/app/api/upload/csv/route.ts | 2 +- .../components/portfolio/UploadCsvModal.tsx | 30 ++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/app/api/upload/csv/route.ts b/src/app/api/upload/csv/route.ts index faf865d..7ff4ff4 100644 --- a/src/app/api/upload/csv/route.ts +++ b/src/app/api/upload/csv/route.ts @@ -56,7 +56,7 @@ export async function POST(request: NextRequest) { }); } catch (error) { console.error(error); - return new NextResponse(JSON.stringify({ msg: "Insternal server error" }), { + return new NextResponse(JSON.stringify({ msg: "Internal server error" }), { status: 500, }); } diff --git a/src/app/components/portfolio/UploadCsvModal.tsx b/src/app/components/portfolio/UploadCsvModal.tsx index cb41cf2..9000ff5 100644 --- a/src/app/components/portfolio/UploadCsvModal.tsx +++ b/src/app/components/portfolio/UploadCsvModal.tsx @@ -19,7 +19,6 @@ async function generatePresignedUrl({ portfolioId: number; }) { const body = JSON.stringify({ userId: userId, portfolioId: portfolioId }); - console.log("body before: ", body); const presignedResponse = await fetch(`/api/upload/csv`, { method: "POST", @@ -69,14 +68,22 @@ export const SubmitPlan = ({ }) => { const router = useRouter(); const session = useSession(); - // This state will hold the presignedUrl once it is available - const [presignedUrl, setPresignedUrl] = useState(null); const { mutate: mutateUploadCsv, isLoading: isUploadLoading } = useMutation( uploadCsvToS3, { onSuccess: (data) => { - return data; + // After the file has been uploaded, we can trigger the job to build the plan + // const response = fetch(`/api/plan/build`, { + // method: "POST", + // body: JSON.stringify({ + // portfolioId: portfolioId, + // housingType: housingType, + // goal: goal, + // goalValue: goalValue, + // }), + // }); + // return response; }, onError: (error) => { // handle error @@ -87,9 +94,9 @@ export const SubmitPlan = ({ const { mutate, isLoading } = useMutation(generatePresignedUrl, { onSuccess: (data) => { - console.log("Got url: ", data.url); // After the presigned URL has been generated, we can upload the file to S3 - mutateUploadCsv({ presignedUrl: data.url, file: file }); + const response = mutateUploadCsv({ presignedUrl: data.url, file: file }); + return response; }, onError: (error) => { // handle error @@ -112,10 +119,13 @@ export const SubmitPlan = ({ // 4) Redirect the user to some loading page - this could be the portfolio page itself and we just trigger a regresh with skeleton cards for the properties mutate({ userId: userId, portfolioId: portfolioId }); - // mutateUploadCsv(presignedUrl); + // TODO: Make api call to backend service to trigger the plan build + // Probably need to pass in the file key to mutate (define it outside) + // because of the async + // We could also trigger it inside of mutateUploadCsv but the nested nature is kind of ugly - // console.log("Redirect user to loading page"); - // router.push("/portfolio/somewhere"); + console.log("Redirect user to loading page"); + router.push("/portfolio/somewhere"); }; return ( @@ -503,7 +513,7 @@ export default function UploadCsvModal({ housingType={housingType} goalValue={goalValue} portfolioId={portfolioId} - file={csvFile} + file={csvFile as File} />