Skip to content

Commit 311d175

Browse files
committed
add generic error message when interactive task fails
1 parent a529420 commit 311d175

3 files changed

Lines changed: 92 additions & 29 deletions

File tree

assets/js/recommendations/interactive-task.js

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global prplSuggestedTask, progressPlannerAjaxRequest, progressPlanner */
1+
/* global prplSuggestedTask, progressPlannerAjaxRequest, progressPlanner, prplL10n */
22

33
/*
44
* Core Blog Description recommendation.
@@ -85,9 +85,13 @@ const prplInteractiveTaskFormListener = {
8585

8686
callback()
8787
.then( ( response ) => {
88-
console.log( response );
8988
if ( true !== response.success ) {
90-
// TODO: Handle error.
89+
// Show error to the user.
90+
prplInteractiveTaskFormListener.showError(
91+
response,
92+
popoverId
93+
);
94+
9195
return response;
9296
}
9397

@@ -107,11 +111,11 @@ const prplInteractiveTaskFormListener = {
107111
} );
108112
} )
109113
.catch( ( error ) => {
110-
console.error(
111-
'Error in interactive task callback:',
112-
error
114+
// Show error to the user.
115+
prplInteractiveTaskFormListener.showError(
116+
error,
117+
popoverId
113118
);
114-
// TODO: Handle the error appropriately.
115119
} );
116120
} );
117121
},
@@ -153,7 +157,12 @@ const prplInteractiveTaskFormListener = {
153157
.then( ( response ) => {
154158
console.log( response );
155159
if ( true !== response.success ) {
156-
// TODO: Handle error.
160+
// Show error to the user.
161+
prplInteractiveTaskFormListener.showError(
162+
response,
163+
popoverId
164+
);
165+
157166
return response;
158167
}
159168

@@ -177,12 +186,57 @@ const prplInteractiveTaskFormListener = {
177186
} );
178187
} )
179188
.catch( ( error ) => {
180-
console.error(
181-
'Error in interactive task settings:',
182-
error
189+
// Show error to the user.
190+
prplInteractiveTaskFormListener.showError(
191+
error,
192+
popoverId
183193
);
184-
// TODO: Handle the error appropriately.
185194
} );
186195
} );
187196
},
197+
198+
/**
199+
* Helper which shows user an error message.
200+
* For now the error message is generic.
201+
*
202+
* @param {Object} error - The error object.
203+
* @param {string} popoverId - The ID of the popover.
204+
* @return {void}
205+
*/
206+
showError: ( error, popoverId ) => {
207+
const formElement = document.querySelector( `#${ popoverId } form` );
208+
209+
if ( ! formElement ) {
210+
return;
211+
}
212+
213+
console.error( 'Error in interactive task callback:', error );
214+
215+
// Add error message.
216+
const submitButton = formElement.querySelector(
217+
'button[type="submit"]'
218+
);
219+
220+
if (
221+
submitButton &&
222+
! formElement.querySelector(
223+
'.prpl-interactive-task-error-message'
224+
)
225+
) {
226+
// Add paragraph with error message.
227+
const errorParagraph = document.createElement( 'p' );
228+
errorParagraph.classList.add(
229+
'prpl-note',
230+
'prpl-note-error',
231+
'prpl-interactive-task-error-message'
232+
);
233+
errorParagraph.textContent = prplL10n( 'somethingWentWrong' );
234+
235+
// Append before submit button.
236+
submitButton.parentNode.insertBefore(
237+
errorParagraph,
238+
submitButton
239+
);
240+
}
241+
},
188242
};

assets/js/recommendations/set-date-format.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,32 @@ prplInteractiveTaskFormListener.customSubmit( {
1010
taskId: 'set-date-format',
1111
popoverId: 'prpl-popover-set-date-format',
1212
callback: () => {
13-
const format = document.querySelector(
14-
'#prpl-popover-set-date-format input[name="date_format"]:checked'
15-
);
16-
const customFormat = document.querySelector(
17-
'#prpl-popover-set-date-format input[name="date_format_custom"]'
18-
);
13+
return new Promise( ( resolve, reject ) => {
14+
const format = document.querySelector(
15+
'#prpl-popover-set-date-format input[name="date_format"]:checked'
16+
);
17+
const customFormat = document.querySelector(
18+
'#prpl-popover-set-date-format input[name="date_format_custom"]'
19+
);
1920

20-
fetch( progressPlanner.ajaxUrl, {
21-
method: 'POST',
22-
headers: {
23-
'Content-Type': 'application/x-www-form-urlencoded',
24-
},
25-
body: new URLSearchParams( {
26-
action: 'prpl_interactive_task_submit_set-date-format',
27-
nonce: progressPlanner.nonce,
28-
date_format: format.value,
29-
date_format_custom: customFormat.value,
30-
} ),
21+
fetch( progressPlanner.ajaxUrl, {
22+
method: 'POST',
23+
headers: {
24+
'Content-Type': 'application/x-www-form-urlencoded',
25+
},
26+
body: new URLSearchParams( {
27+
action: 'prpl_interactive_task_submit_set-date-format',
28+
nonce: progressPlanner.nonce,
29+
date_format: format.value,
30+
date_format_custom: customFormat.value,
31+
} ),
32+
} )
33+
.then( ( response ) => {
34+
resolve( { response, success: true } );
35+
} )
36+
.catch( ( error ) => {
37+
reject( { success: false, error } );
38+
} );
3139
} );
3240
},
3341
} );

classes/admin/class-enqueue.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ public function get_localized_strings() {
381381
'installed' => \esc_html__( 'Installed', 'progress-planner' ),
382382
'activating' => \esc_html__( 'Activating...', 'progress-planner' ),
383383
'activated' => \esc_html__( 'Activated', 'progress-planner' ),
384+
'somethingWentWrong' => \esc_html__( 'Something went wrong.', 'progress-planner' ),
384385
];
385386
}
386387

0 commit comments

Comments
 (0)