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} ;
0 commit comments