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(apps/frontend-manage): do not allow duplicate option values in answer collections #4449

Merged
merged 3 commits into from
Jan 10, 2025

Conversation

sjschlapbach
Copy link
Member

@sjschlapbach sjschlapbach commented Jan 10, 2025

Summary by CodeRabbit

  • New Features

    • Added validation to prevent duplicate entries in answer collections.
    • Introduced a new property to manage existing entries in the answer collection component.
  • Bug Fixes

    • Ensured unique values are required when creating answer collections.
  • Documentation

    • Added error messages in German and English explaining unique value requirements.
  • Tests

    • Implemented Cypress tests to verify unique answer option validation and deletion functionality.

Copy link

coderabbitai bot commented Jan 10, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new validation rule for answer collections to ensure that all entries are unique. The changes span across multiple files, including the frontend component, Cypress tests, and internationalization (i18n) message files. The primary goal is to prevent duplicate entries when creating answer collections by adding a uniqueness check to the validation schema and providing appropriate error messages in German and English.

Changes

File Change Summary
apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionCreation.tsx Added a custom validation test to ensure uniqueness of entries in the answer collection.
cypress/cypress/e2e/K-resources-workflow.cy.ts Added test cases to verify unique answer options and validate the creation process.
packages/i18n/messages/de.ts Added a new German error message for unique values requirement.
packages/i18n/messages/en.ts Added a new English error message for unique values requirement.
apps/frontend-manage/src/components/resources/answerCollections/AddAnswerCollectionEntry.tsx Updated props to include entries and added validation to ensure new values are unique.
apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionEditModal.tsx Updated props to pass collection entries to the AddAnswerCollectionEntry component.
apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionOption.tsx Updated props to replace index with otherEntries and added validation for uniqueness in the value field.

Possibly related PRs


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:

  • 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. (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.

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

cypress bot commented Jan 10, 2025

klicker-uzh    Run #4011

Run Properties:  status check passed Passed #4011  •  git commit 2ff28a5032: enhance: add functionality to remove or delete answer collections, withdraw acce...
Project klicker-uzh
Branch Review v3-new-elements
Run status status check passed Passed #4011
Run duration 16m 42s
Commit git commit 2ff28a5032: enhance: add functionality to remove or delete answer collections, withdraw acce...
Committer Julius Schlapbach
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 240
View all changes introduced in this branch ↗︎

Copy link

@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 (2)
apps/frontend-manage/src/components/resources/answerCollections/AddAnswerCollectionEntry.tsx (2)

54-54: Comprehensive form validation implementation

The validation setup effectively prevents duplicate values through:

  1. Immediate validation on mount
  2. Initial touched state for immediate feedback
  3. Clear error message for unique value requirement

However, consider adding input sanitization before the uniqueness check.

Consider adding value trimming to prevent spaces from creating false uniqueness:

 newValue: Yup.string()
   .required(t('manage.resources.valueRequired'))
+  .transform((value) => value?.trim())
   .notOneOf(entryValues, t('manage.resources.uniqueValuesRequired')),

Also applies to: 58-58, 60-62


31-34: Consider case-insensitive comparison for better UX

Currently, the uniqueness check is case-sensitive, meaning "Option" and "option" would be considered different values. Consider implementing case-insensitive comparison for better user experience.

 const entryValues = useMemo(
-  () => entries.map((entry) => entry.value),
+  () => entries.map((entry) => entry.value.toLowerCase()),
   [entries]
 )

 newValue: Yup.string()
   .required(t('manage.resources.valueRequired'))
+  .transform((value) => value?.toLowerCase())
   .notOneOf(entryValues, t('manage.resources.uniqueValuesRequired')),

Also applies to: 60-62

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ab16ee6 and ca190da.

📒 Files selected for processing (3)
  • apps/frontend-manage/src/components/resources/answerCollections/AddAnswerCollectionEntry.tsx (3 hunks)
  • apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionEditModal.tsx (1 hunks)
  • cypress/cypress/e2e/K-resources-workflow.cy.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cypress/cypress/e2e/K-resources-workflow.cy.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: cypress-run
🔇 Additional comments (3)
apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionEditModal.tsx (1)

70-70: LGTM: Proper prop passing with fallback

The addition of the entries prop with a fallback to an empty array is a good defensive programming practice that enables duplicate value validation.

apps/frontend-manage/src/components/resources/answerCollections/AddAnswerCollectionEntry.tsx (2)

7-7: LGTM: Clean type imports and prop interface

The addition of necessary imports and proper TypeScript typing for the entries prop ensures type safety.

Also applies to: 13-13, 18-18, 22-22


31-34: Good use of useMemo for performance optimization

The memoization of entry values prevents unnecessary recalculations when the component re-renders, optimizing performance when the entries array is large.

Copy link

cypress bot commented Jan 10, 2025

klicker-uzh    Run #4014

Run Properties:  status check passed Passed #4014  •  git commit 3b9ded7552 ℹ️: Merge edf9162d15b660d4631589cc92fe5a4c2c3ff805 into 2ff28a503250be438f26f4287198...
Project klicker-uzh
Branch Review deny-duplicate-answer-options
Run status status check passed Passed #4014
Run duration 16m 15s
Commit git commit 3b9ded7552 ℹ️: Merge edf9162d15b660d4631589cc92fe5a4c2c3ff805 into 2ff28a503250be438f26f4287198...
Committer Julius Schlapbach
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 240
View all changes introduced in this branch ↗︎

Copy link

@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 (1)
apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionOption.tsx (1)

193-204: Consider enhancing button state feedback and accessibility.

While the implementation is functional, consider these improvements:

  1. Add a loading spinner or indicator when isSubmitting is true
  2. Use ARIA attributes to communicate the button state to screen readers
  3. Ensure sufficient color contrast in the disabled state
 <Button
   type="submit"
   className={{
     root: twMerge(
       'border-primary-80 hover:border-primary-80 bg-primary-100 h-8 w-8 items-center justify-center border text-white',
-      (isSubmitting || !isValid) &&
-        'bg-primary-60 cursor-not-allowed'
+      (isSubmitting || !isValid) &&
+        'bg-primary-60 cursor-not-allowed opacity-75'
     ),
   }}
   disabled={isSubmitting || !isValid}
+  aria-busy={isSubmitting}
   data={{ cy: 'save-edit-answer-option' }}
 >
-  <FontAwesomeIcon icon={faSave} />
+  {isSubmitting ? (
+    <FontAwesomeIcon icon={faSpinner} spin />
+  ) : (
+    <FontAwesomeIcon icon={faSave} />
+  )}
 </Button>
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ca190da and edf9162.

📒 Files selected for processing (3)
  • apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionEditModal.tsx (2 hunks)
  • apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionOption.tsx (3 hunks)
  • cypress/cypress/e2e/K-resources-workflow.cy.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cypress/cypress/e2e/K-resources-workflow.cy.ts
  • apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionEditModal.tsx
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: check
  • GitHub Check: test
  • GitHub Check: cypress-run
  • GitHub Check: SonarCloud
  • GitHub Check: Analyze (javascript)
  • GitHub Check: build
🔇 Additional comments (3)
apps/frontend-manage/src/components/resources/answerCollections/AnswerCollectionOption.tsx (3)

13-13: LGTM! Props and imports are well structured.

The changes improve type safety with otherEntries array and add proper i18n support.

Also applies to: 16-16, 20-20, 28-28


126-136: Well-implemented validation logic!

The Yup schema effectively prevents duplicate entries with proper i18n error messages. The form configuration ensures immediate validation on mount.


131-134: Verify otherEntries population in parent components.

Let's ensure that otherEntries is properly populated by parent components to maintain the uniqueness constraint.

✅ Verification successful

✓ Uniqueness validation is properly implemented

The otherEntries prop is correctly populated by filtering out the current entry's value from the collection and passing all other values for validation, ensuring uniqueness across all options.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for components that render AnswerCollectionOption
rg -A 5 "AnswerCollectionOption" apps/frontend-manage/src/

# Search for components that prepare otherEntries
rg -A 5 "otherEntries" apps/frontend-manage/src/

Length of output: 9276

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
7.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@sjschlapbach sjschlapbach merged commit 1a1a69a into v3-new-elements Jan 10, 2025
14 of 15 checks passed
@sjschlapbach sjschlapbach deleted the deny-duplicate-answer-options branch January 10, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant