Skip to content

Commit 7152c8b

Browse files
Refactor deactivation modal (#927)
Co-authored-by: godaddy-wordpress-bot <plugins@godaddy.com>
1 parent 5a376b2 commit 7152c8b

File tree

8 files changed

+154
-134
lines changed

8 files changed

+154
-134
lines changed
Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,37 @@
1+
/* global goThemeDeactivateData */
2+
13
import PropTypes from 'prop-types';
24
import { safeHTML } from '@wordpress/dom';
35
import { Button, ButtonGroup, CheckboxControl, Modal, TextControl } from '@wordpress/components';
46
import { useCallback, useEffect, useState } from '@wordpress/element';
57

68
const language = document.documentElement.getAttribute( 'lang' ) || 'en-US';
79

8-
const fetchData = async ( apiUrl, getParams = null ) => {
9-
const params = {
10-
...getParams,
11-
language,
12-
random: 1,
13-
};
14-
let paramString = '';
15-
Object.keys( params ).forEach( ( key ) => paramString += `${ key }=${ params[ key ] }&` );
16-
17-
try {
18-
const response = await fetch( `${ apiUrl }?${ paramString.slice( 0, -1 ) }` );
19-
if ( ! response.ok ) {
20-
return null;
21-
}
22-
23-
return await response.json();
24-
} catch ( e ) {
25-
return null;
26-
}
27-
};
28-
29-
const DeactivateModal = ( { apiUrl, getParams, isEvent, pageData } ) => {
10+
const DeactivateModal = ( { apiUrl, isEvent, pageData } ) => {
3011
const [ href, setHref ] = useState( null );
3112
const [ isOpen, setOpen ] = useState( false );
32-
const [ feedbackData, setFeedbackData ] = useState( null );
3313
const [ formData, setFormData ] = useState( {} );
3414

3515
useEffect( () => {
36-
const getData = async () => {
37-
const data = await fetchData( apiUrl, getParams );
38-
39-
if ( data && data.can_submit_feedback ) {
40-
window.addEventListener( 'click', clickHandler );
41-
setInitialData( data );
42-
}
43-
};
44-
45-
getData();
46-
47-
return () => {
48-
window.removeEventListener( 'click', clickHandler );
49-
};
50-
}, [] );
51-
52-
const setInitialData = ( data ) => {
53-
setFeedbackData( data );
54-
5516
const textFields = {};
56-
data.choices.forEach( ( choice ) => {
17+
goThemeDeactivateData.choices.forEach( ( choice ) => {
5718
if ( !! choice.text_field ) {
5819
textFields[ choice.text_field ] = '';
5920
}
6021
} );
6122

23+
window.addEventListener( 'click', clickHandler );
24+
6225
setFormData( {
63-
choices: [],
26+
choices: goThemeDeactivateData.choices,
6427
domain: pageData.domain,
6528
go_theme_version: pageData.goThemeVersion,
6629
hostname: pageData.hostname,
6730
language,
6831
wp_version: pageData.wpVersion,
6932
...textFields,
7033
} );
71-
};
34+
}, [] );
7235

7336
const clickHandler = useCallback( ( e ) => {
7437
if ( ! isEvent( e ) ) {
@@ -103,7 +66,7 @@ const DeactivateModal = ( { apiUrl, getParams, isEvent, pageData } ) => {
10366
};
10467

10568
const onAction = async ( submit = false ) => {
106-
if ( submit && formData.choices.length >= feedbackData.choices_min ) {
69+
if ( submit && formData.choices.length >= 0 ) {
10770
await fetch( apiUrl, {
10871
body: JSON.stringify( formData ),
10972
headers: {
@@ -117,19 +80,22 @@ const DeactivateModal = ( { apiUrl, getParams, isEvent, pageData } ) => {
11780
window.location.href = href;
11881
};
11982

120-
if ( ! isOpen || ! feedbackData ) {
83+
if ( ! isOpen ) {
12184
return null;
12285
}
12386

12487
return (
12588
<Modal
12689
className="go-deactivate-modal"
12790
onRequestClose={ () => setOpen( false ) }
128-
title={ feedbackData.labels.title }
91+
title={ goThemeDeactivateData.labels.title }
12992
>
13093
<div className="go-deactivate-modal__checkbox">
131-
{ feedbackData.choices.map( ( choice ) => {
94+
{ goThemeDeactivateData.choices.map( ( choice ) => {
13295
const isChecked = formData.choices.indexOf( choice.slug ) >= 0;
96+
if ( typeof choice !== 'object' ) {
97+
return null;
98+
}
13399
return (
134100
<div key={ choice.slug }>
135101
<CheckboxControl
@@ -156,21 +122,21 @@ const DeactivateModal = ( { apiUrl, getParams, isEvent, pageData } ) => {
156122
onClick={ () => onAction( true ) }
157123
variant="primary"
158124
>
159-
{ feedbackData.labels.submit_deactivate }
125+
{ goThemeDeactivateData.labels.submit_deactivate }
160126
</Button>
161127
<Button
162128
className="go-deactivate-modal__button"
163129
onClick={ () => onAction( false ) }
164130
variant="link"
165131
>
166-
{ feedbackData.labels.skip_deactivate }
132+
{ goThemeDeactivateData.labels.skip_deactivate }
167133
</Button>
168134
</ButtonGroup>
169135

170136
<footer className="go-deactivate-modal__footer">
171137
<div
172138
dangerouslySetInnerHTML={ {
173-
__html: safeHTML( feedbackData.labels.privacy_disclaimer ),
139+
__html: safeHTML( goThemeDeactivateData.labels.privacy_disclaimer ),
174140
} }
175141
/>
176142
</footer>
@@ -180,12 +146,10 @@ const DeactivateModal = ( { apiUrl, getParams, isEvent, pageData } ) => {
180146

181147
DeactivateModal.propTypes = {
182148
apiUrl: PropTypes.string.isRequired,
183-
getParams: PropTypes.object,
184149
isEvent: PropTypes.func.isRequired,
185150
pageData: PropTypes.object.isRequired,
186151
};
187152

188153
export {
189154
DeactivateModal as default,
190-
fetchData,
191155
};

.dev/assets/admin/js/common-action-modal/test/index.spec.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import '@testing-library/jest-dom/extend-expect';
1414
* Internal dependencies.
1515
*/
1616
import mockData from '../../../../../tests/cypress/fixtures/network/go_optout.json';
17-
import Modal, { fetchData } from '../index';
17+
import Modal from '../index';
1818

1919
import { enableFetchMocks } from 'jest-fetch-mock';
2020
enableFetchMocks();
@@ -72,6 +72,8 @@ describe( 'go-deactivate-modal', () => {
7272
fetch.resetMocks();
7373
fetch.mockResponse( JSON.stringify( mockData ) );
7474

75+
global.goThemeDeactivateData = mockData;
76+
7577
events = {};
7678
window.addEventListener = jest.fn( ( event, callback ) => {
7779
events[ event ] = callback;
@@ -82,23 +84,6 @@ describe( 'go-deactivate-modal', () => {
8284
jest.clearAllMocks();
8385
} );
8486

85-
describe( 'fetchData', () => {
86-
test( 'should return feedback data', async () => {
87-
const response = await fetchData( 'foo.com' );
88-
expect( response ).toEqual( mockData );
89-
} );
90-
91-
test( 'should call the fetch api', async () => {
92-
const fetchMock = jest.spyOn( global, 'fetch' );
93-
94-
await act( async () => {
95-
wrapper = setup();
96-
} );
97-
98-
expect( fetchMock ).toHaveBeenCalledWith( 'https://wpnux.godaddy.com/v3/api/feedback/go-theme-optout?domain=foo.com&language=en-US&random=1' );
99-
} );
100-
} );
101-
10287
describe( 'closed state', () => {
10388
beforeEach( async () => {
10489
await act( async () => {

.dev/assets/admin/js/go-deactivate-modal.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ const GoDeactivateModal = () => {
2424
return null;
2525
}
2626

27-
const getParams = { domain };
28-
2927
return (
3028
<DeactivateModal
3129
apiUrl={ apiUrl }
32-
getParams={ getParams }
3330
isEvent={ isEvent }
3431
pageData={ goThemeDeactivateData }
3532
/>

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
= 1.8.14 / 2025-04-07 =
2+
### Updates
3+
- Refactor the deactivation modal. [#927](https://github.com/godaddy-wordpress/go/pull/927)
4+
15
= 1.8.13 / 2025-04-01 =
26
### Updates
37
- Avoid adding onload attribute to stylesheet when serving AMP response. [#917](https://github.com/godaddy-wordpress/go/pull/917)

includes/classes/admin/class-go-theme-deactivation.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,67 @@ public function enqueue_scripts( $hook_suffix ) {
5757

5858
$wpnux_export_data = json_decode( get_option( 'wpnux_export_data', '[]' ), true );
5959

60+
$deactivation_form_choices = array(
61+
array(
62+
'slug' => 'broken-or-breaks-plugins',
63+
'label' => "It's broken or broke other plugins I'm using",
64+
'is_checked' => false,
65+
'text_field' => null,
66+
),
67+
array(
68+
'slug' => 'lack-of-support',
69+
'label' => 'Lack of help or support from GoDaddy',
70+
'is_checked' => false,
71+
'text_field' => null,
72+
),
73+
array(
74+
'slug' => 'not-enough-features',
75+
'label' => "It doesn't have the features I am looking for",
76+
'is_checked' => false,
77+
'text_field' => 'choice_not-enough-features_text',
78+
),
79+
array(
80+
'slug' => 'not-responsive',
81+
'label' => "It isn't responsive to screen size",
82+
'is_checked' => false,
83+
'text_field' => null,
84+
),
85+
array(
86+
'slug' => 'slow-website-performance',
87+
'label' => "It slows down my website's speed/load time",
88+
'is_checked' => false,
89+
'text_field' => null,
90+
),
91+
);
92+
93+
// Randomize the choices.
94+
shuffle( $deactivation_form_choices );
95+
96+
// Ensure "Other Reason" is last.
97+
$deactivation_form_choices[] = array(
98+
'slug' => 'other',
99+
'label' => 'Other reason:',
100+
'is_checked' => false,
101+
'text_field' => 'choice_other_text',
102+
);
103+
104+
$deactivation_labels = array(
105+
'privacy_disclaimer' => __( "Please do not include any personal information in your submission. We do not collect or need this information. Please see our <a href='https://www.godaddy.com/legal/agreements/privacy-policy' target='_blank'>privacy policy</a> for details.", 'go' ),
106+
'skip_deactivate' => __( 'Skip & Switch Theme', 'go' ),
107+
'submit_deactivate' => __( 'Submit & Switch Theme', 'go' ),
108+
'title' => __( 'Thanks for trying Go. Let us know how we can improve.', 'go' ),
109+
);
110+
60111
// Pass data.
61112
wp_localize_script(
62113
'go-theme-deactivation',
63114
'goThemeDeactivateData',
64115
array(
65116
'containerId' => self::CONTAINER_ID,
66117
'hostname' => gethostname(),
118+
// Localize options here.
119+
'choices' => $deactivation_form_choices,
120+
'labels' => $deactivation_labels,
67121
'domain' => site_url(),
68122
'goThemeVersion' => GO_VERSION,
69123
'wpVersion' => $GLOBALS['wp_version'],

languages/go.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"c1b5fa03ecdb95d4a45dd1c40b02527f": "Horizontal",
1818
"106aecf314c1888e3d36a4edc6f5de01": "collapse child menu",
1919
"c6bbcc7f5459e2a48fa1be3c32bb25af": "expand child menu",
20+
"39fbcc1a20fd4bdedb4aa302e71653be": "Please do not include any personal information in your submission. We do not collect or need this information. Please see our <a href='https://www.godaddy.com/legal/agreements/privacy-policy' target='_blank'>privacy policy</a> for details.",
21+
"209f7a934bf1e37cbd2d97e6e4d04822": "Skip & Switch Theme",
22+
"0f9b6d331e364f21e0c92aacc67a43a3": "Submit & Switch Theme",
23+
"c73a52561d708b6e855945a4d6f8c6ef": "Thanks for trying Go. Let us know how we can improve.",
2024
"8d496fe225b423ae91cafb2913fe60fc": "Install Required Plugins",
2125
"4eff3a34f0ddf963f7d0610d3cb79354": "Install Plugins",
2226
"07867e5fd7b858b877323ffd402ac38e": "Installing Plugin: %s",

0 commit comments

Comments
 (0)