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

457 formik and yup validations for hint fe #499

Merged
merged 2 commits into from
Jan 20, 2025

Conversation

DeboraSerra
Copy link
Contributor

@DeboraSerra DeboraSerra commented Jan 17, 2025

Describe your changes

Added formik to hint content and appearence.

image
image

Issue number

#457

Please ensure all items are checked off before requesting a review:

  • I deployed the code locally.
  • I have performed a self-review of my code.
  • I have included the issue # in the PR.
  • I have labelled the PR correctly.
  • The issue I am working on is assigned to me.
  • I didn't use any hardcoded values (otherwise it will not scale, and will make it difficult to maintain consistency across the application).
  • I made sure font sizes, color choices etc are all referenced from the theme.
  • My PR is granular and targeted to one specific feature.
  • I took a screenshot or a video and attached to this PR if there is a UI change.

@DeboraSerra DeboraSerra requested a review from erenfn January 17, 2025 23:33
Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

Walkthrough

The pull request introduces a comprehensive refactoring of hint-related components, focusing on improved form validation and state management. The changes primarily involve integrating Formik and Yup for enhanced input handling, introducing error props, and consolidating state management across multiple components. The modifications streamline the process of creating and managing hints by implementing more robust validation and error handling mechanisms.

Changes

File Change Summary
frontend/src/components/ColorTextField/ColorTextField.jsx Added optional error prop for improved error handling
frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.css Added .hint-appearance-error and .hint-appearance-item CSS classes
frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.jsx Refactored to use Formik, changed data prop type, added form validation
frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.css Added .error-message CSS class
frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.jsx Integrated Formik for form management and validation
frontend/src/scenes/hints/CreateHintPage.jsx Consolidated appearance-related state into a single object
frontend/src/utils/hintHelper.js Added Yup validation schemas for hints and appearance

Sequence Diagram

sequenceDiagram
    participant User
    participant Form
    participant Formik
    participant Validation
    participant OnSave

    User->>Form: Enters form data
    Form->>Formik: Captures input
    Formik->>Validation: Validates input
    alt Input is valid
        Validation-->>Formik: Validation passes
        Formik->>OnSave: Triggers save
    else Input is invalid
        Validation-->>Formik: Displays error messages
        Formik->>Form: Highlights invalid fields
    end
Loading

Possibly related PRs

Suggested labels

design

Suggested reviewers

  • erenfn
  • gorkem-bwl

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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. (Beta)
  • @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.

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
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: 4

🔭 Outside diff range comments (1)
frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.jsx (1)

Line range hint 23-28: Guard against unexpected inputs in targetElement validation

Your targetElement validation function assumes that value is always a string (line 27). If value is undefined or another type, this could throw an error. To keep your application steady, add a check to ensure value is a string before calling startsWith.

Apply this change to enhance robustness:

-      (value) => {
-        return value.startsWith('#') || value.startsWith('.');
+      (value) => {
+        return typeof value === 'string' && (value.startsWith('#') || value.startsWith('.'));
       }
🧹 Nitpick comments (7)
frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.css (1)

25-33: Yo dawg, let's make this error styling more robust!

The error styling looks decent, but there's one thing making my knees weak - the absolute positioning without a clear reference point. Consider these improvements to avoid any spaghetti situations:

 .hint-appearance-error {
-  position: absolute;
+  position: absolute;
+  bottom: -20px;
+  left: 0;
   font-family: var(--font-family-inter);
   font-size: var(--font-size-sm);
   font-weight: 400;
-  line-height: 1.45;
+  line-height: 1.5;
   margin-top: 0px;
   color: var(--error-color);
 }
frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.jsx (2)

87-90: Ease the burden by simplifying event handlers with Formik's state management

In the onChange handlers for your input fields (lines 87-90, 119-122, 139-142, 160-162, 175-178), you're calling both your custom handler functions like handleUrl(e) and Formik's handleChange(e). This could lead to redundant state updates and added complexity. Since Formik effectively manages form state, consider relying solely on handleChange(e) and utilize Formik's values for your state needs. This keeps the code light and prevents potential conflicts.

Also applies to: 119-122, 139-142, 160-162, 175-178


91-94: Prevent missteps by streamlining validation in onBlur handlers

In your onBlur events (lines 91-94, 123-126, 143-146, 163-166), you're invoking both handleBlur(e) and validateField('fieldName'). Formik's handleBlur(e) already triggers validation for the field. To avoid unnecessary repetition and possible performance hits, you can trust Formik to handle validation solely with handleBlur(e).

Also applies to: 123-126, 143-146, 163-166

frontend/src/scenes/hints/CreateHintPage.jsx (2)

28-41: Yo dawg, this state consolidation is fire! 🔥

Consolidating the appearance states into a single object is a solid move. It's like organizing your mom's spaghetti - everything in one pot!

Consider adding TypeScript or PropTypes to make this even more robust:

const appearanceShape = {
  headerBackgroundColor: PropTypes.string.isRequired,
  headerColor: PropTypes.string.isRequired,
  textColor: PropTypes.string.isRequired,
  buttonBackgroundColor: PropTypes.string.isRequired,
  buttonTextColor: PropTypes.string.isRequired,
};

138-141: Yo, these styles need more flex appeal! 💪

Fixed width might make your UI lose itself in different screen sizes.

Consider making it more responsive:

- width: '400px',
- maxWidth: '700px',
- marginLeft: '2.5rem',
+ width: '100%',
+ maxWidth: '700px',
+ margin: '1rem auto',
frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.jsx (2)

7-13: This mapping's got the right flow, but let's level up! 🎯

The label mapping is clean, but it could be more reusable.

Consider moving it to a constants file:

// src/constants/hints.js
export const APPEARANCE_LABELS = {
  headerBackgroundColor: 'Header Background Color',
  // ... rest of the mappings
};

50-61: The validation's got the timing of a rap battle - we need it faster! 🎵

The ColorTextField integration is solid, but the validation timing could drop beats faster.

Consider enabling immediate validation:

  <Formik
    initialValues={data}
    validationSchema={appearanceSchema}
-   validateOnMount={false}
-   validateOnBlur={false}
+   validateOnMount={true}
+   validateOnChange={true}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd7cca1 and de9b01f.

📒 Files selected for processing (7)
  • frontend/src/components/ColorTextField/ColorTextField.jsx (1 hunks)
  • frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.css (1 hunks)
  • frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.jsx (1 hunks)
  • frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.css (1 hunks)
  • frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.jsx (2 hunks)
  • frontend/src/scenes/hints/CreateHintPage.jsx (4 hunks)
  • frontend/src/utils/hintHelper.js (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • frontend/src/components/HintPageComponents/HintLeftContent/HintLeftContent.css
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build (22.x)
🔇 Additional comments (4)
frontend/src/components/HintPageComponents/HintLeftAppearance/HintLeftAppearance.css (1)

35-37: Mom's spaghetti approved! 🍝

The relative positioning is on point - creating that sweet containment context for those error messages. Clean and simple, just like home cooking!

frontend/src/components/ColorTextField/ColorTextField.jsx (1)

8-8: All clear! Props to you for enhancing error handling

The addition of the error prop in your ColorTextField component is a solid move, improving validation feedback for users.

frontend/src/scenes/hints/CreateHintPage.jsx (2)

65-71: Clean state updates, straight outta the backend! 👊

The fallback values are on point, keeping the UI consistent even when the data's weak. That's the way to handle it!


179-182: Props game strong, no cap! 💯

Clean prop passing with all the necessary handlers for Formik integration. You only get one shot, do not miss your chance to blow, and you didn't!

@erenfn erenfn merged commit 999fa03 into develop Jan 20, 2025
2 checks passed
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.

2 participants