-
Notifications
You must be signed in to change notification settings - Fork 274
/
CurrentSurvey.js
327 lines (291 loc) · 9.16 KB
/
CurrentSurvey.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* CurrentSurvey component.
*
* Site Kit by Google, Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import { useMount } from 'react-use';
import { Slide } from '@material-ui/core';
/**
* WordPress dependencies
*/
import { useCallback, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import Data from 'googlesitekit-data';
import { CORE_FORMS } from '../../googlesitekit/datastore/forms/constants';
import { CORE_USER } from '../../googlesitekit/datastore/user/constants';
import SurveyCompletion from './SurveyCompletion';
import SurveyQuestionRating from './SurveyQuestionRating';
import SurveyQuestionOpenText from './SurveyQuestionOpenText';
import SurveyQuestionMultiSelect from './SurveyQuestionMultiSelect';
import SurveyQuestionSingleSelect from './SurveyQuestionSingleSelect';
import SurveyTerms from './SurveyTerms';
const { useDispatch, useSelect } = Data;
const SURVEY_ANSWER_DELAY_MS = 300;
const TYPE_MULTI_SELECT = 'multi_select';
const TYPE_OPEN_TEXT = 'open_text';
const TYPE_RATING = 'rating';
const TYPE_SINGLE_SELECT = 'single_select';
const KNOWN_QUESTION_TYPES = [
TYPE_MULTI_SELECT,
TYPE_OPEN_TEXT,
TYPE_RATING,
TYPE_SINGLE_SELECT,
];
export default function CurrentSurvey() {
const [ sentSurveyShownEvent, setSentSurveyShownEvent ] = useState( false );
const [ sentCompletionEvent, setSentCompletionEvent ] = useState( false );
const [ animateSurvey, setAnimateSurvey ] = useState( false );
const [ hasAnsweredQuestion, setHasAnsweredQuestion ] = useState( false );
const completions = useSelect( ( select ) =>
select( CORE_USER ).getCurrentSurveyCompletions()
);
const questions = useSelect( ( select ) =>
select( CORE_USER ).getCurrentSurveyQuestions()
);
const surveySession = useSelect( ( select ) =>
select( CORE_USER ).getCurrentSurveySession()
);
const isTrackingEnabled = useSelect( ( select ) =>
select( CORE_USER ).isTrackingEnabled()
);
const formName = surveySession
? `survey-${ surveySession.session_id }`
: null;
const shouldHide = useSelect( ( select ) =>
select( CORE_FORMS ).getValue( formName, 'hideSurvey' )
);
const answers = useSelect(
( select ) => select( CORE_FORMS ).getValue( formName, 'answers' ) || []
);
const { setValues } = useDispatch( CORE_FORMS );
const { sendSurveyEvent } = useDispatch( CORE_USER );
useEffect( () => {
if ( questions?.length && ! sentSurveyShownEvent ) {
setSentSurveyShownEvent( true );
sendSurveyEvent( 'survey_shown' );
}
}, [ questions, sentSurveyShownEvent, sendSurveyEvent ] );
// We only have trigger conditions for questions that are answered with
// ordinal values right now.
const ordinalAnswerMap = answers.reduce(
( acc, { question_ordinal: ordinal, answer } ) => ( {
...acc,
[ ordinal ]: answer.answer.answer_ordinal || answer.answer,
} ),
{}
);
let currentQuestionOrdinal =
Math.max( 0, ...answers.map( ( answer ) => answer.question_ordinal ) ) +
1;
const currentQuestion = questions?.find( ( question ) => {
const {
question_ordinal: questionOrdinal,
trigger_condition: conditions,
} = question;
// Ignore questions that have ordinal lower than the current one.
if ( questionOrdinal < currentQuestionOrdinal ) {
return false;
}
if ( Array.isArray( conditions ) && conditions.length > 0 ) {
for ( const condition of conditions ) {
const answer = ordinalAnswerMap[ condition.question_ordinal ];
const allowedAnswers = condition.answer_ordinal || [];
if ( ! answer || ! allowedAnswers.includes( answer ) ) {
currentQuestionOrdinal++;
return false;
}
}
}
return questionOrdinal === currentQuestionOrdinal;
} );
const answerQuestion = useCallback(
( answer ) => {
if ( hasAnsweredQuestion ) {
return;
}
setHasAnsweredQuestion( true );
sendSurveyEvent( 'question_answered', {
// eslint-disable-next-line camelcase
question_ordinal: currentQuestion?.question_ordinal,
answer,
} );
setTimeout( () => {
setValues( formName, {
answers: [
...answers,
{
// eslint-disable-next-line camelcase
question_ordinal: currentQuestion?.question_ordinal,
answer,
},
],
} );
setHasAnsweredQuestion( false );
}, SURVEY_ANSWER_DELAY_MS );
},
[
answers,
currentQuestion,
formName,
sendSurveyEvent,
setValues,
hasAnsweredQuestion,
]
);
// Check to see if a completion trigger has been met.
let triggeredCompletion;
if ( questions?.length && currentQuestionOrdinal > questions.length ) {
triggeredCompletion = ( completions || [] ).find( ( completion ) => {
const conditions = completion.trigger_condition || [];
for ( const condition of conditions ) {
// If a question was answered with the appropriate value, a completion
// trigger has been fulfilled and we should treat this survey as
// complete.
if (
condition.answer_ordinal.includes(
ordinalAnswerMap[ condition.question_ordinal ]
)
) {
// eslint-disable-line camelcase
triggeredCompletion = completion;
return true;
}
}
return false;
} );
// If no specific trigger has been found but all questions are answered, use
// the first available trigger.
if ( ! triggeredCompletion ) {
triggeredCompletion = completions[ 0 ];
}
}
const ctaOnClick = useCallback( () => {
sendSurveyEvent( 'follow_up_link_clicked', {
completion_ordinal: triggeredCompletion?.completion_ordinal, // eslint-disable-line camelcase
} );
sendSurveyEvent( 'survey_closed' );
setValues( formName, { hideSurvey: true } );
}, [ formName, sendSurveyEvent, setValues, triggeredCompletion ] );
const dismissSurvey = useCallback( () => {
sendSurveyEvent( 'survey_closed' );
setAnimateSurvey( false );
}, [ sendSurveyEvent ] );
const handleAnimationOnExited = useCallback( () => {
setValues( formName, { hideSurvey: true } );
}, [ formName, setValues ] );
useEffect( () => {
if ( triggeredCompletion && ! sentCompletionEvent ) {
setSentCompletionEvent( true );
sendSurveyEvent( 'completion_shown', {
completion_ordinal: triggeredCompletion?.completion_ordinal, // eslint-disable-line camelcase
} );
}
}, [ sentCompletionEvent, sendSurveyEvent, triggeredCompletion ] );
useMount( () => {
setAnimateSurvey( true );
} );
if (
shouldHide ||
! questions ||
! completions ||
isTrackingEnabled === undefined
) {
return null;
}
if ( triggeredCompletion ) {
return (
<Slide
direction="up"
in={ animateSurvey }
onExited={ handleAnimationOnExited }
>
<div className="googlesitekit-survey">
<SurveyCompletion
dismissSurvey={ dismissSurvey }
ctaOnClick={ ctaOnClick }
ctaText={ triggeredCompletion.follow_up_text }
ctaURL={ triggeredCompletion.follow_up_url }
title={ triggeredCompletion.completion_title }
>
{ triggeredCompletion.completion_text }
</SurveyCompletion>
</div>
</Slide>
);
}
// eslint-disable-next-line camelcase
if ( ! KNOWN_QUESTION_TYPES.includes( currentQuestion?.question_type ) ) {
return null;
}
const commonProps = {
key: currentQuestion.question_text,
answerQuestion,
dismissSurvey,
question: currentQuestion.question_text,
submitButtonText:
questions?.length === currentQuestionOrdinal
? __( 'Submit', 'google-site-kit' )
: __( 'Next', 'google-site-kit' ),
};
return (
<Slide
direction="up"
in={ animateSurvey }
onExited={ handleAnimationOnExited }
>
<div className="googlesitekit-survey">
{ currentQuestion.question_type === TYPE_MULTI_SELECT && (
<SurveyQuestionMultiSelect
{ ...commonProps }
choices={ currentQuestion.question.answer_choice }
minChoices={ currentQuestion.question.min_choices }
maxChoices={ currentQuestion.question.max_choices }
/>
) }
{ currentQuestion.question_type === TYPE_OPEN_TEXT && (
<SurveyQuestionOpenText
{ ...commonProps }
subtitle={ currentQuestion.question.subtitle }
placeholder={ currentQuestion.question.placeholder }
/>
) }
{ currentQuestion.question_type === TYPE_RATING && (
<SurveyQuestionRating
{ ...commonProps }
choices={ currentQuestion.question.answer_choice }
/>
) }
{ currentQuestion.question_type === TYPE_SINGLE_SELECT && (
<SurveyQuestionSingleSelect
{ ...commonProps }
choices={ currentQuestion.question.answer_choice }
/>
) }
{ isTrackingEnabled === false &&
currentQuestion?.question_ordinal === 1 && ( // eslint-disable-line camelcase
<div className="googlesitekit-survey__footer">
<SurveyTerms />
</div>
) }
</div>
</Slide>
);
}