Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
[NEW] Introduce Widget API method to manage Business Units (#677)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Aleman <kevin.aleman@rocket.chat>
Co-authored-by: Renato Becker <renato.augusto.becker@gmail.com>
  • Loading branch information
3 people committed Jan 20, 2022
1 parent b5f0cce commit d95ae3d
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Livechat } from '../api';
import { store } from '../store';
import CustomFields from './customFields';
import { setWidgetLanguage } from './locale';
import { loadConfig } from './main';
import { loadConfig, updateBusinessUnit } from './main';
import { parentCall } from './parentCall';
import { createToken } from './random';
import Triggers from './triggers';
Expand Down Expand Up @@ -69,6 +69,21 @@ const api = {
updateIframeGuestData({ department });
},

async setBusinessUnit(newBusinessUnit) {
if (!newBusinessUnit || !newBusinessUnit.trim().length) {
throw new Error('Error! Invalid business ids');
}

const { businessUnit: existingBusinessUnit } = store.state;

return existingBusinessUnit !== newBusinessUnit && updateBusinessUnit(newBusinessUnit);
},

async clearBusinessUnit() {
const { businessUnit } = store.state;
return businessUnit && updateBusinessUnit();
},

clearDepartment() {
updateIframeGuestData({ department: '' });
},
Expand Down
39 changes: 38 additions & 1 deletion src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,43 @@ import I18n from '../i18n';
import store from '../store';
import constants from './constants';

export const updateBusinessUnit = async (newBusinessUnit) => {
const {
token,
config: existingConfig,
} = store.state;
if (!token) {
throw new Error('Error! no livechat token found. please make sure you initialize widget first before setting business unit');
}

const { departments } = await Livechat.config({
token,
...newBusinessUnit && { businessUnit: newBusinessUnit },
});

if (newBusinessUnit) {
return store.setState({
config: {
...existingConfig,
departments,
},
businessUnit: newBusinessUnit,
});
}

await store.setState({
config: {
...existingConfig,
departments,
},
});
await store.unsetSinglePropInStateByName('businessUnit');
};

export const loadConfig = async () => {
const {
token,
businessUnit = null,
} = store.state;

Livechat.credentials.token = token;
Expand All @@ -21,7 +55,10 @@ export const loadConfig = async () => {
resources: { sound: src = null } = {},
queueInfo,
...config
} = await Livechat.config({ token });
} = await Livechat.config({
token,
...businessUnit && { businessUnit },
});

await store.setState({
config,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const closeChat = async ({ transcriptRequested } = {}) => {

if (clearLocalStorageWhenChatEnded) {
// exclude UI-affecting flags
const { minimized, visible, undocked, expanded, ...initial } = initialState();
const { minimized, visible, undocked, expanded, businessUnit, ...initial } = initialState();
await store.setState(initial);
}

Expand Down
8 changes: 8 additions & 0 deletions src/store/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export default class Store {
this.emit('change', [this._state, prevState, partialState]);
}

unsetSinglePropInStateByName(propName) {
const prevState = this._state;
delete prevState[propName];
this._state = { ...prevState };
this.persist();
this.emit('change', [this._state, prevState]);
}

setStoredState(storedState) {
const prevState = this._state;

Expand Down
1 change: 1 addition & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const initialState = () => ({
unread: null,
incomingCallAlert: null,
ongoingCall: null, // TODO: store call info like url, startTime, timeout, etc here
businessUnit: null,
});

const dontPersist = ['messages', 'typing', 'loading', 'alerts', 'unread', 'noMoreMessages', 'modal', 'incomingCallAlert', 'ongoingCall'];
Expand Down
14 changes: 14 additions & 0 deletions src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ function setDepartment(department) {
callHook('setDepartment', department);
}

function setBusinessUnit(businessUnit) {
callHook('setBusinessUnit', businessUnit);
}

function clearBusinessUnit() {
callHook('clearBusinessUnit');
}

function setGuestToken(token) {
callHook('setGuestToken', token);
}
Expand Down Expand Up @@ -334,6 +342,10 @@ function initialize(params) {
case 'department':
setDepartment(params[method]);
continue;
case 'businessUnit': {
setBusinessUnit(params[method]);
continue;
}
case 'guestToken':
setGuestToken(params[method]);
continue;
Expand Down Expand Up @@ -436,6 +448,8 @@ window.RocketChat.livechat = {
hideWidget,
maximizeWidget,
minimizeWidget,
setBusinessUnit,
clearBusinessUnit,

// callbacks
onChatMaximized(fn) { registerCallback('chat-maximized', fn); },
Expand Down
9 changes: 9 additions & 0 deletions widget-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
j.src = 'build/rocketchat-livechat.min.js?_=' + Math.random();
h.parentNode.insertBefore(j, h);
})(window, document, 'script', 'http://localhost:8080');
RocketChat(function() {
this.setTheme({
color: '#04436A', // widget title background color
fontColor: '#FFFFFF', // widget title font color
iconColor: '#1d74f5', // widget icon color
title: "Welcome to Rocket.Chat", // default widget title when the status of service is online
offlineTitle: "Service is offline", // default widget title when the status of service is online
});
});
</script>
</body>

Expand Down

0 comments on commit d95ae3d

Please sign in to comment.