Skip to content

Commit

Permalink
refactor(FileUploadDialog.tsx): refactor fileUploadHelpers imports an…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
ktun95 committed Dec 19, 2024
1 parent 9e2b095 commit 2310153
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/editor-toolbar/src/components/FileUploadDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,15 +36,19 @@ type FileUploadDialogProperties = {
const FileUploadDialog = ({ open }: FileUploadDialogProperties) => {
const [selectedFile, setSelectedFile] = useState<Option<File>>(none)
const [fileError, setFileError] = useState<Option<ErrorState>>(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<HTMLInputElement> = async ({
target: { files },
}) => {
Expand All @@ -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)
}
Expand Down Expand Up @@ -83,7 +91,7 @@ const FileUploadDialog = ({ open }: FileUploadDialogProperties) => {
<Dialog open={open} onClose={handleClose}>
{match(data)
.with({ uploadFile: { url: P.select(P.string) } }, (url) => (
<InsertUrl fileUrl={url} />
<InsertUrl handleClose={handleClose} fileUrl={url} />
))
.otherwise(() => (
<Upload
Expand Down

0 comments on commit 2310153

Please sign in to comment.