-
Notifications
You must be signed in to change notification settings - Fork 131
fix: block submit while file upload is pending #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { shouldBlockSubmitForFileUpload } from "../file-upload-validation"; | ||
|
|
||
| describe("shouldBlockSubmitForFileUpload", () => { | ||
| test("does not block an untouched optional upload field", () => { | ||
| expect( | ||
| shouldBlockSubmitForFileUpload({ | ||
| selectedFilesCount: 0, | ||
| uploadedFilesCount: 0, | ||
| isUploading: false, | ||
| }) | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("blocks when a selected file has not produced an uploaded path yet", () => { | ||
| expect( | ||
| shouldBlockSubmitForFileUpload({ | ||
| selectedFilesCount: 1, | ||
| uploadedFilesCount: 0, | ||
| isUploading: true, | ||
| }) | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test("allows submit after each selected file has an uploaded path", () => { | ||
| expect( | ||
| shouldBlockSubmitForFileUpload({ | ||
| selectedFilesCount: 2, | ||
| uploadedFilesCount: 2, | ||
| isUploading: false, | ||
| }) | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("does not block multipart file uploads", () => { | ||
| expect( | ||
| shouldBlockSubmitForFileUpload({ | ||
| isMultipartFile: true, | ||
| selectedFilesCount: 1, | ||
| uploadedFilesCount: 0, | ||
| isUploading: true, | ||
| }) | ||
| ).toBe(false); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| "use client"; | ||
|
|
||
| import React, { useMemo, useState, useEffect } from "react"; | ||
| import React, { useMemo, useState, useEffect, useRef } from "react"; | ||
| import { | ||
| FileUploader, | ||
| FileUploaderTrigger, | ||
|
|
@@ -16,6 +16,7 @@ import { DropzoneOptions } from "react-dropzone"; | |
| import { UploadStatus, useFileUploader } from "./use-file-uploader"; | ||
| import type { FileUploaderFn } from "./uploader"; | ||
| import Image from "next/image"; | ||
| import { shouldBlockSubmitForFileUpload } from "./file-upload-validation"; | ||
|
|
||
| type Accept = { | ||
| [key: string]: string[]; | ||
|
|
@@ -59,6 +60,8 @@ export const FileUploadField = ({ | |
| onFilesChange, | ||
| isMultipartFile, | ||
| }: FileUploadDropzoneProps) => { | ||
| const uploadedFileValueRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (isMultipartFile) return; | ||
| if (!uploader) { | ||
|
|
@@ -69,7 +72,7 @@ export const FileUploadField = ({ | |
| }, [isMultipartFile, uploader]); | ||
|
|
||
| const [files, setFiles] = useState<File[]>([]); | ||
| const { getStatus, data } = useFileUploader({ | ||
| const { getStatus, data, isUploading } = useFileUploader({ | ||
| files, | ||
| uploader, | ||
| autoUpload: true, | ||
|
|
@@ -94,6 +97,45 @@ export const FileUploadField = ({ | |
| () => data.map((info) => info.path).filter(Boolean) as string[], | ||
| [data] | ||
| ); | ||
| const uploadedFilesValue = uploadedFilesPaths.join(","); | ||
|
|
||
| const shouldBlockSubmit = shouldBlockSubmitForFileUpload({ | ||
| isMultipartFile, | ||
| selectedFilesCount: files.length, | ||
| uploadedFilesCount: uploadedFilesPaths.length, | ||
| isUploading, | ||
| }); | ||
|
|
||
| const validationMessage = shouldBlockSubmit | ||
| ? "Please wait for the selected file upload to finish." | ||
| : ""; | ||
|
Comment on lines
+109
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation message is inaccurate when upload is not in progress
🤖 Prompt for AI Agents |
||
|
|
||
| useEffect(() => { | ||
| uploadedFileValueRef.current?.setCustomValidity(validationMessage); | ||
| }, [validationMessage]); | ||
|
|
||
| useEffect(() => { | ||
| const input = uploadedFileValueRef.current; | ||
| const form = input?.form; | ||
|
|
||
| if (!input || !form || isMultipartFile) return; | ||
|
|
||
| const handleSubmit = (event: SubmitEvent) => { | ||
| if (!shouldBlockSubmit) return; | ||
|
|
||
| input.setCustomValidity(validationMessage); | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| event.stopImmediatePropagation(); | ||
| input.reportValidity(); | ||
| }; | ||
|
|
||
| form.addEventListener("submit", handleSubmit, true); | ||
|
|
||
| return () => { | ||
| form.removeEventListener("submit", handleSubmit, true); | ||
| }; | ||
| }, [isMultipartFile, shouldBlockSubmit, validationMessage]); | ||
|
|
||
| return ( | ||
| <FileUploader | ||
|
|
@@ -114,9 +156,10 @@ export const FileUploadField = ({ | |
| /> | ||
| {!isMultipartFile && ( | ||
| <UploadedFileValue | ||
| ref={uploadedFileValueRef} | ||
| name={name} | ||
| required={required} | ||
| value={uploadedFilesPaths} | ||
| required={required || shouldBlockSubmit} | ||
| value={uploadedFilesValue} | ||
| /> | ||
| )} | ||
| </> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| export function shouldBlockSubmitForFileUpload(args: { | ||
| isMultipartFile?: boolean; | ||
| selectedFilesCount: number; | ||
| uploadedFilesCount: number; | ||
| isUploading: boolean; | ||
| }): boolean { | ||
| if (args.isMultipartFile) return false; | ||
| if (args.selectedFilesCount === 0) return false; | ||
|
|
||
| return args.isUploading || args.uploadedFilesCount < args.selectedFilesCount; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for count mismatch when
isUploadingis false.There’s no assertion for the
uploadedFilesCount < selectedFilesCountbranch when upload activity has stopped, which is part of the helper contract.Proposed test addition
describe("shouldBlockSubmitForFileUpload", () => { + test("blocks when selected files still lack uploaded paths even if not actively uploading", () => { + expect( + shouldBlockSubmitForFileUpload({ + selectedFilesCount: 2, + uploadedFilesCount: 1, + isUploading: false, + }) + ).toBe(true); + }); + test("does not block an untouched optional upload field", () => { expect( shouldBlockSubmitForFileUpload({ selectedFilesCount: 0, uploadedFilesCount: 0,📝 Committable suggestion
🤖 Prompt for AI Agents