From fca4474efc8ab2665d830b1c85d96f62381f170b Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 26 Sep 2023 08:25:45 +0100 Subject: [PATCH] Implemented the due considerations process --- .../api/upload/due-considerations/route.ts | 2 + src/app/due-considerations/page.tsx | 61 ++++++++++++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/app/api/upload/due-considerations/route.ts b/src/app/api/upload/due-considerations/route.ts index 46de42f..4de0595 100644 --- a/src/app/api/upload/due-considerations/route.ts +++ b/src/app/api/upload/due-considerations/route.ts @@ -24,6 +24,8 @@ export async function POST(request: NextRequest) { }); } + console.log("UPLOAD BODY", validatedBody); + try { const s3 = new S3({ signatureVersion: "v4", diff --git a/src/app/due-considerations/page.tsx b/src/app/due-considerations/page.tsx index 8a31150..148550d 100644 --- a/src/app/due-considerations/page.tsx +++ b/src/app/due-considerations/page.tsx @@ -13,19 +13,58 @@ function generateDueConsiderationsS3Folder(userId: string) { return key; } +async function postDueConsiderations( + userId: string, + folderKey: string, + schemeName: string +) { + // Triggers the due considerations process + const body = JSON.stringify({ + userId: userId, + folderKey: folderKey, + scheme: schemeName, + }); + + try { + const response = await fetch(`/api/due-considerations`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: body, + }); + + if (!response.ok) { + throw new Error("Network response was not ok"); + } + + // Handle the response as needed + const data = await response.json(); + console.log("Response Data:", data); + } catch (error) { + console.error(error); + // Handle the error appropriately + } +} + const useUploadFiles = ({ dueConsiderationsFiles, - folderKey, + folderKey: initialFolderKey, + userId, + schemeName, }: { dueConsiderationsFiles: File[]; folderKey: string; + userId: string; + schemeName: string; }) => { const { mutate: mutateUploadFiles, isLoading: isUploadLoading } = useMutation( uploadFilesToS3, { onSuccess: () => { console.log("Trigger the due considerations process"); - // You can replace the above log with the actual API call when it's implemented + console.log("Folder key: ", folderKey); + postDueConsiderations(userId, folderKey, schemeName); }, onError: (error) => { console.error(error); @@ -53,9 +92,11 @@ const useUploadFiles = ({ } ); - const handleUpload = () => { - mutate({ folderKey: folderKey, files: dueConsiderationsFiles }); - // Navigate to a loading or success page if needed + const [folderKey, setFolderKey] = useState(initialFolderKey); + + const handleUpload = (newFolderKey: string) => { + setFolderKey(newFolderKey); + mutate({ folderKey: newFolderKey, files: dueConsiderationsFiles }); }; return { @@ -117,6 +158,7 @@ export default function DueConsiderationsHome() { ); const [buttonDisabled, setButtonDisabled] = useState(true); const [uploadMessage, setUploadMessage] = useState(""); + const [schemeName, setSchemeName] = useState(""); const session = useSession(); const userId = String(session.data?.user.dbId); @@ -126,7 +168,12 @@ export default function DueConsiderationsHome() { ); const { handleUpload, isGeneratingUrlLoading, isUploadLoading } = - useUploadFiles({ dueConsiderationsFiles, folderKey }); + useUploadFiles({ dueConsiderationsFiles, folderKey, userId, schemeName }); + + const initiateUpload = () => { + const newFolderKey = generateDueConsiderationsS3Folder(userId); + handleUpload(newFolderKey); + }; function handleOnChange(e: React.ChangeEvent) { if (e.target.files && e.target.files.length === 3) { @@ -175,7 +222,7 @@ export default function DueConsiderationsHome() {