mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-07 01:54:16 +01:00
(quality) refactored upload components
This commit is contained in:
parent
74f826eef7
commit
a97e08b43b
@ -4,17 +4,13 @@ import { Path } from 'react-hook-form';
|
|||||||
import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form';
|
import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form';
|
||||||
import { FieldPathValue } from 'react-hook-form/dist/types/path';
|
import { FieldPathValue } from 'react-hook-form/dist/types/path';
|
||||||
import { FieldValues } from 'react-hook-form/dist/types/fields';
|
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 { FormComponent } from '../../models/form-component';
|
||||||
import { AbstractFormItemProps } from './abstract-form-item';
|
import { AbstractFormItemProps } from './abstract-form-item';
|
||||||
import { FabButton } from '../base/fab-button';
|
import { FabButton } from '../base/fab-button';
|
||||||
import { FilePdf, Trash } from 'phosphor-react';
|
import { FilePdf, Trash } from 'phosphor-react';
|
||||||
|
import { FileType } from '../../models/file';
|
||||||
export interface FileType {
|
import FileUploadLib from '../../lib/file-upload';
|
||||||
id?: number,
|
|
||||||
attachment_name?: string,
|
|
||||||
attachment_url?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface FormFileUploadProps<TFieldValues> extends FormComponent<TFieldValues>, AbstractFormItemProps<TFieldValues> {
|
interface FormFileUploadProps<TFieldValues> extends FormComponent<TFieldValues>, AbstractFormItemProps<TFieldValues> {
|
||||||
setValue: UseFormSetValue<TFieldValues>,
|
setValue: UseFormSetValue<TFieldValues>,
|
||||||
@ -36,7 +32,7 @@ export const FormFileUpload = <TFieldValues extends FieldValues>({ id, register,
|
|||||||
* Check if file is selected
|
* Check if file is selected
|
||||||
*/
|
*/
|
||||||
const hasFile = (): boolean => {
|
const hasFile = (): boolean => {
|
||||||
return !!file?.attachment_name;
|
return FileUploadLib.hasFile(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,20 +58,7 @@ export const FormFileUpload = <TFieldValues extends FieldValues>({ id, register,
|
|||||||
* Callback triggered when the user clicks on the delete button.
|
* Callback triggered when the user clicks on the delete button.
|
||||||
*/
|
*/
|
||||||
function onRemoveFile () {
|
function onRemoveFile () {
|
||||||
if (file?.id) {
|
FileUploadLib.onRemoveFile(file, id, setFile, setValue, onFileRemove);
|
||||||
setValue(
|
|
||||||
`${id}[_destroy]` as Path<TFieldValues>,
|
|
||||||
true as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
setValue(
|
|
||||||
`${id}[attachment_files]` as Path<TFieldValues>,
|
|
||||||
null as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>
|
|
||||||
);
|
|
||||||
setFile(null);
|
|
||||||
if (typeof onFileRemove === 'function') {
|
|
||||||
onFileRemove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compose classnames from props
|
// Compose classnames from props
|
||||||
|
@ -4,19 +4,14 @@ import { Path } from 'react-hook-form';
|
|||||||
import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form';
|
import { UnpackNestedValue, UseFormSetValue } from 'react-hook-form/dist/types/form';
|
||||||
import { FieldPathValue } from 'react-hook-form/dist/types/path';
|
import { FieldPathValue } from 'react-hook-form/dist/types/path';
|
||||||
import { FieldValues } from 'react-hook-form/dist/types/fields';
|
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 { FormComponent } from '../../models/form-component';
|
||||||
import { AbstractFormItemProps } from './abstract-form-item';
|
import { AbstractFormItemProps } from './abstract-form-item';
|
||||||
import { FabButton } from '../base/fab-button';
|
import { FabButton } from '../base/fab-button';
|
||||||
import noImage from '../../../../images/no_image.png';
|
import noImage from '../../../../images/no_image.png';
|
||||||
import { Trash } from 'phosphor-react';
|
import { Trash } from 'phosphor-react';
|
||||||
|
import { ImageType } from '../../models/file';
|
||||||
export interface ImageType {
|
import FileUploadLib from '../../lib/file-upload';
|
||||||
id?: number,
|
|
||||||
attachment_name?: string,
|
|
||||||
attachment_url?: string,
|
|
||||||
is_main?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
interface FormImageUploadProps<TFieldValues> extends FormComponent<TFieldValues>, AbstractFormItemProps<TFieldValues> {
|
interface FormImageUploadProps<TFieldValues> extends FormComponent<TFieldValues>, AbstractFormItemProps<TFieldValues> {
|
||||||
setValue: UseFormSetValue<TFieldValues>,
|
setValue: UseFormSetValue<TFieldValues>,
|
||||||
@ -46,7 +41,7 @@ export const FormImageUpload = <TFieldValues extends FieldValues>({ id, register
|
|||||||
* Check if image is selected
|
* Check if image is selected
|
||||||
*/
|
*/
|
||||||
const hasImage = (): boolean => {
|
const hasImage = (): boolean => {
|
||||||
return !!file?.attachment_name;
|
return FileUploadLib.hasFile(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -82,20 +77,7 @@ export const FormImageUpload = <TFieldValues extends FieldValues>({ id, register
|
|||||||
* Callback triggered when the user clicks on the delete button.
|
* Callback triggered when the user clicks on the delete button.
|
||||||
*/
|
*/
|
||||||
function onRemoveFile () {
|
function onRemoveFile () {
|
||||||
if (file?.id) {
|
FileUploadLib.onRemoveFile(file, id, setFile, setValue, onFileRemove);
|
||||||
setValue(
|
|
||||||
`${id}[_destroy]` as Path<TFieldValues>,
|
|
||||||
true as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
setValue(
|
|
||||||
`${id}[attachment_files]` as Path<TFieldValues>,
|
|
||||||
null as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>
|
|
||||||
);
|
|
||||||
setFile(null);
|
|
||||||
if (typeof onFileRemove === 'function') {
|
|
||||||
onFileRemove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
29
app/frontend/src/javascript/lib/file-upload.ts
Normal file
29
app/frontend/src/javascript/lib/file-upload.ts
Normal file
@ -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<TFieldValues extends FieldValues> (file: FileType, id: string, setFile: Dispatch<SetStateAction<FileType>>, setValue: UseFormSetValue<TFieldValues>, onFileRemove: () => void) {
|
||||||
|
if (file?.id) {
|
||||||
|
setValue(
|
||||||
|
`${id}[_destroy]` as Path<TFieldValues>,
|
||||||
|
true as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
setValue(
|
||||||
|
`${id}[attachment_files]` as Path<TFieldValues>,
|
||||||
|
null as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>
|
||||||
|
);
|
||||||
|
setFile(null);
|
||||||
|
if (typeof onFileRemove === 'function') {
|
||||||
|
onFileRemove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static hasFile (file: FileType): boolean {
|
||||||
|
return !!file?.attachment_name;
|
||||||
|
}
|
||||||
|
}
|
9
app/frontend/src/javascript/models/file.ts
Normal file
9
app/frontend/src/javascript/models/file.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export interface FileType {
|
||||||
|
id?: number,
|
||||||
|
attachment_name?: string,
|
||||||
|
attachment_url?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ImageType extends FileType {
|
||||||
|
is_main?: boolean
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user