@@ -8,7 +8,6 @@ import { sendFeedback } from '../sendFeedback';
88import type { DialogComponent , FeedbackFormData , FeedbackInternalOptions , FeedbackWidget } from '../types' ;
99import type { ActorComponent } from './Actor' ;
1010import { Actor } from './Actor' ;
11- import { SuccessMessage } from './SuccessMessage' ;
1211import { loadDialog } from './loadDialog' ;
1312
1413interface CreateWidgetParams {
@@ -44,79 +43,6 @@ export function createWidget({
4443 let actor : ActorComponent | undefined ;
4544 let dialog : DialogComponent | undefined ;
4645
47- /**
48- * Show the success message for 5 seconds
49- */
50- function showSuccessMessage ( ) : void {
51- if ( ! shadow ) {
52- return ;
53- }
54-
55- try {
56- const success = SuccessMessage ( {
57- message : options . successMessageText ,
58- onRemove : ( ) => {
59- if ( timeoutId ) {
60- clearTimeout ( timeoutId ) ;
61- }
62- showActor ( ) ;
63- } ,
64- } ) ;
65-
66- if ( ! success . el ) {
67- throw new Error ( 'Unable to show success message' ) ;
68- }
69-
70- shadow . appendChild ( success . el ) ;
71-
72- const timeoutId = setTimeout ( ( ) => {
73- if ( success ) {
74- success . remove ( ) ;
75- }
76- } , 5000 ) ;
77- } catch ( err ) {
78- // TODO: error handling
79- logger . error ( err ) ;
80- console . log ( err ) ; // eslint-disable-line no-console
81- }
82- }
83-
84- /**
85- * Handler for when the feedback form is completed by the user. This will
86- * create and send the feedback message as an event.
87- */
88- async function _handleFeedbackSubmit ( feedback : FeedbackFormData ) : Promise < void > {
89- if ( ! dialog ) {
90- return ;
91- }
92-
93- const missingFields = getMissingFields ( feedback , options ) ;
94- if ( missingFields . length > 0 ) {
95- dialog . showError ( `Please enter in the following required fields: ${ missingFields . join ( ', ' ) } ` ) ;
96- return ;
97- }
98-
99- dialog . hideError ( ) ;
100- try {
101- await sendFeedback ( { ...feedback , source : FEEDBACK_WIDGET_SOURCE } ) ;
102- } catch ( err ) {
103- DEBUG_BUILD && logger . error ( err ) ;
104- dialog . showError ( 'There was a problem submitting feedback, please wait and try again.' ) ;
105- if ( options . onSubmitError ) {
106- options . onSubmitError ( ) ;
107- }
108- return ;
109- }
110-
111- // Success
112- removeDialog ( ) ;
113- showSuccessMessage ( ) ;
114-
115- if ( options . onSubmitSuccess ) {
116- options . onSubmitSuccess ( ) ;
117- }
118- }
119-
12046 /**
12147 * Displays the default actor
12248 */
@@ -157,7 +83,7 @@ export function createWidget({
15783 const user = scope && scope . getUser ( ) ;
15884
15985 loadDialog ( { screenshots : false } )
160- . then ( Dialog => {
86+ . then ( ( { Dialog, showSuccessMessage } ) => {
16187 dialog = Dialog ( {
16288 colorScheme : options . colorScheme ,
16389 showBranding : options . showBranding ,
@@ -180,7 +106,35 @@ export function createWidget({
180106 closeDialog ( ) ;
181107 showActor ( ) ;
182108 } ,
183- onSubmit : _handleFeedbackSubmit ,
109+ onSubmit : async function _handleFeedbackSubmit ( feedback : FeedbackFormData ) : Promise < void > {
110+ if ( ! dialog ) {
111+ return ;
112+ }
113+
114+ const missingFields = getMissingFields ( feedback , options ) ;
115+ if ( missingFields . length > 0 ) {
116+ dialog . showError ( `Please enter in the following required fields: ${ missingFields . join ( ', ' ) } ` ) ;
117+ return ;
118+ }
119+
120+ dialog . hideError ( ) ;
121+ try {
122+ await sendFeedback ( { ...feedback , source : FEEDBACK_WIDGET_SOURCE } ) ;
123+ } catch ( err ) {
124+ DEBUG_BUILD && logger . error ( err ) ;
125+ dialog . showError ( 'There was a problem submitting feedback, please wait and try again.' ) ;
126+ if ( options . onSubmitError ) {
127+ options . onSubmitError ( ) ;
128+ }
129+ return ;
130+ }
131+
132+ removeDialog ( ) ;
133+ showSuccessMessage ( shadow , options , showActor ) ;
134+ if ( options . onSubmitSuccess ) {
135+ options . onSubmitSuccess ( ) ;
136+ }
137+ } ,
184138 } ) ;
185139
186140 if ( ! dialog . el ) {
0 commit comments