feat: Add chatwoot:error
sdk event (#3998)
This commit is contained in:
parent
3b015d4dd8
commit
7ba24b90c4
12 changed files with 59 additions and 29 deletions
|
@ -2,17 +2,12 @@ import Cookies from 'js-cookie';
|
||||||
import { IFrameHelper } from '../sdk/IFrameHelper';
|
import { IFrameHelper } from '../sdk/IFrameHelper';
|
||||||
import { getBubbleView } from '../sdk/bubbleHelpers';
|
import { getBubbleView } from '../sdk/bubbleHelpers';
|
||||||
import md5 from 'md5';
|
import md5 from 'md5';
|
||||||
|
import { getUserCookieName } from '../sdk/cookieHelpers';
|
||||||
|
|
||||||
const REQUIRED_USER_KEYS = ['avatar_url', 'email', 'name'];
|
const REQUIRED_USER_KEYS = ['avatar_url', 'email', 'name'];
|
||||||
|
|
||||||
const ALLOWED_USER_ATTRIBUTES = [...REQUIRED_USER_KEYS, 'identifier_hash'];
|
const ALLOWED_USER_ATTRIBUTES = [...REQUIRED_USER_KEYS, 'identifier_hash'];
|
||||||
|
|
||||||
export const getUserCookieName = () => {
|
|
||||||
const SET_USER_COOKIE_PREFIX = 'cw_user_';
|
|
||||||
const { websiteToken: websiteIdentifier } = window.$chatwoot;
|
|
||||||
return `${SET_USER_COOKIE_PREFIX}${websiteIdentifier}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getUserString = ({ identifier = '', user }) => {
|
export const getUserString = ({ identifier = '', user }) => {
|
||||||
const userStringWithSortedKeys = ALLOWED_USER_ATTRIBUTES.reduce(
|
const userStringWithSortedKeys = ALLOWED_USER_ATTRIBUTES.reduce(
|
||||||
(acc, key) => `${acc}${key}${user[key] || ''}`,
|
(acc, key) => `${acc}${key}${user[key] || ''}`,
|
||||||
|
|
|
@ -23,8 +23,9 @@ import {
|
||||||
removeUnreadClass,
|
removeUnreadClass,
|
||||||
} from './bubbleHelpers';
|
} from './bubbleHelpers';
|
||||||
import { dispatchWindowEvent } from 'shared/helpers/CustomEventHelper';
|
import { dispatchWindowEvent } from 'shared/helpers/CustomEventHelper';
|
||||||
|
import { CHATWOOT_ERROR, CHATWOOT_READY } from '../widget/constants/sdkEvents';
|
||||||
const EVENT_NAME = 'chatwoot:ready';
|
import { SET_USER_ERROR } from '../widget/constants/errorTypes';
|
||||||
|
import { getUserCookieName } from './cookieHelpers';
|
||||||
|
|
||||||
export const IFrameHelper = {
|
export const IFrameHelper = {
|
||||||
getUrl({ baseUrl, websiteToken }) {
|
getUrl({ baseUrl, websiteToken }) {
|
||||||
|
@ -129,7 +130,14 @@ export const IFrameHelper = {
|
||||||
if (window.$chatwoot.user) {
|
if (window.$chatwoot.user) {
|
||||||
IFrameHelper.sendMessage('set-user', window.$chatwoot.user);
|
IFrameHelper.sendMessage('set-user', window.$chatwoot.user);
|
||||||
}
|
}
|
||||||
dispatchWindowEvent(EVENT_NAME);
|
dispatchWindowEvent({ eventName: CHATWOOT_READY });
|
||||||
|
},
|
||||||
|
error: ({ errorType, data }) => {
|
||||||
|
dispatchWindowEvent({ eventName: CHATWOOT_ERROR, data: data });
|
||||||
|
|
||||||
|
if (errorType === SET_USER_ERROR) {
|
||||||
|
Cookies.remove(getUserCookieName());
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setBubbleLabel(message) {
|
setBubbleLabel(message) {
|
||||||
|
|
5
app/javascript/sdk/cookieHelpers.js
Normal file
5
app/javascript/sdk/cookieHelpers.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
export const getUserCookieName = () => {
|
||||||
|
const SET_USER_COOKIE_PREFIX = 'cw_user_';
|
||||||
|
const { websiteToken: websiteIdentifier } = window.$chatwoot;
|
||||||
|
return `${SET_USER_COOKIE_PREFIX}${websiteIdentifier}`;
|
||||||
|
};
|
8
app/javascript/sdk/specs/cookieHelpers.spec.js
Normal file
8
app/javascript/sdk/specs/cookieHelpers.spec.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { getUserCookieName } from '../cookieHelpers';
|
||||||
|
|
||||||
|
describe('#getUserCookieName', () => {
|
||||||
|
it('returns correct cookie name', () => {
|
||||||
|
global.$chatwoot = { websiteToken: '123456' };
|
||||||
|
expect(getUserCookieName()).toBe('cw_user_123456');
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,15 +1,15 @@
|
||||||
export const createEvent = eventName => {
|
export const createEvent = ({ eventName, data = null }) => {
|
||||||
let event;
|
let event;
|
||||||
if (typeof window.CustomEvent === 'function') {
|
if (typeof window.CustomEvent === 'function') {
|
||||||
event = new CustomEvent(eventName);
|
event = new CustomEvent(eventName, { detail: data });
|
||||||
} else {
|
} else {
|
||||||
event = document.createEvent('CustomEvent');
|
event = document.createEvent('CustomEvent');
|
||||||
event.initCustomEvent(eventName, false, false, null);
|
event.initCustomEvent(eventName, false, false, data);
|
||||||
}
|
}
|
||||||
return event;
|
return event;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const dispatchWindowEvent = eventName => {
|
export const dispatchWindowEvent = ({ eventName, data }) => {
|
||||||
const event = createEvent(eventName);
|
const event = createEvent({ eventName, data });
|
||||||
window.dispatchEvent(event);
|
window.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { dispatchWindowEvent } from '../CustomEventHelper';
|
||||||
describe('dispatchWindowEvent', () => {
|
describe('dispatchWindowEvent', () => {
|
||||||
it('dispatches correct event', () => {
|
it('dispatches correct event', () => {
|
||||||
window.dispatchEvent = jest.fn();
|
window.dispatchEvent = jest.fn();
|
||||||
dispatchWindowEvent('chatwoot:ready');
|
dispatchWindowEvent({ eventName: 'chatwoot:ready' });
|
||||||
expect(dispatchEvent).toHaveBeenCalled();
|
expect(dispatchEvent).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,11 +1,4 @@
|
||||||
import { getUserCookieName, getUserString, hasUserKeys } from '../../packs/sdk';
|
import { getUserString, hasUserKeys } from '../../packs/sdk';
|
||||||
|
|
||||||
describe('#getUserCookieName', () => {
|
|
||||||
it('returns correct cookie name', () => {
|
|
||||||
global.$chatwoot = { websiteToken: '123456' };
|
|
||||||
expect(getUserCookieName()).toBe('cw_user_123456');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#getUserString', () => {
|
describe('#getUserString', () => {
|
||||||
it('returns correct user string', () => {
|
it('returns correct user string', () => {
|
||||||
|
|
1
app/javascript/widget/constants/errorTypes.js
Normal file
1
app/javascript/widget/constants/errorTypes.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export const SET_USER_ERROR = 'SET_USER_ERROR';
|
2
app/javascript/widget/constants/sdkEvents.js
Normal file
2
app/javascript/widget/constants/sdkEvents.js
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const CHATWOOT_ERROR = 'chatwoot:error';
|
||||||
|
export const CHATWOOT_READY = 'chatwoot:ready';
|
|
@ -1,5 +1,6 @@
|
||||||
|
import { IFrameHelper } from 'widget/helpers/utils';
|
||||||
import ContactsAPI from '../../api/contacts';
|
import ContactsAPI from '../../api/contacts';
|
||||||
|
import { SET_USER_ERROR } from '../../constants/errorTypes';
|
||||||
const state = {
|
const state = {
|
||||||
currentUser: {},
|
currentUser: {},
|
||||||
};
|
};
|
||||||
|
@ -38,7 +39,14 @@ export const actions = {
|
||||||
dispatch('conversation/fetchOldConversations', {}, { root: true });
|
dispatch('conversation/fetchOldConversations', {}, { root: true });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Ignore error
|
const {
|
||||||
|
response: { data },
|
||||||
|
} = error;
|
||||||
|
IFrameHelper.sendMessage({
|
||||||
|
event: 'error',
|
||||||
|
errorType: SET_USER_ERROR,
|
||||||
|
data,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setCustomAttributes: async (_, customAttributes = {}) => {
|
setCustomAttributes: async (_, customAttributes = {}) => {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { API } from 'widget/helpers/axios';
|
||||||
import { actions } from '../../contacts';
|
import { actions } from '../../contacts';
|
||||||
|
|
||||||
const commit = jest.fn();
|
const commit = jest.fn();
|
||||||
|
const dispatch = jest.fn();
|
||||||
jest.mock('widget/helpers/axios');
|
jest.mock('widget/helpers/axios');
|
||||||
|
|
||||||
describe('#actions', () => {
|
describe('#actions', () => {
|
||||||
|
@ -11,11 +12,16 @@ describe('#actions', () => {
|
||||||
email: 'thoma@sphadikam.com',
|
email: 'thoma@sphadikam.com',
|
||||||
name: 'Adu Thoma',
|
name: 'Adu Thoma',
|
||||||
avatar_url: '',
|
avatar_url: '',
|
||||||
identifier_hash: 'malana_hash',
|
identifier_hash: 'random_hex_identifier_hash',
|
||||||
};
|
};
|
||||||
API.patch.mockResolvedValue({ data: { pubsub_token: 'token' } });
|
API.patch.mockResolvedValue({ data: { pubsub_token: 'token' } });
|
||||||
await actions.update({ commit }, { identifier: 1, user });
|
await actions.update({ commit, dispatch }, { identifier: 1, user });
|
||||||
expect(commit.mock.calls).toEqual([]);
|
expect(commit.mock.calls).toEqual([]);
|
||||||
|
expect(dispatch.mock.calls).toEqual([
|
||||||
|
['get'],
|
||||||
|
['conversation/clearConversations', {}, { root: true }],
|
||||||
|
['conversation/fetchOldConversations', {}, { root: true }],
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -36,14 +36,18 @@ window.chatwootSettings = {
|
||||||
})(document,"script");
|
})(document,"script");
|
||||||
|
|
||||||
window.addEventListener('chatwoot:ready', function() {
|
window.addEventListener('chatwoot:ready', function() {
|
||||||
console.log(window.$chatwoot);
|
console.log('chatwoot:ready', window.$chatwoot);
|
||||||
if (window.location.search.includes('setUser')) {
|
if (window.location.search.includes('setUser')) {
|
||||||
window.$chatwoot.setUser('<%= user_id %>', {
|
window.$chatwoot.setUser('<%= user_id %>', {
|
||||||
identifier_hash: '<%= user_hash %>',
|
identifier_hash: 'a<%= user_hash %>',
|
||||||
email: 'jane@example.com',
|
email: 'jane@example.com',
|
||||||
name: 'Jane Doe',
|
name: 'Jane Doe',
|
||||||
phone_number: ''
|
phone_number: ''
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
window.addEventListener('chatwoot:error', function(e) {
|
||||||
|
console.log('chatwoot:error', e.details)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in a new issue