Implemented the due considerations process

This commit is contained in:
Khalim Conn-Kowlessar 2023-09-26 08:25:45 +01:00
parent 3a4aa6ce49
commit fca4474efc
2 changed files with 56 additions and 7 deletions

View file

@ -24,6 +24,8 @@ export async function POST(request: NextRequest) {
});
}
console.log("UPLOAD BODY", validatedBody);
try {
const s3 = new S3({
signatureVersion: "v4",

View file

@ -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<HTMLInputElement>) {
if (e.target.files && e.target.files.length === 3) {
@ -175,7 +222,7 @@ export default function DueConsiderationsHome() {
<Button
disabled={buttonDisabled}
className="bg-brandblue hover:bg-hoverblue"
onClick={handleUpload}
onClick={initiateUpload}
>
Upload
</Button>