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

feat(surveys branching): handle question shuffling #23116

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions frontend/src/scenes/surveys/QuestionBranchingInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './EditSurvey.scss'

import { LemonSelect } from '@posthog/lemon-ui'
import { LemonDialog, LemonSelect } from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { truncate } from 'lib/utils'
Expand All @@ -17,7 +17,7 @@ export function QuestionBranchingInput({
question: RatingSurveyQuestion | MultipleSurveyQuestion
}): JSX.Element {
const { survey, getBranchingDropdownValue } = useValues(surveyLogic)
const { setQuestionBranchingType } = useActions(surveyLogic)
const { setQuestionBranchingType, setSurveyValue } = useActions(surveyLogic)

const availableNextQuestions = survey.questions
.map((question, questionIndex) => ({
Expand All @@ -37,12 +37,39 @@ export function QuestionBranchingInput({
value={branchingDropdownValue}
data-attr={`branching-question-${questionIndex}`}
onSelect={(type) => {
let specificQuestionIndex
if (type.startsWith(SurveyQuestionBranchingType.SpecificQuestion)) {
specificQuestionIndex = parseInt(type.split(':')[1])
type = SurveyQuestionBranchingType.SpecificQuestion
const handleSelect = (): void => {
let specificQuestionIndex
if (type.startsWith(SurveyQuestionBranchingType.SpecificQuestion)) {
specificQuestionIndex = parseInt(type.split(':')[1])
type = SurveyQuestionBranchingType.SpecificQuestion
}
setQuestionBranchingType(questionIndex, type, specificQuestionIndex)
}

if (survey.appearance.shuffleQuestions) {
LemonDialog.open({
title: 'Your survey has question shuffling enabled',
description: (
<p className="py-2">
Adding branching logic will disable shuffling of questions. Are you sure you
want to continue?
</p>
),
primaryButton: {
children: 'Continue',
status: 'danger',
onClick: () => {
setSurveyValue('appearance', { ...survey.appearance, shuffleQuestions: false })
handleSelect()
},
},
secondaryButton: {
children: 'Cancel',
},
})
} else {
handleSelect()
}
setQuestionBranchingType(questionIndex, type, specificQuestionIndex)
}}
options={[
...(questionIndex < survey.questions.length - 1
Expand Down
36 changes: 32 additions & 4 deletions frontend/src/scenes/surveys/SurveyCustomization.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LemonButton, LemonCheckbox, LemonInput, LemonSelect } from '@posthog/lemon-ui'
import { useValues } from 'kea'
import { LemonButton, LemonCheckbox, LemonDialog, LemonInput, LemonSelect } from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { PayGateMini } from 'lib/components/PayGateMini/PayGateMini'
import { upgradeModalLogic } from 'lib/components/UpgradeModal/upgradeModalLogic'
import { surveyLogic } from 'scenes/surveys/surveyLogic'
Expand All @@ -26,7 +26,8 @@ interface WidgetCustomizationProps extends Omit<CustomizationProps, 'surveyQuest

export function Customization({ appearance, surveyQuestionItem, onAppearanceChange }: CustomizationProps): JSX.Element {
const { surveysStylingAvailable } = useValues(surveysLogic)
const { surveyShufflingQuestionsAvailable } = useValues(surveyLogic)
const { surveyShufflingQuestionsAvailable, hasBranchingLogic } = useValues(surveyLogic)
const { deleteBranchingLogic } = useActions(surveyLogic)
const surveyShufflingQuestionsDisabledReason = surveyShufflingQuestionsAvailable
? ''
: 'Please add more than one question to the survey to enable shuffling questions'
Expand Down Expand Up @@ -130,7 +131,34 @@ export function Customization({ appearance, surveyQuestionItem, onAppearanceChan
<span>Shuffle questions</span>
</div>
}
onChange={(checked) => onAppearanceChange({ ...appearance, shuffleQuestions: checked })}
onChange={(checked) => {
if (checked && hasBranchingLogic) {
onAppearanceChange({ ...appearance, shuffleQuestions: false })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LemonCheckbox behaves like an uncontrolled component when the dialog is opened, so this needs to be set to false to make sure it stays unchecked initially.

LemonDialog.open({
title: 'Your survey has active branching logic',
description: (
<p className="py-2">
Enabling this option will remove your branching logic. Are you sure you want
to continue?
</p>
),
primaryButton: {
children: 'Continue',
status: 'danger',
onClick: () => {
deleteBranchingLogic()
onAppearanceChange({ ...appearance, shuffleQuestions: true })
},
},
secondaryButton: {
children: 'Cancel',
},
})
} else {
onAppearanceChange({ ...appearance, shuffleQuestions: checked })
}
}}
checked={appearance?.shuffleQuestions}
/>
</div>
Expand Down
Loading