From c6e4951f976e51d428233a65e01e840a805ce589 Mon Sep 17 00:00:00 2001 From: Maximilian Harz Date: Mon, 9 Jun 2025 12:54:15 +0200 Subject: [PATCH] Lift state to fix setState error --- src/Components/Profile/ProfileForm.tsx | 10 +++++---- .../Profile/Subcomponents/GalleryForm.tsx | 21 +++---------------- src/types/FormState.d.ts | 2 +- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/Components/Profile/ProfileForm.tsx b/src/Components/Profile/ProfileForm.tsx index a898f06e..62a3cc8d 100644 --- a/src/Components/Profile/ProfileForm.tsx +++ b/src/Components/Profile/ProfileForm.tsx @@ -48,7 +48,7 @@ export function ProfileForm() { end: '', openCollectiveSlug: '', gallery: [], - isUpdatingGallery: false, + uploadingImages: [], }) const [updatePermission, setUpdatePermission] = useState(false) @@ -143,7 +143,7 @@ export function ProfileForm() { end: item.end ?? '', openCollectiveSlug: item.openCollectiveSlug ?? '', gallery: item.gallery ?? [], - isUpdatingGallery: false, + uploadingImages: [], }) // eslint-disable-next-line react-hooks/exhaustive-deps }, [item, tags, items]) @@ -158,6 +158,8 @@ export function ProfileForm() { } }, [item, layers]) + const isUpdatingGallery = state.uploadingImages.length > 0 + return ( <> Update diff --git a/src/Components/Profile/Subcomponents/GalleryForm.tsx b/src/Components/Profile/Subcomponents/GalleryForm.tsx index 07c3cd0f..a677b60a 100644 --- a/src/Components/Profile/Subcomponents/GalleryForm.tsx +++ b/src/Components/Profile/Subcomponents/GalleryForm.tsx @@ -24,17 +24,14 @@ const compressionOptions = { export const GalleryForm = ({ state, setState }: Props) => { const appState = useAppState() - const [uploadingImages, setUploadingImages] = useState([]) - const [imageSelectedToDelete, setImageSelectedToDelete] = useState(null) const closeModal = () => setImageSelectedToDelete(null) const upload = async (acceptedFiles: File[]) => { - setUploadingImages((files) => [...files, ...acceptedFiles]) setState((prevState) => ({ ...prevState, - isUpdatingGallery: true, + uploadingImages: [...prevState.uploadingImages, ...acceptedFiles], })) const uploads = acceptedFiles.map(async (file) => { @@ -51,6 +48,7 @@ export const GalleryForm = ({ state, setState }: Props) => { for await (const upload of uploads) { setState((prevState) => ({ ...prevState, + uploadingImages: prevState.uploadingImages.filter((f) => f.name !== upload.name), gallery: [ ...prevState.gallery, { @@ -62,19 +60,6 @@ export const GalleryForm = ({ state, setState }: Props) => { }, ], })) - - setUploadingImages((files) => { - const newFiles = files.filter((f) => f.name !== upload.name) - - if (newFiles.length === 0) { - setState((prevState) => ({ - ...prevState, - isUpdatingGallery: false, - })) - } - - return newFiles - }) } } @@ -92,7 +77,7 @@ export const GalleryForm = ({ state, setState }: Props) => { state: 'uploaded', })) .concat( - uploadingImages.map((file) => ({ + state.uploadingImages.map((file) => ({ src: URL.createObjectURL(file), state: 'uploading', })), diff --git a/src/types/FormState.d.ts b/src/types/FormState.d.ts index f69b19ef..7322b58d 100644 --- a/src/types/FormState.d.ts +++ b/src/types/FormState.d.ts @@ -22,5 +22,5 @@ export interface FormState { end: string openCollectiveSlug: string gallery: GalleryItem[] - isUpdatingGallery: boolean + uploadingImages: File[] }