feat: Add chatwoot:ready event listener on window (#1091)
* feat: Add `chatwoot:ready` event listener on window * Add specs * Rename customEventHelper.js -> CustomEventHelper.js
This commit is contained in:
@@ -13,6 +13,9 @@ import {
|
||||
onClickChatBubble,
|
||||
onBubbleClick,
|
||||
} from './bubbleHelpers';
|
||||
import { dispatchWindowEvent } from 'shared/helpers/CustomEventHelper';
|
||||
|
||||
const EVENT_NAME = 'chatwoot:ready';
|
||||
|
||||
export const IFrameHelper = {
|
||||
getUrl({ baseUrl, websiteToken }) {
|
||||
@@ -89,6 +92,7 @@ export const IFrameHelper = {
|
||||
loaded: message => {
|
||||
Cookies.set('cw_conversation', message.config.authToken, {
|
||||
expires: 365,
|
||||
sameSite: 'Lax',
|
||||
});
|
||||
window.$chatwoot.hasLoaded = true;
|
||||
IFrameHelper.sendMessage('config-set', {
|
||||
@@ -103,6 +107,7 @@ export const IFrameHelper = {
|
||||
if (window.$chatwoot.user) {
|
||||
IFrameHelper.sendMessage('set-user', window.$chatwoot.user);
|
||||
}
|
||||
dispatchWindowEvent(EVENT_NAME);
|
||||
},
|
||||
|
||||
toggleBubble: () => {
|
||||
|
||||
15
app/javascript/shared/helpers/CustomEventHelper.js
Normal file
15
app/javascript/shared/helpers/CustomEventHelper.js
Normal file
@@ -0,0 +1,15 @@
|
||||
export const createEvent = eventName => {
|
||||
let event;
|
||||
if (typeof window.CustomEvent === 'function') {
|
||||
event = new CustomEvent(eventName);
|
||||
} else {
|
||||
event = document.createEvent('CustomEvent');
|
||||
event.initCustomEvent(eventName, false, false, null);
|
||||
}
|
||||
return event;
|
||||
};
|
||||
|
||||
export const dispatchWindowEvent = eventName => {
|
||||
const event = createEvent(eventName);
|
||||
window.dispatchEvent(event);
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { dispatchWindowEvent } from '../CustomEventHelper';
|
||||
|
||||
describe('dispatchWindowEvent', () => {
|
||||
it('dispatches correct event', () => {
|
||||
window.dispatchEvent = jest.fn();
|
||||
dispatchWindowEvent('chatwoot:ready');
|
||||
expect(dispatchEvent).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user