Skip to content

Commit

Permalink
feat: added clear field functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr committed Sep 5, 2024
1 parent 778877c commit f2d24c7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const DocumentField = (
message: response.message as string,
type: 'warning',
});

return;
}

Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand All @@ -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<HTMLInputElement>(null);
//@ts-ignore
useFileAssigner(inputRef, registeredFile);
Expand All @@ -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 (
<div
className="flex h-[56px] flex-row items-center gap-3 rounded-[16px] border bg-white px-4"
className="relative flex h-[56px] flex-row items-center gap-3 rounded-[16px] border bg-white px-4"
onClick={handdleContainerClick}
>
<div className="flex gap-3 text-[#007AFF]">
Expand All @@ -58,6 +73,18 @@ export const FileUploaderField = ({
<span className="overflow-hidden text-ellipsis whitespace-nowrap text-sm">
{registeredFile ? registeredFile.name : 'No File Choosen'}
</span>
{registeredFile && (
<Button
variant="ghost"
size="icon"
className="top-[calc(50% - 14px)] absolute right-[-36px] h-[28px] w-[28px] rounded-full"
onClick={handleClear}
>
<div className="rounded-full bg-white">
<XCircle />
</div>
</Button>
)}
<Input
data-testid={testId}
type="file"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
RegisterFileFn,
RemoveFileFn,
UseFileRepositoryResult,
} from '@/components/organisms/UIRenderer/elements/JSONForm/components/FileUploaderField/hooks/useFileUploading/types';
import { useRefValue } from '@/hooks/useRefValue';
Expand All @@ -26,6 +27,14 @@ export const useFileRepository = (
[fileRepository],
);

const removeFile: RemoveFileFn = useCallback(
(fileId: string) => {
fileRepository.removeByFileId(fileId);
setFile(null);
},
[fileRepository],
);

const repositoryListener: FileRepositoryListener = useCallback(
(updatedFileId, action) => {
if (fileId === updatedFileId && action === 'add') {
Expand All @@ -48,5 +57,6 @@ export const useFileRepository = (
return {
file,
registerFile,
removeFile,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type UploadedFileResult = { fileId: string };
export type UploadFileCallback = (file: File) => Promise<UploadedFileResult>;

export interface DocumentUploadFieldProps {
onChange: (fileId: string) => void;
onChange: (fileId: string, clear?: boolean) => void;
uploadFile: UploadFileFn;
onBlur?: () => void;
fileRepository: FileRepository;
Expand Down

0 comments on commit f2d24c7

Please sign in to comment.