Frontend School

Document Uploader with Drag-and-Drop

Machine Coding
medium

Document Uploader with Drag-and-Drop

Medium
React
Frontend
File Upload
UI/UX
Machine Coding

Overview

Zerodha requires users to submit various documents (e.g., PAN card, Aadhaar card, bank statements) for KYC verification during account opening or updates. This process often involves uploading multiple files. Your task is to build a robust and user-friendly file upload component that allows users to select and manage documents before initiating an upload. This problem focuses on the client-side UI and logic for handling file selection, validation, and status updates.

What you need to do / Task

Build a React component that fulfills the following requirements:

  1. File Selection:
    • Allow users to select files using a traditional file input dialog (triggered by a button click).
    • Implement a drag-and-drop zone where users can drag files from their local system directly into the component.
  2. File Display & Management:
    • As files are selected (via either method), display a list of these files. Each item in the list should show:
      • File name
      • File size (e.g., "2.5 MB")
      • A status indicator (e.g., "Pending", "Uploading...", "Uploaded", "Failed")
      • A button/icon to remove the file from the list before an overall upload is triggered.
  3. Client-side Validation:
    • Before adding a file to the display list, validate it against the specified constraints (file type and size).
    • If a file fails validation, provide clear feedback to the user (e.g., an error message next to the invalid file or a toast notification). Do not add invalid files to the main upload list.
  4. Simulated Upload Process:
    • Add an "Upload All" button. When clicked, iterate through the pending files and simulate an upload for each.
    • The simulation should mimic an asynchronous operation (e.g., using setTimeout).
    • Randomly simulate success or failure for each file after a short delay (e.g., 1-2 seconds).
    • Update the status of each file in the display list accordingly ("Uploading...", "Uploaded", "Failed").

UI/UX expectations

  • A clearly delineated drag-and-drop area, perhaps with a dashed border.
  • A prominent button to "Select Files" that triggers the file input.
  • The list of selected files should be easy to read and manage.
  • Error messages for invalid files should be user-friendly and informative.
  • The component should be responsive enough for typical desktop use.

Acceptance criteria

  • User can successfully drag and drop files into the designated area.
  • User can click a button to open a file selection dialog and add files.
  • Only files meeting the type and size constraints are added to the list.
  • Each selected file is displayed with its name, size, and current status.
  • Users can remove individual files from the list before uploading.
  • Clicking "Upload All" triggers a simulated asynchronous upload for all pending files.
  • File statuses are updated correctly during and after the simulated upload.
  • Invalid files are rejected with appropriate user feedback.

Constraints

Constraints & assumptions

  • Maximum Files: A batch upload should not exceed 5 files at a time. If more than 5 files are added, subsequent files should be rejected with a message.
  • Maximum File Size: Each individual file must not exceed 5 MB.
  • Allowed File Types: Only .pdf, .jpg, .jpeg, .png are permitted.
  • Backend Simulation: There will be no actual backend API integration. All "upload" operations should be simulated using client-side logic (e.g., setTimeout).
  • Environment: The application will run in a modern web browser.
  • Focus: The primary focus is on frontend UI and logic, not complex styling or accessibility beyond basic usability.

Out of scope

  • Server-side file processing or storage.
  • Advanced network error handling (e.g., retries, detailed error codes).
  • User authentication or authorization.
  • Progress bars for individual file uploads (status text is sufficient).
  • Complex animations or transitions.

What interviewers evaluate

  • React Component Design: How well the component is structured, state management, and use of React hooks.
  • File API & Event Handling: Correct implementation of browser File API for reading file metadata and handling drag-and-drop events.
  • Client-side Validation: Accuracy and robustness of file type and size validation.
  • User Feedback & Experience: Clarity of UI for file selection, status updates, and error messages.
  • Code Quality: Readability, maintainability, and adherence to best practices.

Loading editor...