From 54abd73d42b15492f66bf3799a0b32de6f09922c Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 18 Oct 2024 16:40:50 +0200 Subject: [PATCH] refactor: use react hook form in edit file form --- src/components/item/edit/EditModal.tsx | 8 +- src/components/item/form/FileForm.tsx | 122 +++++++++++++++--------- src/components/item/form/FolderForm.tsx | 2 +- 3 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/components/item/edit/EditModal.tsx b/src/components/item/edit/EditModal.tsx index 07e707c02..a69cf73bf 100644 --- a/src/components/item/edit/EditModal.tsx +++ b/src/components/item/edit/EditModal.tsx @@ -74,13 +74,7 @@ const EditModal = ({ item, onClose, open }: Props): JSX.Element => { return ; case ItemType.LOCAL_FILE: case ItemType.S3_FILE: - return ( - - ); + return ; case ItemType.SHORTCUT: return ( { - const { item, setChanges, updatedProperties } = props; +type Inputs = { + name: string; + altText: string; + description: string; + descriptionPlacement: DescriptionPlacementType; +}; +const FileForm = ({ + item, + setChanges, +}: { + item: DiscriminatedItem; + setChanges: EditModalContentPropType['setChanges']; +}): JSX.Element | null => { const { t: translateBuilder } = useBuilderTranslation(); + const { register, watch, setValue } = useForm(); + const altText = watch('altText'); + const description = watch('description'); + const descriptionPlacement = watch('descriptionPlacement'); + const name = watch('name'); - if (!item) { - return null; - } + useEffect(() => { + let newExtra: S3FileItemExtra | LocalFileItemExtra | undefined; + + if (item.type === ItemType.S3_FILE) { + newExtra = { + [ItemType.S3_FILE]: { + ...item.extra[ItemType.S3_FILE], + altText, + }, + }; + } else if (item.type === ItemType.LOCAL_FILE) { + newExtra = { + [ItemType.LOCAL_FILE]: { + ...item.extra[ItemType.LOCAL_FILE], + altText, + }, + }; + } + + setChanges({ + name, + description, + settings: { descriptionPlacement }, + extra: newExtra, + } as S3FileItemType | LocalFileItemType); + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [altText, description, descriptionPlacement, name, setChanges]); - if ( - item && - (item.type === ItemType.LOCAL_FILE || item.type === ItemType.S3_FILE) && - (updatedProperties.type === ItemType.LOCAL_FILE || - updatedProperties.type === ItemType.S3_FILE) - ) { + if (item) { const itemExtra = getExtraFromPartial(item); - const { mimetype, altText } = itemExtra; - const updatedExtra = updatedProperties.extra - ? getExtraFromPartial(updatedProperties) - : {}; + const { mimetype, altText: previousAltText } = itemExtra; return ( <> - + {mimetype && MimeTypes.isImage(mimetype) && ( { - const newExtra = { - ...itemExtra, - ...updatedExtra, - altText: e.target.value, - } as FileItemProperties; - - setChanges( - item.type === ItemType.S3_FILE - ? { - extra: { - [ItemType.S3_FILE]: newExtra, - }, - } - : { - extra: { [ItemType.LOCAL_FILE]: newExtra }, - }, - ); - }} // always shrink because setting name from defined app does not shrink automatically InputLabelProps={{ shrink: true }} sx={{ width: '50%', my: 1 }} multiline + {...register('altText', { value: previousAltText })} /> )} { + if (v.description) { + setValue('description', v.description); + } + if (v.settings?.descriptionPlacement) { + setValue( + 'descriptionPlacement', + v.settings?.descriptionPlacement, + ); + } + }} + description={description ?? item?.description ?? ''} descriptionPlacement={ - updatedProperties?.settings?.descriptionPlacement ?? - item?.settings?.descriptionPlacement + descriptionPlacement ?? item?.settings?.descriptionPlacement } /> diff --git a/src/components/item/form/FolderForm.tsx b/src/components/item/form/FolderForm.tsx index 9d992456e..12b7bfb74 100644 --- a/src/components/item/form/FolderForm.tsx +++ b/src/components/item/form/FolderForm.tsx @@ -63,7 +63,7 @@ const FolderForm = ({ setValue('description', v.description); } if (v.settings?.descriptionPlacement) { - setValue('description', v.settings?.descriptionPlacement); + setValue('descriptionPlacement', v.settings?.descriptionPlacement); } }} showPlacement={false}