Skip to content

Commit

Permalink
Merge branch 'master' into dn-fix/show-defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin committed Sep 22, 2023
2 parents cea2491 + 3ac6b80 commit 0ae7850
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 6 deletions.
Binary file modified frontend/__snapshots__/scenes-app-surveys--new-survey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions frontend/src/scenes/surveys/Survey.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SceneExport } from 'scenes/sceneTypes'
import { NewSurvey, defaultSurveyAppearance, surveyLogic } from './surveyLogic'
import { NewSurvey, defaultSurveyAppearance, defaultSurveyFieldValues, surveyLogic } from './surveyLogic'
import { BindLogic, useActions, useValues } from 'kea'
import { Form, Group } from 'kea-forms'
import { PageHeader } from 'lib/components/PageHeader'
Expand Down Expand Up @@ -60,7 +60,7 @@ export function SurveyComponent({ id }: { id?: string } = {}): JSX.Element {

export function SurveyForm({ id }: { id: string }): JSX.Element {
const { survey, surveyLoading, isEditingSurvey, hasTargetingFlag } = useValues(surveyLogic)
const { loadSurvey, editingSurvey, setSurveyValue } = useActions(surveyLogic)
const { loadSurvey, editingSurvey, setSurveyValue, setDefaultForQuestionType } = useActions(surveyLogic)
const { featureFlags } = useValues(enabledFeaturesLogic)

return (
Expand Down Expand Up @@ -119,6 +119,16 @@ export function SurveyForm({ id }: { id: string }): JSX.Element {
<Group name={`questions.${index}`} key={index}>
<Field name="type" label="Question type" className="max-w-60">
<LemonSelect
onSelect={(newType) => {
const stateObj = survey.questions[0]
const isEditingQuestion =
defaultSurveyFieldValues[stateObj.type].questions[0].question !==
stateObj.question
const isEditingDescription =
defaultSurveyFieldValues[stateObj.type].questions[0].description !==
stateObj.description
setDefaultForQuestionType(newType, isEditingQuestion, isEditingDescription)
}}
options={[
{ label: 'Open text', value: SurveyQuestionType.Open },
{ label: 'Link', value: SurveyQuestionType.Link },
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/scenes/surveys/SurveyAppearance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ export function SurveyAppearance({
)}
</>
)}
{(type === SurveyQuestionType.Open || type === SurveyQuestionType.Link) && (
{[
SurveyQuestionType.Open,
SurveyQuestionType.Link,
SurveyQuestionType.SingleChoice,
SurveyQuestionType.MultipleChoice,
].includes(type) && (
<>
<div className="mt-2">Button color</div>
<LemonInput
Expand Down
113 changes: 111 additions & 2 deletions frontend/src/scenes/surveys/surveyLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
PropertyFilterType,
PropertyOperator,
Survey,
SurveyQuestionBase,
SurveyQuestionType,
SurveyType,
} from '~/types'
Expand Down Expand Up @@ -46,20 +47,90 @@ export interface NewSurvey

export const defaultSurveyAppearance = {
backgroundColor: 'white',
submitButtonColor: '#2C2C2C',
textColor: 'black',
submitButtonText: 'Submit',
submitButtonColor: '#2c2c2c',
ratingButtonColor: '#e0e2e8',
descriptionTextColor: '#4b4b52',
whiteLabel: false,
displayThankYouMessage: true,
thankYouMessageHeader: 'Thank you for your feedback!',
}

export const defaultSurveyFieldValues = {
[SurveyQuestionType.Open]: {
questions: [
{
question: 'Give us feedback on our product!',
description: '',
},
],
appearance: {
submitButtonText: 'Submit',
},
},
[SurveyQuestionType.Link]: {
questions: [
{
question: 'Do you want to join our upcoming webinar?',
description: '',
},
],
appearance: {
submitButtonText: 'Register',
thankYouMessageHeader: 'Redirecting ...',
},
},
[SurveyQuestionType.Rating]: {
questions: [
{
question: 'How likely are you to recommend us to a friend?',
description: '',
display: 'number',
scale: 10,
lowerBoundLabel: 'Unlikely',
upperBoundLabel: 'Very likely',
},
],
appearance: {},
},
[SurveyQuestionType.SingleChoice]: {
questions: [
{
question: 'Have you found this tutorial useful?',
description: '',
choices: ['Yes', 'No'],
},
],
appearance: {
submitButtonText: 'Submit',
},
},
[SurveyQuestionType.MultipleChoice]: {
questions: [
{
question: 'Which types of content would you like to see more of?',
description: '',
choices: ['Tutorials', 'Customer case studies', 'Product announcements'],
},
],
appearance: {
submitButtonText: 'Submit',
},
},
}

export const NEW_SURVEY: NewSurvey = {
id: 'new',
name: '',
description: '',
questions: [{ type: SurveyQuestionType.Open, question: '' }],
questions: [
{
type: SurveyQuestionType.Open,
question: defaultSurveyFieldValues[SurveyQuestionType.Open].questions[0].question,
description: defaultSurveyFieldValues[SurveyQuestionType.Open].questions[0].description,
},
],
type: SurveyType.Popover,
linked_flag_id: undefined,
targeting_flag_filters: undefined,
Expand Down Expand Up @@ -108,6 +179,15 @@ export const surveyLogic = kea<surveyLogicType>([
})),
actions({
editingSurvey: (editing: boolean) => ({ editing }),
setDefaultForQuestionType: (
type: SurveyQuestionType,
isEditingQuestion: boolean,
isEditingDescription: boolean
) => ({
type,
isEditingQuestion,
isEditingDescription,
}),
launchSurvey: true,
stopSurvey: true,
archiveSurvey: true,
Expand Down Expand Up @@ -178,6 +258,35 @@ export const surveyLogic = kea<surveyLogicType>([
editingSurvey: (_, { editing }) => editing,
},
],
survey: [
{ ...NEW_SURVEY } as NewSurvey | Survey,
{
setDefaultForQuestionType: (state, { type, isEditingQuestion, isEditingDescription }) => {
const question = isEditingQuestion
? state.questions[0].question
: defaultSurveyFieldValues[type].questions[0].question
const description = isEditingDescription
? state.questions[0].description
: defaultSurveyFieldValues[type].questions[0].description

return {
...state,
questions: [
{
...state.questions[0],
...(defaultSurveyFieldValues[type].questions[0] as SurveyQuestionBase),
question,
description,
},
],
appearance: {
...state.appearance,
...defaultSurveyFieldValues[type].appearance,
},
}
},
},
],
}),
selectors({
isSurveyRunning: [
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ export interface SurveyAppearance {
thankYouMessageDescription?: string
}

interface SurveyQuestionBase {
export interface SurveyQuestionBase {
question: string
description?: string | null
required?: boolean
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ae7850

Please sign in to comment.