got csv upload working

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-14 15:28:33 +01:00
parent 9be55a54bf
commit 6a3ee8dee4
2 changed files with 21 additions and 11 deletions

View file

@ -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,
});
}

View file

@ -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}
/>
</div>
</Dialog.Panel>