-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
customer-effort-score-modal-container.tsx
94 lines (85 loc) · 2.48 KB
/
customer-effort-score-modal-container.tsx
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
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import {
CustomerFeedbackModal,
STORE_KEY,
} from '@woocommerce/customer-effort-score';
import { recordEvent } from '@woocommerce/tracks';
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
/**
* Internal dependencies
*/
import { getStoreAgeInWeeks } from './utils';
import { ADMIN_INSTALL_TIMESTAMP_OPTION_NAME } from './constants';
export const PRODUCT_MVP_CES_ACTION_OPTION_NAME =
'woocommerce_ces_product_mvp_ces_action';
export const CustomerEffortScoreModalContainer: React.FC = () => {
const { createSuccessNotice } = useDispatch( 'core/notices' );
const { hideCesModal } = useDispatch( STORE_KEY );
const {
storeAgeInWeeks,
resolving: isLoading,
visibleCESModalData,
} = useSelect( ( select ) => {
const { getOption, hasFinishedResolution } =
select( OPTIONS_STORE_NAME );
const { getVisibleCESModalData } = select( STORE_KEY );
const adminInstallTimestamp =
( getOption( ADMIN_INSTALL_TIMESTAMP_OPTION_NAME ) as number ) || 0;
const resolving =
adminInstallTimestamp === null ||
! hasFinishedResolution( 'getOption', [
ADMIN_INSTALL_TIMESTAMP_OPTION_NAME,
] );
return {
storeAgeInWeeks: getStoreAgeInWeeks( adminInstallTimestamp ),
visibleCESModalData: getVisibleCESModalData(),
resolving,
};
} );
const recordScore = (
score: number,
secondScore: number,
comments: string
) => {
recordEvent( 'ces_feedback', {
action: visibleCESModalData.action,
score,
score_second_question: secondScore ?? null,
score_combined: score + ( secondScore ?? 0 ),
comments: comments || '',
store_age: storeAgeInWeeks,
} );
createSuccessNotice(
visibleCESModalData.onSubmitLabel ||
__(
"Thanks for the feedback. We'll put it to good use!",
'woocommerce'
),
visibleCESModalData.onSubmitNoticeProps || {}
);
};
if ( ! visibleCESModalData || isLoading ) {
return null;
}
return (
<CustomerFeedbackModal
title={ visibleCESModalData.title }
firstQuestion={ visibleCESModalData.firstQuestion }
secondQuestion={ visibleCESModalData.secondQuestion }
recordScoreCallback={ ( ...args ) => {
recordScore( ...args );
hideCesModal();
visibleCESModalData.props?.onRecordScore?.();
} }
onCloseModal={ () => {
visibleCESModalData.props?.onCloseModal?.();
hideCesModal();
} }
shouldShowComments={ visibleCESModalData.props?.shouldShowComments }
/>
);
};