Skip to content

fix: survey preview updation issues#3269

Merged
gupta-piyush19 merged 1 commit intomainfrom
wc-fix
Oct 3, 2024
Merged

fix: survey preview updation issues#3269
gupta-piyush19 merged 1 commit intomainfrom
wc-fix

Conversation

@Dhruwang
Copy link
Member

@Dhruwang Dhruwang commented Oct 3, 2024

What does this PR do?

Fixes survey preview not getting updated.

We recently added an advanced logic editor to Formbricks, which involved switching from using survey to localSurvey in survey.tsx. While localSurvey worked well for handling questions, it wasn’t reflecting changes made to the ending and welcome cards. To resolve this, we implemented a useEffect that keeps survey and localSurvey synchronized. This effect doesn’t run when a user takes a survey but is necessary in the survey editor, where modifications to the survey can occur.

How should this be tested?

Make changes to welcome card/ thank you card. Survey preview wont update

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

Summary by CodeRabbit

  • New Features
    • Enhanced the Survey component to automatically update and reflect changes in survey data when edited.

This improvement ensures users see the most current survey information without needing to refresh or navigate away.

@vercel
Copy link

vercel bot commented Oct 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
formbricks-cloud ⬜️ Ignored (Inspect) Oct 3, 2024 6:05am
formbricks-docs ⬜️ Ignored (Inspect) Oct 3, 2024 6:05am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 3, 2024

Walkthrough

The changes introduce a new useEffect hook in the Survey component, which updates the localSurvey state whenever the survey prop changes. This ensures that the component accurately reflects updates to the survey data, particularly during editing. The effect is triggered by changes to the survey prop, maintaining the integrity of the displayed survey data without altering existing functionalities.

Changes

File Path Change Summary
packages/surveys/src/components/general/Survey.tsx Added a useEffect hook to update localSurvey state when the survey prop changes.

Possibly related PRs

  • fix: load more button applies filter #3167: The changes in the MultipleChoiceSummary component involve event handling improvements, which may relate to the overall responsiveness of survey components, similar to the useEffect hook added in the Survey component to handle prop changes.
  • fix: toast Message while creating the survey #3240: The SuccessMessage component's updates to the useEffect logic for displaying messages upon survey submission could be relevant, as both PRs involve managing component state and effects in response to user interactions with surveys.

Suggested labels

bug

Suggested reviewers

  • jobenjada

Poem

In the meadow, surveys bloom,
With props that change, we make room.
A hook to catch each little shift,
Ensures our data's always swift.
So hop along, let’s celebrate,
For every change, we elevate! 🐇✨


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

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2024

Thank you for following the naming conventions for pull request titles! 🙏

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

🧹 Outside diff range and nitpick comments (2)
packages/surveys/src/components/general/Survey.tsx (2)

57-61: Approve the addition of the useEffect hook for survey synchronization.

The new useEffect hook effectively addresses the issue of survey preview not updating correctly after modifications. It ensures that localSurvey state is synchronized with the survey prop, which is crucial for reflecting changes in the survey editor context.

However, to optimize performance, consider memoizing the survey prop using useMemo if it's an object, especially if it's coming from a parent component that might re-render frequently. This would prevent unnecessary re-renders of the Survey component.

Consider adding a memoization for the survey prop:

const memoizedSurvey = useMemo(() => survey, [JSON.stringify(survey)]);

useEffect(() => {
  setlocalSurvey(memoizedSurvey);
}, [memoizedSurvey]);

This approach ensures that the effect only runs when the survey content actually changes, not just when the parent component re-renders.


Line range hint 1-486: Consider refactoring the Survey component for improved maintainability.

While the current changes address the immediate issue, the Survey component appears to be quite large and complex. This could lead to maintenance challenges in the future. Consider the following suggestions for improving the overall structure:

  1. Break down the component into smaller, more manageable sub-components.
  2. Extract complex logic (e.g., evaluateLogicAndGetNextQuestionId) into separate utility functions.
  3. Use custom hooks to encapsulate related state and effects (e.g., a useSurveyNavigation hook).
  4. Implement memoization for expensive computations to optimize performance.

Here's a high-level example of how you might start refactoring:

// Custom hook for survey navigation
const useSurveyNavigation = (survey, initialQuestionId) => {
  // ... navigation state and logic
};

// Custom hook for response handling
const useSurveyResponses = (onResponse, onFinished) => {
  // ... response state and logic
};

// Smaller, focused components
const SurveyHeader = ({ showLanguageSwitch, onClose }) => {
  // ... header JSX
};

const SurveyContent = ({ currentQuestion, onSubmit }) => {
  // ... content JSX
};

const SurveyFooter = ({ isBrandingEnabled, showProgressBar }) => {
  // ... footer JSX
};

// Main Survey component
export const Survey = (props) => {
  const { currentQuestion, nextQuestion } = useSurveyNavigation(props.survey, props.startAtQuestionId);
  const { responseData, handleResponse } = useSurveyResponses(props.onResponse, props.onFinished);

  // ... rest of the component logic

  return (
    <StackedCardsContainer {...containerProps}>
      <SurveyHeader {...headerProps} />
      <SurveyContent currentQuestion={currentQuestion} onSubmit={handleResponse} />
      <SurveyFooter {...footerProps} />
    </StackedCardsContainer>
  );
};

This structure would make the component easier to understand, test, and maintain. Consider implementing these changes in a future refactoring task.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0b9d76e and 3972faf.

📒 Files selected for processing (1)
  • packages/surveys/src/components/general/Survey.tsx (1 hunks)
🔇 Additional comments (1)
packages/surveys/src/components/general/Survey.tsx (1)

Line range hint 1-486: Summary of Survey.tsx review

  1. The addition of the useEffect hook (lines 57-61) successfully addresses the issue of survey preview not updating correctly after modifications. This change is approved and aligns well with the PR objectives.

  2. A suggestion for memoizing the survey prop has been provided to optimize performance and prevent unnecessary re-renders.

  3. While not directly related to the current PR, a recommendation for refactoring the Survey component has been made to improve its maintainability and scalability in the future.

Overall, the changes in this PR effectively solve the immediate problem. However, consider the suggested optimizations and potential refactoring in future iterations to ensure long-term maintainability of this critical component.

Copy link
Contributor

@gupta-piyush19 gupta-piyush19 left a comment

Choose a reason for hiding this comment

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

LGTM 🚀
Changes look good and work great; thanks. 😊

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