Chatwoot/app/javascript/packs/sdk.js
Pranav Raj S 59f4dd7ebe
Feature: Introduce locale in web-widget SDK (#871)
* Introduce setLocale in SDK

Co-authored-by: Sojan <sojan@pepalo.com>
2020-05-17 16:15:53 +05:30

66 lines
1.7 KiB
JavaScript
Executable file

import Cookies from 'js-cookie';
import { IFrameHelper } from '../sdk/IFrameHelper';
const runSDK = ({ baseUrl, websiteToken }) => {
const chatwootSettings = window.chatwootSettings || {};
window.$chatwoot = {
baseUrl,
hasLoaded: false,
hideMessageBubble: chatwootSettings.hideMessageBubble || false,
isOpen: false,
position: chatwootSettings.position === 'left' ? 'left' : 'right',
websiteToken,
locale: chatwootSettings.locale || 'en',
toggle() {
IFrameHelper.events.toggleBubble();
},
setUser(identifier, user) {
if (typeof identifier === 'string' || typeof identifier === 'number') {
window.$chatwoot.identifier = identifier;
window.$chatwoot.user = user || {};
IFrameHelper.sendMessage('set-user', {
identifier,
user: window.$chatwoot.user,
});
} else {
throw new Error('Identifier should be a string or a number');
}
},
setLabel(label = '') {
IFrameHelper.sendMessage('set-label', { label });
},
removeLabel(label = '') {
IFrameHelper.sendMessage('remove-label', { label });
},
setLocale(localeToBeUsed = 'en') {
IFrameHelper.sendMessage('set-locale', { locale: localeToBeUsed });
},
reset() {
if (window.$chatwoot.isOpen) {
IFrameHelper.events.toggleBubble();
}
Cookies.remove('cw_conversation');
const iframe = IFrameHelper.getAppFrame();
iframe.src = IFrameHelper.getUrl({
baseUrl: window.$chatwoot.baseUrl,
websiteToken: window.$chatwoot.websiteToken,
});
},
};
IFrameHelper.createFrame({
baseUrl,
websiteToken,
});
};
window.chatwootSDK = {
run: runSDK,
};