@@ -17,6 +17,11 @@ customElements.define(
1717 // Get parent class properties
1818 super ( ) ;
1919 this . repositionPopover = this . repositionPopover . bind ( this ) ; // So this is available in the event listener.
20+
21+ // First step.
22+ this . formStep = this . querySelector (
23+ '#prpl-sending-email-form-step'
24+ ) ;
2025 }
2126
2227 /**
@@ -55,83 +60,200 @@ customElements.define(
5560 popover . style . transform = 'translateX(-50%)' ;
5661 }
5762
63+ /**
64+ * Runs when the popover is added to the DOM.
65+ */
66+ popoverAddedToDOM ( ) {
67+ window . addEventListener ( 'resize' , this . repositionPopover ) ;
68+
69+ // For the results step, add event listener to radio buttons.
70+ const nextButton = this . querySelector (
71+ '#prpl-sending-email-result-step .prpl-steps-nav-wrapper .prpl-button'
72+ ) ;
73+
74+ if ( nextButton ) {
75+ this . querySelectorAll (
76+ 'input[name="prpl-sending-email-result"]'
77+ ) . forEach ( ( input ) => {
78+ input . addEventListener ( 'change' , ( event ) => {
79+ nextButton . setAttribute (
80+ 'data-action' ,
81+ event . target . getAttribute ( 'data-action' )
82+ ) ;
83+ } ) ;
84+ } ) ;
85+ }
86+ }
87+
5888 /**
5989 * Runs when the popover is opening.
6090 */
6191 popoverOpening ( ) {
6292 this . repositionPopover ( ) ;
63- window . addEventListener ( 'resize' , this . repositionPopover ) ;
6493 }
6594
6695 /**
67- * Runs when the popover is closing .
96+ * Hide all steps .
6897 */
69- popoverClosing ( ) {
70- window . removeEventListener ( 'resize' , this . repositionPopover ) ;
98+ hideAllSteps ( ) {
99+ this . querySelectorAll ( '.prpl-sending-email-step' ) . forEach (
100+ ( step ) => {
101+ step . style . display = 'none' ;
102+ }
103+ ) ;
104+ }
105+
106+ /**
107+ * Show the form (first step).
108+ */
109+ showForm ( ) {
110+ this . hideAllSteps ( ) ;
111+
112+ this . formStep . style . display = 'flex' ;
71113 }
72114
73115 /**
74116 * Show the results.
75117 */
76118 showResults ( ) {
77- const nextButton = this . querySelector (
78- '#prpl-sending-email-result .prpl-steps-nav-wrapper .prpl-button'
119+ const resultsStep = this . querySelector (
120+ '#prpl-sending-email-result-step'
121+ ) ;
122+
123+ const emailAddress = this . querySelector (
124+ '#prpl-sending-email-address'
79125 ) ;
80- const form = this . querySelector ( '#prpl-sending-email-form' ) ;
81- const results = this . querySelector ( '#prpl-sending-email-result' ) ;
126+
127+ // Update result message with the email address.
128+ let resultMessageText = resultsStep
129+ . querySelector ( '#prpl-sending-email-sent-message' )
130+ . getAttribute ( 'data-email-message' ) ;
131+
132+ // Replace the placeholder with the email address.
133+ resultMessageText = resultMessageText . replace (
134+ '[EMAIL_ADDRESS]' ,
135+ emailAddress . value
136+ ) ;
137+
138+ // Replace the placeholder with the error message.
139+ resultsStep . querySelector (
140+ '#prpl-sending-email-sent-message'
141+ ) . textContent = resultMessageText ;
82142
83143 // Make AJAX GET request.
84- fetch ( prplEmailSending . ajax_url + '?action=test_email_sending' )
144+ fetch (
145+ prplEmailSending . ajax_url +
146+ '?action=prpl_test_email_sending&_wpnonce=' +
147+ prplEmailSending . nonce +
148+ '&email_address=' +
149+ emailAddress . value
150+ )
85151 . then ( ( response ) => response . json ( ) )
86152 // eslint-disable-next-line no-unused-vars
87- . then ( ( data ) => {
88- form . style . display = 'none' ;
89- results . style . display = 'block' ;
153+ . then ( ( response ) => {
154+ if ( true === response . success ) {
155+ this . formStep . style . display = 'none' ;
156+ resultsStep . style . display = 'flex' ;
157+ } else {
158+ this . showErrorOccurred ( response . data ) ;
159+ }
90160 } )
91161 . catch ( ( error ) => {
92162 console . error ( 'Error testing email:' , error ) ; // eslint-disable-line no-console
93- this . showTroubleshooting ( ) ;
163+ this . showErrorOccurred ( error . message ) ;
94164 } ) ;
165+ }
95166
96- // Add event listener to radio buttons.
97- this . querySelectorAll (
98- 'input[name="prpl-sending-email-result"]'
99- ) . forEach ( ( input ) => {
100- input . addEventListener ( 'change' , ( event ) => {
101- console . log ( event . target . getAttribute ( 'data-action' ) ) ;
102- nextButton . setAttribute (
103- 'data-action' ,
104- event . target . getAttribute ( 'data-action' )
105- ) ;
106- } ) ;
107- } ) ;
167+ /**
168+ * Show the error occurred.
169+ * @param {string } errorMessageReason
170+ */
171+ showErrorOccurred ( errorMessageReason = '' ) {
172+ if ( ! errorMessageReason ) {
173+ errorMessageReason = prplEmailSending . unknown_error ;
174+ }
175+
176+ const errorOccurredStep = this . querySelector (
177+ '#prpl-sending-email-error-occurred-step'
178+ ) ;
179+
180+ // Replace the placeholder with the email address (text in the left column).
181+ const emailAddress = this . querySelector (
182+ '#prpl-sending-email-address'
183+ ) . value ;
184+
185+ // Get the error message text.
186+ const errorMessageText = errorOccurredStep
187+ . querySelector ( '#prpl-sending-email-error-occurred-message' )
188+ . getAttribute ( 'data-email-message' ) ;
189+
190+ // Replace the placeholder with the email address.
191+ errorOccurredStep . querySelector (
192+ '#prpl-sending-email-error-occurred-message'
193+ ) . textContent = errorMessageText . replace (
194+ '[EMAIL_ADDRESS]' ,
195+ emailAddress
196+ ) ;
197+
198+ // Replace the placeholder with the error message (text in the right column).
199+ const errorMessageNotification = errorOccurredStep . querySelector (
200+ '.prpl-note.prpl-note-error .prpl-note-text'
201+ ) ;
202+ const errorMessageNotificationText =
203+ errorMessageNotification . getAttribute ( 'data-email-message' ) ;
204+
205+ errorMessageNotification . textContent =
206+ errorMessageNotificationText . replace (
207+ '[ERROR_MESSAGE]' ,
208+ errorMessageReason
209+ ) ;
210+
211+ // Hide form step.
212+ this . formStep . style . display = 'none' ;
213+
214+ // Show error occurred step.
215+ errorOccurredStep . style . display = 'flex' ;
216+ }
217+
218+ /**
219+ * Show the troubleshooting.
220+ */
221+ showSuccess ( ) {
222+ this . hideAllSteps ( ) ;
223+
224+ this . querySelector (
225+ '#prpl-sending-email-success-step'
226+ ) . style . display = 'flex' ;
108227 }
109228
110229 /**
111230 * Show the troubleshooting.
112231 */
113232 showTroubleshooting ( ) {
114- const results = this . querySelector ( '#prpl-sending-email-result' ) ;
115- const troubleshooting = this . querySelector (
116- '#prpl-sending-email-troubleshooting'
117- ) ;
118- results . style . display = 'none' ;
119- troubleshooting . style . display = 'block' ;
233+ this . hideAllSteps ( ) ;
234+
235+ this . querySelector (
236+ '#prpl-sending-email-troubleshooting-step'
237+ ) . style . display = 'flex' ;
120238 }
121239
122240 /**
123- * Reset the popover .
241+ * Open the troubleshooting guide .
124242 */
125- resetPopover ( ) {
126- const form = this . querySelector ( '#prpl-sending-email-form' ) ;
127- const results = this . querySelector ( '#prpl-sending-email-result' ) ;
128- const troubleshooting = this . querySelector (
129- '#prpl-sending-email-troubleshooting'
130- ) ;
243+ openTroubleshootingGuide ( ) {
244+ // Open the troubleshooting guide in a new tab.
245+ window . open ( prplEmailSending . troubleshooting_guide_url , '_blank' ) ;
246+
247+ // Close the popover.
248+ this . closePopover ( ) ;
249+ }
131250
132- form . style . display = 'block' ;
133- results . style . display = 'none' ;
134- troubleshooting . style . display = 'none' ;
251+ /**
252+ * Popover closing, reset the layout, values, etc.
253+ */
254+ popoverClosing ( ) {
255+ // Hide all steps and show the first step.
256+ this . showForm ( ) ;
135257
136258 // Reset radio buttons.
137259 this . querySelectorAll (
0 commit comments