From 35bdebb4496e4102542c739bfd41f5ae12079c2c Mon Sep 17 00:00:00 2001 From: Brandon Egger Date: Sun, 25 Jun 2023 20:56:02 -0500 Subject: [PATCH] integrate file upload into save routine --- src/components/admin/resources/form.tsx | 2 +- src/pages/resources/[id]/edit.tsx | 33 +++++++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/components/admin/resources/form.tsx b/src/components/admin/resources/form.tsx index c32b3b0..8dc74d7 100644 --- a/src/components/admin/resources/form.tsx +++ b/src/components/admin/resources/form.tsx @@ -486,7 +486,7 @@ const ResourceForm = ({ ) : undefined}
- {/** //resource={resource} /> */} +

diff --git a/src/pages/resources/[id]/edit.tsx b/src/pages/resources/[id]/edit.tsx index ab1855b..3ee4ea7 100644 --- a/src/pages/resources/[id]/edit.tsx +++ b/src/pages/resources/[id]/edit.tsx @@ -59,19 +59,32 @@ const EditResourcePage = ( onError: (error) => setServerError(error.message), }); - const onSubmit: SubmitHandler = (data) => { + const onSubmit: SubmitHandler = async (data) => { + // TODO: Fix file upload, currently it is not updating correctly on the server side + // May also need to look into re-rendering static pages when icon changes + // Also need to add authentication of route! if (updateIconFile) { - console.log("we need to update the file!"); + const data = new FormData(); + data.append("photo", updateIconFile); + + const uploadResponse = await fetch( + `/api/resources/photo/${resource.id}`, + { + method: "POST", + body: data, + } + ); + + console.log("uploading icon"); + + if (uploadResponse.status !== 200) { + setServerError( + "Failed uploading resource icon file. Changes did not save!" + ); + throw new Error(JSON.stringify(uploadResponse)); + } } - // const data = new FormData(); - // data.append("photo", event.target.files[0]); - - // await fetch(`/api/resources/photo/${resourceId}`, { - // method: "POST", - // body: data, - // }); - mutate(data); };