From f2d24c74dd7a395d5c49eb92777a331c6109f96f Mon Sep 17 00:00:00 2001 From: Illia Rudniev Date: Thu, 5 Sep 2024 13:52:27 +0300 Subject: [PATCH] feat: added clear field functionality --- .../DocumentField/DocumentField.tsx | 10 ++++- .../FileUploaderField/FileUploaderField.tsx | 41 +++++++++++++++---- .../useFileRepository/useFileRepository.ts | 10 +++++ .../hooks/useFileUploading/types.ts | 3 ++ .../components/FileUploaderField/types.ts | 2 +- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/DocumentField/DocumentField.tsx b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/DocumentField/DocumentField.tsx index ae569ec84a..8fd4a7b235 100644 --- a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/DocumentField/DocumentField.tsx +++ b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/DocumentField/DocumentField.tsx @@ -139,6 +139,7 @@ export const DocumentField = ( message: response.message as string, type: 'warning', }); + return; } @@ -159,7 +160,7 @@ export const DocumentField = ( ); const handleChange = useCallback( - (fileId: string) => { + (fileId: string, clear?: boolean) => { //@ts-ignore const destinationParser = new DocumentValueDestinationParser(definition.valueDestination); const pathToDocumentsList = destinationParser.extractRootPath(); @@ -202,8 +203,13 @@ export const DocumentField = ( ({} as Document['pages'][number]); // Assigning file properties + if (clear) { + set(documentPage, pathToFileId!, undefined); + } else { + set(documentPage, pathToFileId!, fileId); + } + //@ts-ignore - set(documentPage, pathToFileId, fileId); set(documentPage, 'fileName', file?.name); set(documentPage, 'type', file?.type); diff --git a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/FileUploaderField.tsx b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/FileUploaderField.tsx index 37ee8abfc2..c2c12a3b7f 100644 --- a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/FileUploaderField.tsx +++ b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/FileUploaderField.tsx @@ -2,8 +2,8 @@ import { useFileAssigner } from '@/components/organisms/UIRenderer/elements/JSON import { useFileRepository } from '@/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileRepository'; import { useFileUploading } from '@/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileUploading'; import { DocumentUploadFieldProps } from '@/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/types'; -import { Input } from '@ballerine/ui'; -import { Upload } from 'lucide-react'; +import { Button, Input } from '@ballerine/ui'; +import { Upload, XCircle } from 'lucide-react'; import { useCallback, useRef } from 'react'; export const FileUploaderField = ({ @@ -19,10 +19,11 @@ export const FileUploaderField = ({ testId, }: DocumentUploadFieldProps) => { const { isUploading, uploadFile } = useFileUploading(_uploadFile); - const { file: registeredFile, registerFile } = useFileRepository( - fileStorage, - fileId || undefined, - ); + const { + file: registeredFile, + registerFile, + removeFile, + } = useFileRepository(fileStorage, fileId || undefined); const inputRef = useRef(null); //@ts-ignore useFileAssigner(inputRef, registeredFile); @@ -46,9 +47,23 @@ export const FileUploaderField = ({ inputRef.current?.click(); }, [inputRef]); + const handleClear = useCallback( + (event: React.SyntheticEvent) => { + event.stopPropagation(); + + if (!registeredFile || !fileId || !inputRef.current) return; + + inputRef.current.value = ''; + + removeFile(fileId); + onChange(fileId, true); + }, + [fileId, inputRef, registeredFile, removeFile, onChange], + ); + return (
@@ -58,6 +73,18 @@ export const FileUploaderField = ({ {registeredFile ? registeredFile.name : 'No File Choosen'} + {registeredFile && ( + + )} { + fileRepository.removeByFileId(fileId); + setFile(null); + }, + [fileRepository], + ); + const repositoryListener: FileRepositoryListener = useCallback( (updatedFileId, action) => { if (fileId === updatedFileId && action === 'add') { @@ -48,5 +57,6 @@ export const useFileRepository = ( return { file, registerFile, + removeFile, }; }; diff --git a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileUploading/types.ts b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileUploading/types.ts index ad98242bc6..237d03c274 100644 --- a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileUploading/types.ts +++ b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileUploading/types.ts @@ -16,7 +16,10 @@ export type UseFileUploadOnUploadCallback = (fileId: string) => void; export type RegisterFileFn = (file: File, fileId: string) => File; +export type RemoveFileFn = (fileId: string) => void; + export interface UseFileRepositoryResult { file: File | null; registerFile: RegisterFileFn; + removeFile: RemoveFileFn; } diff --git a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/types.ts b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/types.ts index 18c3160081..8dce0d27e2 100644 --- a/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/types.ts +++ b/apps/kyb-app/src/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/types.ts @@ -5,7 +5,7 @@ export type UploadedFileResult = { fileId: string }; export type UploadFileCallback = (file: File) => Promise; export interface DocumentUploadFieldProps { - onChange: (fileId: string) => void; + onChange: (fileId: string, clear?: boolean) => void; uploadFile: UploadFileFn; onBlur?: () => void; fileRepository: FileRepository;