Skip to content
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

Fix/profile photo upload issues #11092

Closed

Conversation

Rustix69
Copy link
Contributor

@Rustix69 Rustix69 commented Mar 6, 2025

Proposed Changes

@ohcnetwork/care-fe-code-reviewers

Summary by CodeRabbit

  • New Features

    • Introduced an intuitive image cropping tool in the avatar editing feature, allowing you to adjust your photo before uploading.
    • Enhanced input validation with clear error messages for minimum character limits, numerical values, age requirements, and image dimension guidelines.
  • Chores

    • Updated underlying dependencies to support improved image cropping functionality.

@Rustix69 Rustix69 requested a review from a team as a code owner March 6, 2025 20:26
Copy link
Contributor

coderabbitai bot commented Mar 6, 2025

Walkthrough

This update adds a new image cropping feature to the application. It introduces the react-easy-crop dependency in the project configuration and implements a new utility function, getCroppedImg, to handle image cropping. The localization file has been enhanced with additional error messages for input validations. The AvatarEditModal component now integrates the cropping functionality by adding new state variables, methods, and UI elements to manage and display the crop process.

Changes

File(s) Change Summary
package.json Added new dependency "react-easy-crop": "^5.4.1".
public/locale/en.json Added new error messages: "min_length_error", "min_value_error", "minimum_age_should_be", and "minimum_image_dimensions".
src/Utils/getCroppedImg.tsx Introduced the getCroppedImg utility function to crop an image using a canvas and return a PNG Blob URL.
src/components/.../AvatarEditModal.tsx Integrated cropping functionality using react-easy-crop; added new methods (handleCropImage, onCropComplete) and state variables for crop control.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant M as AvatarEditModal
    participant C as Cropper Component
    participant G as getCroppedImg Utility

    U->>M: Select image for upload
    M->>C: Activate cropping interface
    C->>M: Return cropped area coordinates (onCropComplete)
    M->>G: Call getCroppedImg(imageSrc, croppedAreaPixels)
    G-->>M: Return cropped image Blob URL
    M->>M: Update state with cropped image and preview
    M->>U: Display cropped image preview
Loading

Possibly related PRs

Suggested labels

tested, needs review

Suggested reviewers

  • Jacobjeevan
  • rithviknishad

Poem

Oh, I’m a rabbit with a coder’s flair,
Hopping through code with a joyful air.
Cropped images now prance on screen so neat,
Every line of code a crunchy, delightful treat!
With carrots of changes and features that gleam,
I cheer, "Hop on board, let’s code as a team!"
🥕🐇

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Mar 6, 2025

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit 1fdd91f
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/67ca0a998175250008ded031
😎 Deploy Preview https://deploy-preview-11092.preview.ohc.network
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/Utils/getCroppedImg.tsx (1)

10-35: Improve type safety in the getCroppedImg function.

The function implementation is good but uses any type for croppedAreaPixels. Consider creating a proper interface for this parameter.

- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- export async function getCroppedImg(imageSrc: string, croppedAreaPixels: any) {
+ interface CroppedAreaPixels {
+   x: number;
+   y: number;
+   width: number;
+   height: number;
+ }
+ export async function getCroppedImg(imageSrc: string, croppedAreaPixels: CroppedAreaPixels) {
src/components/Common/AvatarEditModal.tsx (3)

68-88: Consider applying file size validation to uploaded images.

State variables for cropping functionality are well-defined, but the component should validate that uploaded files don't exceed the 1MB size limit mentioned in the hint text.

const onSelectFile: ChangeEventHandler<HTMLInputElement> = (e) => {
  if (!e.target.files || e.target.files.length === 0) {
    setSelectedFile(undefined);
    return;
  }
  if (!isImageFile(e.target.files[0])) {
    toast.warning(t("please_upload_an_image_file"));
    return;
  }
+ // Check file size (1MB = 1048576 bytes)
+ if (e.target.files[0].size > 1048576) {
+   toast.warning(t("max_size_for_image_uploaded_should_be", { maxSize: "1MB" }));
+   return;
+ }
  setSelectedFile(e.target.files[0]);
  setIsCropping(true);
};

143-156: Consider cleanup for the object URL in the cropped image effect.

The useEffect that processes the cropped image should revoke the object URL when it's no longer needed to prevent memory leaks.

useEffect(() => {
  if (croppedImage) {
    fetch(croppedImage)
      .then((res) => res.blob())
      .then((blob) => {
        const myFile = new File([blob], "cropped_image.png", {
          type: blob.type,
        });
        setSelectedFile(myFile);
        setPreview(croppedImage);
        setCroppedImage(null);
+       // Revoke the object URL to free memory
+       URL.revokeObjectURL(croppedImage);
      });
  }
}, [croppedImage]);

527-588: Consider extracting UI components to reduce complexity.

The camera controls section is getting quite complex with multiple conditional renders. Consider extracting these into separate components for better maintainability.

You could create separate components like CameraControls, CroppingControls, and PreviewControls to make the main component more focused and easier to maintain.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1fdd91f and ead55f0.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (4)
  • package.json (1 hunks)
  • public/locale/en.json (1 hunks)
  • src/Utils/getCroppedImg.tsx (1 hunks)
  • src/components/Common/AvatarEditModal.tsx (14 hunks)
🔇 Additional comments (12)
package.json (1)

109-109: Adding the react-easy-crop dependency for image cropping functionality.

The new dependency has been properly added with the correct version specification.

src/Utils/getCroppedImg.tsx (1)

1-8: Well-implemented utility for creating and handling image elements.

The createImage helper function is well-structured using promises to handle asynchronous image loading.

public/locale/en.json (1)

1339-1345: New translation strings for user feedback and validation.

The added strings improve user experience by providing specific feedback for validation errors related to length, value, age, and image dimensions.

src/components/Common/AvatarEditModal.tsx (9)

8-25: Good import organization for the new cropping functionality.

The imports for Cropper from react-easy-crop and the utility function getCroppedImg are properly placed.


37-48: Consistent camera constraints for square image capture.

The video constraints are correctly set to capture images in a 1:1 aspect ratio (720x720), which aligns well with the cropping functionality.


98-111: Well-implemented crop completion handler.

The onCropComplete callback is correctly implemented as a memoized function that updates the cropped area pixels state.


124-141: Good error handling in the crop image function.

The handleCropImage function properly manages loading states and handles errors with user feedback.


158-167: Complete state reset in the closeModal function.

The closeModal function properly resets all states, including the new cropping-related states.


285-319: Well-implemented cropping UI with intuitive controls.

The cropping interface displays properly and provides clear actions for the user to crop or cancel.


412-419: Good UX with a dedicated crop button for uploaded images.

Adding a separate crop button when an image is previewed gives users clear control over the cropping process.


466-480: Responsive webcam implementation with good error handling.

The webcam container is properly styled with a fixed aspect ratio, and errors are handled with user feedback.


484-504: Consistent UI pattern for cropping captured images.

The cropping interface for captured images follows the same pattern as for uploaded images, providing a consistent user experience.

@Rustix69 Rustix69 closed this Mar 6, 2025
@Rustix69 Rustix69 force-pushed the fix/profile-photo-upload-issues branch from ead55f0 to 1fdd91f Compare March 6, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Profile Photo Upload Issues: Missing 1:1 Crop and Unclear Pixel Requirements
1 participant