-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathInternalMultipleChoiceSurveyLogic.ts
92 lines (89 loc) · 3.51 KB
/
InternalMultipleChoiceSurveyLogic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @fileoverview A logic that handles the internal multiple choice survey
*/
import { actions, afterMount, kea, key, listeners, path, props, reducers } from 'kea'
import posthog, { Survey as PostHogSurvey } from 'posthog-js'
import type { InternalMultipleChoiceSurveyLogicType } from './InternalMultipleChoiceSurveyLogicType'
export interface InternalSurveyLogicProps {
surveyId: string
}
export const InternalMultipleChoiceSurveyLogic = kea<InternalMultipleChoiceSurveyLogicType>([
path(['lib', 'components', 'InternalSurvey', 'InternalMultipleChoiceSurveyLogic']),
props({} as InternalSurveyLogicProps),
key((props) => props.surveyId),
actions({
setSurveyId: (surveyId: string) => ({ surveyId }),
getSurveys: () => ({}),
setSurvey: (survey: PostHogSurvey) => ({ survey }),
handleSurveys: (surveys: PostHogSurvey[]) => ({ surveys }),
handleSurveyResponse: () => ({}),
handleChoiceChange: (choice: string, isAdded: boolean) => ({ choice, isAdded }),
setShowThankYouMessage: (showThankYouMessage: boolean) => ({ showThankYouMessage }),
setThankYouMessage: (thankYouMessage: string) => ({ thankYouMessage }),
}),
reducers({
surveyId: [
null as string | null,
{
setSurveyId: (_, { surveyId }) => surveyId,
},
],
survey: [
null as PostHogSurvey | null,
{
setSurvey: (_, { survey }) => survey,
},
],
thankYouMessage: [
'Thank you for your feedback!',
{
setThankYouMessage: (_, { thankYouMessage }) => thankYouMessage,
},
],
showThankYouMessage: [
false as boolean,
{
setShowThankYouMessage: (_, { showThankYouMessage }) => showThankYouMessage,
},
],
surveyResponse: [
[] as string[],
{
handleChoiceChange: (state, { choice, isAdded }) =>
isAdded ? [...state, choice] : state.filter((c) => c !== choice),
},
],
}),
listeners(({ actions, values }) => ({
/** When surveyId is set, get the list of surveys for the user */
setSurveyId: () => {
posthog.getSurveys(actions.handleSurveys)
},
/** Callback for the surveys response. Filter it to the surveyId and set the survey */
handleSurveys: ({ surveys }) => {
const survey = surveys.find((s: PostHogSurvey) => s.id === values.surveyId)
if (survey) {
posthog.capture('survey shown', {
$survey_id: values.surveyId,
})
actions.setSurvey(survey)
if (survey.appearance?.thankYouMessageHeader) {
actions.setThankYouMessage(survey.appearance?.thankYouMessageHeader)
}
}
},
/** When the survey response is sent, capture the response and show the thank you message */
handleSurveyResponse: () => {
posthog.capture('survey sent', {
$survey_id: values.surveyId,
$survey_response: values.surveyResponse,
})
actions.setShowThankYouMessage(true)
setTimeout(() => actions.setSurvey(null), 5000)
},
})),
afterMount(({ actions, props }) => {
/** When the logic is mounted, set the surveyId from the props */
actions.setSurveyId(props.surveyId)
}),
])