Skip to content

Commit 2fba233

Browse files
committed
ref: change/add some callbacks
1 parent 7053828 commit 2fba233

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

packages/feedback/src/integration.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Feedback implements Integration {
7878
/**
7979
* Tracks if dialog has ever been opened at least one time
8080
*/
81-
private _hasDialogOpened: boolean;
81+
private hasDialogEverOpened: boolean;
8282

8383
public constructor({
8484
attachTo = null,
@@ -108,7 +108,10 @@ export class Feedback implements Integration {
108108
nameLabel = NAME_LABEL,
109109
successMessageText = SUCCESS_MESSAGE_TEXT,
110110

111-
onOpenDialog,
111+
onActorClick,
112+
onDialogOpen,
113+
onSubmitError,
114+
onSubmitSuccess,
112115
}: FeedbackConfiguration = {}) {
113116
// Initializations
114117
this.name = Feedback.id;
@@ -117,7 +120,7 @@ export class Feedback implements Integration {
117120
this._host = null;
118121
this._shadow = null;
119122
this._isDialogOpen = false;
120-
this._hasDialogOpened = false;
123+
this.hasDialogEverOpened = false;
121124

122125
this.options = {
123126
attachTo,
@@ -147,7 +150,10 @@ export class Feedback implements Integration {
147150
namePlaceholder,
148151
successMessageText,
149152

150-
onOpenDialog,
153+
onActorClick,
154+
onDialogOpen,
155+
onSubmitError,
156+
onSubmitSuccess,
151157
};
152158

153159
// TOOD: temp for testing;
@@ -231,6 +237,9 @@ export class Feedback implements Integration {
231237
if (this._dialog) {
232238
this._dialog.open();
233239
this._isDialogOpen = true;
240+
if (this.options.onDialogOpened) {
241+
this.options.onDialogOpened();
242+
}
234243
return;
235244
}
236245

@@ -241,7 +250,7 @@ export class Feedback implements Integration {
241250
}
242251

243252
// Lazy-load until dialog is opened and only inject styles once
244-
if (!this._hasDialogOpened) {
253+
if (!this.hasDialogEverOpened) {
245254
this._shadow.appendChild(createDialogStyles(document));
246255
}
247256

@@ -252,7 +261,7 @@ export class Feedback implements Integration {
252261
this._dialog = Dialog({
253262
defaultName: (userKey && user && user[userKey.name]) || '',
254263
defaultEmail: (userKey && user && user[userKey.email]) || '',
255-
onClose: () => {
264+
onClosed: () => {
256265
this.showActor();
257266
this._isDialogOpen = false;
258267
},
@@ -268,7 +277,10 @@ export class Feedback implements Integration {
268277
// Hides the default actor whenever dialog is opened
269278
this._actor && this._actor.hide();
270279

271-
this._hasDialogOpened = true;
280+
this.hasDialogEverOpened = true;
281+
if (this.options.onDialogOpened) {
282+
this.options.onDialogOpened();
283+
}
272284
} catch (err) {
273285
// TODO: Error handling?
274286
console.error(err);
@@ -401,8 +413,8 @@ export class Feedback implements Integration {
401413
this._actor.hide();
402414
}
403415

404-
if (this.options.onOpenDialog) {
405-
this.options.onOpenDialog();
416+
if (this.options.onActorClick) {
417+
this.options.onActorClick();
406418
}
407419
};
408420

@@ -413,10 +425,21 @@ export class Feedback implements Integration {
413425
protected _handleFeedbackSubmit = async (feedback: FeedbackFormData): Promise<void> => {
414426
const result = await handleFeedbackSubmit(this._dialog, feedback);
415427

428+
// Error submitting feedback
429+
if (!result) {
430+
if (this.options.onSubmitError) {
431+
this.options.onSubmitError();
432+
}
433+
434+
return;
435+
}
436+
416437
// Success
417-
if (result) {
418-
this.removeDialog();
419-
this._showSuccessMessage();
438+
this.removeDialog();
439+
this._showSuccessMessage();
440+
441+
if (this.options.onSubmitSuccess) {
442+
this.options.onSubmitSuccess();
420443
}
421444
};
422445
}

packages/feedback/src/types/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,25 @@ export interface FeedbackConfigurationWithDefaults {
143143
// * End of text customization * //
144144

145145
// * Start of Callbacks * //
146-
onOpenDialog?: () => void;
146+
/**
147+
* Callback when dialog is opened
148+
*/
149+
onDialogOpen?: () => void;
150+
151+
/**
152+
* Callback when widget actor is clicked
153+
*/
154+
onActorClick?: () => void;
155+
156+
/**
157+
* Callback when feedback is successfully submitted
158+
*/
159+
onSubmitSuccess?: () => void;
160+
161+
/**
162+
* Callback when feedback is unsuccessfully submitted
163+
*/
164+
onSubmitError?: () => void;
147165
// * End of Callbacks * //
148166
}
149167

packages/feedback/src/widget/Dialog.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface DialogProps {
66
defaultName: string;
77
defaultEmail: string;
88
onCancel?: (e: Event) => void;
9-
onClose?: () => void;
9+
onClosed?: () => void;
1010
onSubmit?: (feedback: FeedbackFormData) => void;
1111
options: FeedbackConfigurationWithDefaults;
1212
}
@@ -49,7 +49,7 @@ export interface DialogComponent extends FeedbackComponent<HTMLDialogElement> {
4949
export function Dialog({
5050
defaultName,
5151
defaultEmail,
52-
onClose,
52+
onClosed,
5353
onCancel,
5454
onSubmit,
5555
options,
@@ -66,7 +66,7 @@ export function Dialog({
6666

6767
// Only this should trigger `onClose`, we don't want the `close()` method to
6868
// trigger it, otherwise it can cause cycles.
69-
onClose && onClose();
69+
onClosed && onClosed();
7070
}
7171

7272
/**

0 commit comments

Comments
 (0)