diff --git a/app/frontend/src/javascript/components/form/form-file-upload.tsx b/app/frontend/src/javascript/components/form/form-file-upload.tsx index 4fbe89f27..f0ed2859a 100644 --- a/app/frontend/src/javascript/components/form/form-file-upload.tsx +++ b/app/frontend/src/javascript/components/form/form-file-upload.tsx @@ -4,17 +4,13 @@ import { Path } from 'react-hook-form'; import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form'; import { FieldPathValue } from 'react-hook-form/dist/types/path'; import { FieldValues } from 'react-hook-form/dist/types/fields'; -import { FormInput } from '../form/form-input'; +import { FormInput } from './form-input'; import { FormComponent } from '../../models/form-component'; import { AbstractFormItemProps } from './abstract-form-item'; import { FabButton } from '../base/fab-button'; import { FilePdf, Trash } from 'phosphor-react'; - -export interface FileType { - id?: number, - attachment_name?: string, - attachment_url?: string -} +import { FileType } from '../../models/file'; +import FileUploadLib from '../../lib/file-upload'; interface FormFileUploadProps extends FormComponent, AbstractFormItemProps { setValue: UseFormSetValue, @@ -36,7 +32,7 @@ export const FormFileUpload = ({ id, register, * Check if file is selected */ const hasFile = (): boolean => { - return !!file?.attachment_name; + return FileUploadLib.hasFile(file); }; /** @@ -62,20 +58,7 @@ export const FormFileUpload = ({ id, register, * Callback triggered when the user clicks on the delete button. */ function onRemoveFile () { - if (file?.id) { - setValue( - `${id}[_destroy]` as Path, - true as UnpackNestedValue>> - ); - } - setValue( - `${id}[attachment_files]` as Path, - null as UnpackNestedValue>> - ); - setFile(null); - if (typeof onFileRemove === 'function') { - onFileRemove(); - } + FileUploadLib.onRemoveFile(file, id, setFile, setValue, onFileRemove); } // Compose classnames from props diff --git a/app/frontend/src/javascript/components/form/form-image-upload.tsx b/app/frontend/src/javascript/components/form/form-image-upload.tsx index dd3d5435d..1b73abf68 100644 --- a/app/frontend/src/javascript/components/form/form-image-upload.tsx +++ b/app/frontend/src/javascript/components/form/form-image-upload.tsx @@ -4,19 +4,14 @@ import { Path } from 'react-hook-form'; import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form'; import { FieldPathValue } from 'react-hook-form/dist/types/path'; import { FieldValues } from 'react-hook-form/dist/types/fields'; -import { FormInput } from '../form/form-input'; +import { FormInput } from './form-input'; import { FormComponent } from '../../models/form-component'; import { AbstractFormItemProps } from './abstract-form-item'; import { FabButton } from '../base/fab-button'; import noImage from '../../../../images/no_image.png'; import { Trash } from 'phosphor-react'; - -export interface ImageType { - id?: number, - attachment_name?: string, - attachment_url?: string, - is_main?: boolean -} +import { ImageType } from '../../models/file'; +import FileUploadLib from '../../lib/file-upload'; interface FormImageUploadProps extends FormComponent, AbstractFormItemProps { setValue: UseFormSetValue, @@ -46,7 +41,7 @@ export const FormImageUpload = ({ id, register * Check if image is selected */ const hasImage = (): boolean => { - return !!file?.attachment_name; + return FileUploadLib.hasFile(file); }; /** @@ -82,20 +77,7 @@ export const FormImageUpload = ({ id, register * Callback triggered when the user clicks on the delete button. */ function onRemoveFile () { - if (file?.id) { - setValue( - `${id}[_destroy]` as Path, - true as UnpackNestedValue>> - ); - } - setValue( - `${id}[attachment_files]` as Path, - null as UnpackNestedValue>> - ); - setFile(null); - if (typeof onFileRemove === 'function') { - onFileRemove(); - } + FileUploadLib.onRemoveFile(file, id, setFile, setValue, onFileRemove); } /** diff --git a/app/frontend/src/javascript/lib/file-upload.ts b/app/frontend/src/javascript/lib/file-upload.ts new file mode 100644 index 000000000..237763545 --- /dev/null +++ b/app/frontend/src/javascript/lib/file-upload.ts @@ -0,0 +1,29 @@ +import { Path } from 'react-hook-form'; +import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form'; +import { FieldPathValue } from 'react-hook-form/dist/types/path'; +import { FieldValues } from 'react-hook-form/dist/types/fields'; +import { FileType } from '../models/file'; +import { Dispatch, SetStateAction } from 'react'; + +export default class FileUploadLib { + public static onRemoveFile (file: FileType, id: string, setFile: Dispatch>, setValue: UseFormSetValue, onFileRemove: () => void) { + if (file?.id) { + setValue( + `${id}[_destroy]` as Path, + true as UnpackNestedValue>> + ); + } + setValue( + `${id}[attachment_files]` as Path, + null as UnpackNestedValue>> + ); + setFile(null); + if (typeof onFileRemove === 'function') { + onFileRemove(); + } + } + + public static hasFile (file: FileType): boolean { + return !!file?.attachment_name; + } +} diff --git a/app/frontend/src/javascript/models/file.ts b/app/frontend/src/javascript/models/file.ts new file mode 100644 index 000000000..c05e64787 --- /dev/null +++ b/app/frontend/src/javascript/models/file.ts @@ -0,0 +1,9 @@ +export interface FileType { + id?: number, + attachment_name?: string, + attachment_url?: string +} + +export interface ImageType extends FileType { + is_main?: boolean +}