Overview
At Nykaa, providing a seamless experience for sellers to showcase their products is crucial. High-quality product images are a cornerstone of this experience. This problem challenges you to build a robust and user-friendly image upload component that handles multiple files, provides clear visual feedback, and ensures basic client-side data integrity before actual upload.
What you need to do / Task
Your task is to develop a React component named App that functions as a product image uploader. This component should allow users to select multiple image files, display them in a list, and manage their upload status.
- File Selection: Create a UI element (e.g., a button) that, when clicked, allows the user to select multiple image files (
.jpg, .jpeg, .png, .gif).
- Display Selected Files: Once files are selected, display them in a list. Each list item should clearly show:
- The file's name.
- Its size, formatted for readability (e.g., "1.2 MB").
- Its current status (e.g., "Pending", "Uploading...", "Uploaded", "Failed", "Invalid").
- An "Upload" button.
- A "Remove" button.
- Remove File Functionality: Implement the "Remove" button for each file. Clicking this button should delete the corresponding file from the selection list.
- Simulated Upload: Implement the "Upload" button for each file. Clicking this button should:
- Update the file's status to "Uploading...".
- Simulate an asynchronous upload operation (e.g., using
setTimeout for 2-3 seconds).
- After the simulated upload, update the file's status to either "Uploaded" or "Failed" (you can randomize this for demonstration purposes, e.g., 80% success rate).
- Disable the "Upload" button once the file has been successfully uploaded.
- Clear All Functionality: Add a "Clear All" button that, when clicked, removes all files from the current selection list.
- Client-side Validation: Implement the following validation rules when files are selected:
- File Type: Only allow
image/jpeg, image/png, and image/gif files. Files of other types should be marked as "Invalid" with an appropriate error message.
- File Size: Each file must not exceed 2 MB. Files larger than this should be marked as "Invalid" with an appropriate error message.
UI/UX expectations
- The component should have a clear area or button for initiating file selection.
- The list of selected files should be easy to read, with each file entry visually distinct.
- Status indicators (e.g., text, potentially different colors) should clearly communicate the state of each file (pending, uploading, uploaded, failed, invalid).
- Buttons should be appropriately enabled/disabled based on the file's status (e.g., "Upload" disabled during upload or after completion).
- Validation error messages should be clearly displayed next to the invalid file.
Acceptance criteria
- User can select multiple image files via a file input or button interaction.
- Selected files are displayed in a list, showing name, formatted size, status, and action buttons ("Upload", "Remove").
- Clicking "Remove" on a file correctly deletes it from the displayed list.
- Clicking "Upload" on a file initiates a simulated asynchronous process, showing "Uploading..." then transitioning to "Uploaded" or "Failed".
- The "Upload" button becomes disabled/hidden for files that are successfully uploaded.
- Clicking "Clear All" effectively removes all files from the list.
- Only image files (JPEG, PNG, GIF) are accepted; non-image files are marked "Invalid" with a message.
- Files exceeding 2MB are marked "Invalid" with a message.
- The UI provides clear feedback for all file statuses and validation errors.