From 6a68300ceacbaa3c204130d3a539c81ad79ddabc Mon Sep 17 00:00:00 2001 From: Maximilian Harz Date: Wed, 4 Jun 2025 23:44:50 +0200 Subject: [PATCH] Add GalleryForm (WIP) --- .../Profile/Subcomponents/GalleryForm.tsx | 82 +++++++++++++++++++ src/Components/Profile/Templates/FlexForm.tsx | 5 +- src/types/Item.d.ts | 2 +- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/Components/Profile/Subcomponents/GalleryForm.tsx diff --git a/src/Components/Profile/Subcomponents/GalleryForm.tsx b/src/Components/Profile/Subcomponents/GalleryForm.tsx new file mode 100644 index 00000000..76659a38 --- /dev/null +++ b/src/Components/Profile/Subcomponents/GalleryForm.tsx @@ -0,0 +1,82 @@ +import { useDropzone } from 'react-dropzone' + +import { useAppState } from '#components/AppShell/hooks/useAppState' + +import type { FormState } from '#types/FormState' + +interface Props { + state: FormState + setState: React.Dispatch> +} + +export const GalleryForm = ({ state, setState }: Props) => { + const appState = useAppState() + + const upload = async (acceptedFiles: File[]) => { + const assets = await Promise.all( + acceptedFiles.map(async (file) => { + return appState.assetsApi.upload(file, 'gallery') + }), + ) + + const newGalleryItems = assets.map((asset) => ({ + directus_files_id: { + id: asset.id, + width: 600, // TODO determine + height: 400, // TODO determine + }, + })) + + setState((prevState) => ({ + ...prevState, + gallery: [...prevState.gallery, ...newGalleryItems], + })) + } + + const { getRootProps, getInputProps } = useDropzone({ + // eslint-disable-next-line @typescript-eslint/no-misused-promises + onDrop: upload, + }) + + const images = state.gallery.map((i, j) => ({ + src: appState.assetsApi.url + `${i.directus_files_id.id}.jpg`, + width: i.directus_files_id.width, + height: i.directus_files_id.height, + index: j, + })) + + const removeImage = (index: number) => { + setState((prevState) => ({ + ...prevState, + gallery: prevState.gallery.filter((_, i) => i !== index), + })) + } + + return ( +
+
+ + Drop here +
+ + {images.map((image, index) => ( +
+ {`Gallery + +
+ ))} +
+ ) +} diff --git a/src/Components/Profile/Templates/FlexForm.tsx b/src/Components/Profile/Templates/FlexForm.tsx index c85d3d41..1bc42a6e 100644 --- a/src/Components/Profile/Templates/FlexForm.tsx +++ b/src/Components/Profile/Templates/FlexForm.tsx @@ -1,8 +1,8 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { ContactInfoForm } from '#components/Profile/Subcomponents/ContactInfoForm' import { CrowdfundingForm } from '#components/Profile/Subcomponents/CrowdfundingForm' +import { GalleryForm } from '#components/Profile/Subcomponents/GalleryForm' import { GroupSubheaderForm } from '#components/Profile/Subcomponents/GroupSubheaderForm' import { ProfileStartEndForm } from '#components/Profile/Subcomponents/ProfileStartEndForm' import { ProfileTextForm } from '#components/Profile/Subcomponents/ProfileTextForm' @@ -16,6 +16,7 @@ const componentMap = { contactInfos: ContactInfoForm, startEnd: ProfileStartEndForm, crowdfundings: CrowdfundingForm, + gallery: GalleryForm, // weitere Komponenten hier } @@ -25,7 +26,7 @@ export const FlexForm = ({ setState, }: { state: FormState - setState: React.Dispatch> + setState: React.Dispatch> item: Item }) => { return ( diff --git a/src/types/Item.d.ts b/src/types/Item.d.ts index d1073884..54f3b00b 100644 --- a/src/types/Item.d.ts +++ b/src/types/Item.d.ts @@ -9,7 +9,7 @@ type TagIds = { tags_id: string }[] interface GalleryItem { directus_files_id: { - id: number + id: string width: number height: number }