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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logic jump for other option #1922

Merged
merged 1 commit into from
Jan 18, 2024
Merged

fix: logic jump for other option #1922

merged 1 commit into from
Jan 18, 2024

Conversation

Dhruwang
Copy link
Contributor

What does this PR do?

Seems like we didnt have a logic to handle logic jumps when other option is selected
In logic object we check for Other as response value in order to execute the jump logic but response value is the actual answer of that Other option

Fixes 1735

How should this be tested?

Add some logic jumps to other option

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read How we Code at Formbricks
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues
  • First PR at Formbricks? Please sign the CLA! Without it we wont be able to merge it 馃檹

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Formbricks Docs if changes were necessary

Copy link

vercel bot commented Jan 18, 2024

The latest updates on your projects. Learn more about Vercel for Git 鈫楋笌

2 Ignored Deployments
Name Status Preview Updated (UTC)
formbricks-cloud 猬滐笍 Ignored (Inspect) Jan 18, 2024 9:16am
formbricks-com 猬滐笍 Ignored (Inspect) Jan 18, 2024 9:16am

Copy link
Contributor

Thank you for following the naming conventions for pull request titles! 馃檹

Copy link
Contributor

packages/surveys/src/components/general/Survey.tsx

Consider extracting the logic for handling multiple choice questions into a separate function. This would improve the readability of the code and make it easier to maintain and debug in the future.
Create Issue
See the diff
Checkout the fix

    function handleMultipleChoiceQuestion(currentQuestion, responseValue, logic) {
      const choice = currentQuestion.choices.find((choice) => choice.label === responseValue);
      // if choice is undefined we can determine that, "other" option is selected
      if (!choice) {
        if (evaluateCondition(logic, "Other")) {
          return logic.destination;
        }
      }
      if (evaluateCondition(logic, responseValue)) {
        return logic.destination;
      }
    }
git fetch origin && git checkout -b ReviewBot/The-n-eli09fh origin/ReviewBot/The-n-eli09fh

Consider using a Map or an Object to store the choices, with the labels as keys. This would allow for constant time lookups, improving the performance of this operation, especially for larger arrays.
Create Issue
See the diff
Checkout the fix

    const choicesMap = new Map(currentQuestion.choices.map(choice => [choice.label, choice]));
    const choice = choicesMap.get(responseValue);
git fetch origin && git checkout -b ReviewBot/The-c-d8z1jcz origin/ReviewBot/The-c-d8z1jcz

currentQuestion.type === "multipleChoiceSingle" ||
currentQuestion.type === "multipleChoiceMulti"
) {
const choice = currentQuestion.choices.find((choice) => choice.label === responseValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace the find method with a Map for constant time lookups, improving performance especially for larger arrays.

Suggested change
const choice = currentQuestion.choices.find((choice) => choice.label === responseValue);
const choicesMap = new Map(currentQuestion.choices.map(choice => [choice.label, choice]));
const choice = choicesMap.get(responseValue);

Copy link
Member

@mattinannt mattinannt left a comment

Choose a reason for hiding this comment

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

@Dhruwang thank you for the fix 馃挭馃敟

@mattinannt mattinannt added this pull request to the merge queue Jan 18, 2024
Merged via the queue into main with commit 41aa828 Jan 18, 2024
14 checks passed
@mattinannt mattinannt deleted the logic-jump-issue branch January 18, 2024 10:30
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.

None yet

2 participants