From 3520d0858dba7ff28841d3e8745d878f48b230c4 Mon Sep 17 00:00:00 2001 From: StefanWout Date: Thu, 21 Nov 2024 14:35:54 +0000 Subject: [PATCH] succesfully implemented the valuation data upload functionality on handleSubmit now to turn this into a form --- .../components/RemoteAssessmentModal.tsx | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx index 38ea37a..15d7810 100644 --- a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx +++ b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx @@ -136,6 +136,7 @@ async function uploadCsvToS3({ throw new Error("Upload failed."); } + console.log("S3 got the stuff"); return { success: true }; } @@ -192,11 +193,13 @@ function useCreateRemoteAssessment({ uprn, addressLineOne, postcode, + valuation, }: { portfolioId: string; uprn: number | null; addressLineOne: string; postcode: string; + valuation: string | number | null; }) { // 1) We want to upload the asset data. To do this, we format the asset data, generate a presigned URL, and upload the data to S3. // 2) We then want to upload valuation data. To do this, we format the valuation data, generate a presigned URL, and upload the data to S3. @@ -218,8 +221,8 @@ function useCreateRemoteAssessment({ isError: uploadAssetListIsError, } = useMutation(uploadCsvToS3, { onSuccess: (data) => { - console.log("WAS IT A SUCCESS?", data.success); - console.log("TRIGGERING THE ENGINE"); + console.log("WAS THE ASSET LIST A SUCCESS?", data.success); + console.log("ASSETS TRIGGERING THE ENGINE"); // This is where we trigger the engine!!! const body = { trigger_file_path: assetListFileKey, @@ -231,6 +234,25 @@ function useCreateRemoteAssessment({ }, }); + const { + mutate: mutateUploadValuationData, + isLoading: uploadValuationDataIsLoading, + isError: uploadValuationDataIsError, + } = useMutation(uploadCsvToS3, { + onSuccess: (data) => { + console.log("WAS VALUATION DATA A SUCCESS?", data.success); + console.log("VALUATION TRIGGERING THE ENGINE"); + // This is where we trigger the engine!!! + const body = { + trigger_file_path: valuationDataFileKey, + }; + // engine API call goes here + }, + onError: (error) => { + console.error(error); + }, + }); + const { mutate: mutatePresignedUrl, isLoading: presignedUrlIsLoading, @@ -246,21 +268,45 @@ function useCreateRemoteAssessment({ postcode: postcode, }, ]; + const valuationData = [ + { + uprn: uprn, + valuation: 100000, + }, + ]; const assetListCsvString = convertToCSV(assetList); const assetListCsv = new Blob([assetListCsvString], { type: "text/csv", }); - mutateUploadAssetList({ presignedUrl: data.url, file: assetListCsv }); + + const valuationDataCsvString = convertToCSV(valuationData); + const valuationDataCsv = new Blob([valuationDataCsvString], { + type: "text/csv", + }); + mutateUploadValuationData({ presignedUrl: data.url, file: valuationDataCsv }); }, onError: (error) => { console.error(error); }, }); - function handleSubmit() { - mutatePresignedUrl({ userId, portfolioId, fileKey: assetListFileKey }); - console.log("SUCCESS"); // This is where we would want to trigger some kind of use feedback + async function handleSubmit() { + try { + // Mutate presigned URL for asset list file + await mutatePresignedUrl({ userId, portfolioId, fileKey: assetListFileKey }); + console.log("ASSET LIST SUCCESS"); + + // Mutate presigned URL for valuation data file + await mutatePresignedUrl({ userId, portfolioId, fileKey: valuationDataFileKey }); + console.log("VALUATION DATA SUCCESS"); + + // Trigger user feedback (e.g., show a success message) + console.log("Both uploads SUCCESS"); + } catch (error) { + // Handle error (e.g., show an error message) + console.error("Error uploading files:", error); + } } return { @@ -317,6 +363,7 @@ export default function RemoteAssessmentModal({ uprn, addressLineOne, postcode, + valuation, }); useEffect(() => {