From 2310153048e284d2e61d44af556508e883bc8adf Mon Sep 17 00:00:00 2001 From: Kevin Tun Date: Thu, 19 Dec 2024 10:23:37 -0600 Subject: [PATCH] refactor(FileUploadDialog.tsx): refactor fileUploadHelpers imports and functions for better organization and readability The changes include refactoring the imports and functions related to file upload helpers in the FileUploadDialog component to improve code organization and readability. This includes renaming getFileError to getFileValidationError and adding isValidFile function for better error handling. --- .../src/components/FileUploadDialog.tsx | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/editor-toolbar/src/components/FileUploadDialog.tsx b/packages/editor-toolbar/src/components/FileUploadDialog.tsx index b79d34f47..77b97330b 100644 --- a/packages/editor-toolbar/src/components/FileUploadDialog.tsx +++ b/packages/editor-toolbar/src/components/FileUploadDialog.tsx @@ -11,16 +11,20 @@ import { head as Ahead } from "fp-ts/Array" import { mapLeft as EmapLeft } from "fp-ts/Either" import { isSome, - isNone, some, none, Option, fromNullable as OfromNullable, map as Omap, flatMap as OflatMap, + getOrElse as OgetOrElse, } from "fp-ts/Option" import { uploadFileDialogOpenAtom } from "../context/atomConfigs" -import { getFileError, ErrorState } from "./fileUploadHelpers" +import { + getFileValidationError, + ErrorState, + isValidFile, +} from "./fileUploadHelpers" import { InsertUrl } from "./InsertUrl" import { Upload } from "./Upload" import { createFileUploadFunction } from "./createUploadFileFunction" @@ -32,15 +36,19 @@ type FileUploadDialogProperties = { const FileUploadDialog = ({ open }: FileUploadDialogProperties) => { const [selectedFile, setSelectedFile] = useState>(none) const [fileError, setFileError] = useState>(none) - const canSubmit = BMonoidAll.concat(isSome(selectedFile), isNone(fileError)) - + const canSubmit = BMonoidAll.concat( + isSome(selectedFile), + pipe( + selectedFile, + Omap(isValidFile), + OgetOrElse(() => false), + ), + ) const { getAccessToken } = useLogto() const [uploadFile, { data, loading, reset }] = useUploadFileMutation() const setDialogDisplay = useSetAtom(uploadFileDialogOpenAtom) - const [editor] = useLexicalComposerContext() - const onFileChange: React.ChangeEventHandler = async ({ target: { files }, }) => { @@ -53,7 +61,7 @@ const FileUploadDialog = ({ open }: FileUploadDialogProperties) => { OflatMap(Ahead), ) // set the error state of the file - pipe(selected, OflatMap(getFileError), setFileError) + pipe(selected, OflatMap(getFileValidationError), setFileError) // set the file state setSelectedFile(selected) } @@ -83,7 +91,7 @@ const FileUploadDialog = ({ open }: FileUploadDialogProperties) => { {match(data) .with({ uploadFile: { url: P.select(P.string) } }, (url) => ( - + )) .otherwise(() => (