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

[NEW] Introduce Widget API method to manage Business Units #677

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ const api = {
updateIframeGuestData({ department });
},

async setBusinessUnit(newBusinessUnit) {
if (!newBusinessUnit || !newBusinessUnit.trim().length) {
throw new Error('Error! Invalid business ids');
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
}

const { businessUnit: existingBusinessUnit } = store.state;
if (existingBusinessUnit === newBusinessUnit) {
return;
}
store.setState({ businessUnit: newBusinessUnit });
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
await loadConfig();
},

clearDepartment() {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
updateIframeGuestData({ department: '' });
},
Expand Down
6 changes: 5 additions & 1 deletion src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import constants from './constants';
export const loadConfig = async () => {
const {
token,
businessUnit = null,
} = store.state;

Livechat.credentials.token = token;
Expand All @@ -21,7 +22,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
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
9 changes: 9 additions & 0 deletions src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ function setDepartment(department) {
callHook('setDepartment', department);
}

function setBusinessUnit(businessUnits) {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
callHook('setBusinessUnit', businessUnits);
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
}

function setGuestToken(token) {
callHook('setGuestToken', token);
}
Expand Down Expand Up @@ -334,6 +338,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 +444,7 @@ window.RocketChat.livechat = {
hideWidget,
maximizeWidget,
minimizeWidget,
setBusinessUnit,

// 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() {
murtaza98 marked this conversation as resolved.
Show resolved Hide resolved
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