Skip to content

Commit 07d85e4

Browse files
committed
Add a component state canCESPromptOpen in ProductFeed
Reason to change: There was a bug - Success modal is displayed, CES prompt is not shown - Open a new tab to the product feed page, CES prompt is shown - Go back to the original tab and dismiss the success modal, the CES prmopt is shown, which is what we don't want. To solve it, moving `removeCESPromptFlagFromLocal` from `onNoticeDismissed` and `onModalShown` to only `onNoticeShown` is not enough, as it leads to another problem: - Success modal is displayed, CES prompt is not shown - Open a new tab to the product feed page, CES prompt is shown - Click `Give feedback` but the CES modal is not displayed This is because we change to remove the CES flag from local storage when the CES prompt is shown. When it happens the ProductFeed will re-render and the CES prmopt will not be rendered because the flag was removed. Then clicking `Give feedback` won't render the modal. To solve this we add a component state `canCESPromptOpen` in ProductFeed component, rather than check the local storage flag directly. Then we add the logic to update the `canCESPromptOpen` state only when its value is false.
1 parent 3c9b2f6 commit 07d85e4

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

js/src/components/customer-effort-score-prompt/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,19 @@ const CustomerEffortScorePrompt = ( { eventContext, label } ) => {
5454
};
5555

5656
const onNoticeShown = () => {
57+
removeCESPromptFlagFromLocal();
5758
recordEvent( 'gla_ces_snackbar_open', {
5859
context: eventContext,
5960
} );
6061
};
6162

6263
const onNoticeDismissed = () => {
63-
removeCESPromptFlagFromLocal();
6464
recordEvent( 'gla_ces_snackbar_closed', {
6565
context: eventContext,
6666
} );
6767
};
6868

6969
const onModalShown = () => {
70-
removeCESPromptFlagFromLocal();
7170
recordEvent( 'gla_ces_modal_open', {
7271
context: eventContext,
7372
} );

js/src/product-feed/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* External dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5+
import { useState, useEffect } from '@wordpress/element';
56
import { getQuery } from '@woocommerce/navigation';
67

78
/**
@@ -22,26 +23,35 @@ import isWCTracksEnabled from '.~/utils/isWCTracksEnabled';
2223

2324
const ProductFeed = () => {
2425
const { hasFinishedResolution, data } = useProductFeedPrefetch();
26+
const [ canCESPromptOpen, setCESPromptOpen ] = useState( false );
2527

2628
// Show submission success guide modal by visiting the path with a specific query `guide=submission-success`.
2729
// For example: `/wp-admin/admin.php?page=wc-admin&path=%2Fgoogle%2Fproduct-feed&guide=submission-success`.
2830
const isSubmissionSuccessOpen =
2931
getQuery()?.guide === GUIDE_NAMES.SUBMISSION_SUCCESS;
3032

31-
const canCESPromptOpen = localStorage.get(
32-
LOCAL_STORAGE_KEYS.CAN_ONBOARDING_SETUP_CES_PROMPT_OPEN
33-
);
34-
3533
const wcTracksEnabled = isWCTracksEnabled();
3634

37-
const shouldOpenCESPrompt =
38-
! isSubmissionSuccessOpen && canCESPromptOpen && wcTracksEnabled;
35+
useEffect( () => {
36+
if ( ! canCESPromptOpen ) {
37+
const canCESPromptOpenLocal = localStorage.get(
38+
LOCAL_STORAGE_KEYS.CAN_ONBOARDING_SETUP_CES_PROMPT_OPEN
39+
);
40+
41+
const canOpen =
42+
! isSubmissionSuccessOpen &&
43+
canCESPromptOpenLocal &&
44+
wcTracksEnabled;
45+
46+
setCESPromptOpen( canOpen );
47+
}
48+
}, [ isSubmissionSuccessOpen, canCESPromptOpen, wcTracksEnabled ] );
3949

4050
return (
4151
<>
4252
<NavigationClassic />
4353
{ isSubmissionSuccessOpen && <SubmissionSuccessGuide /> }
44-
{ shouldOpenCESPrompt && (
54+
{ canCESPromptOpen && (
4555
<CustomerEffortScorePrompt
4656
label={ __(
4757
'How easy was it to set up Google Listings & Ads?',

js/src/product-feed/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe( 'ProductFeed', () => {
8787
).not.toBeInTheDocument();
8888
} );
8989

90-
describe( 'And both canCESPromptOpen and wcTracksEnabled are false', () => {
90+
describe( 'And both canCESPromptOpenLocal and wcTracksEnabled are false', () => {
9191
beforeAll( () => {
9292
localStorage.get.mockImplementation( () => {
9393
return false;
@@ -110,7 +110,7 @@ describe( 'ProductFeed', () => {
110110
} );
111111
} );
112112

113-
describe( 'And canCESPromptOpen is true but wcTracksEnabled is false', () => {
113+
describe( 'And canCESPromptOpenLocal is true but wcTracksEnabled is false', () => {
114114
beforeAll( () => {
115115
localStorage.get.mockImplementation( () => {
116116
return true;
@@ -133,7 +133,7 @@ describe( 'ProductFeed', () => {
133133
} );
134134
} );
135135

136-
describe( 'And wcTracksEnabled is true but canCESPromptOpen is false', () => {
136+
describe( 'And wcTracksEnabled is true but canCESPromptOpenLocal is false', () => {
137137
beforeAll( () => {
138138
localStorage.get.mockImplementation( () => {
139139
return false;
@@ -156,7 +156,7 @@ describe( 'ProductFeed', () => {
156156
} );
157157
} );
158158

159-
describe( 'And both canCESPromptOpen and wcTracksEnabled are true', () => {
159+
describe( 'And both canCESPromptOpenLocal and wcTracksEnabled are true', () => {
160160
beforeAll( () => {
161161
localStorage.get.mockImplementation( () => {
162162
return true;

0 commit comments

Comments
 (0)