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

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat.Livechat …
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
tiagoevanp committed Jan 20, 2022
2 parents 8da8b8b + 1ee9420 commit ca2d04e
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/i18n/fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"change_department_523a16e8": "تغییر بخش",
"chat_finished_effbd589": "گفتگو پایان یافت",
"chat_now_3d7f6769": "Chat now",
"chat_started_3b1db6d6": "Chat started",
"chat_started_3b1db6d6": "گفتگو شروع شد",
"choose_a_department_b106da55": "یک بخش را انتخاب کنید ...",
"choose_a_department_fe9755fd": "یک بخش را انتخاب کنید",
"choose_an_option_26ac97d2": "یک گزینه را انتخاب کنید",
Expand All @@ -28,7 +28,7 @@
"error_removing_user_data_ce507478": "خطا در حذف اطلاعات کاربر.",
"error_starting_a_new_conversation_reason_a1b491a1": "خطا در ایجاد گفتگوی جدید: %{reason}",
"expand_chat_a0045dbd": "گسترش گفتگو",
"field_required_fc5c6b05": "Field required",
"field_required_fc5c6b05": "ورودی الزامی",
"file_exceeds_allowed_size_of_size_bd65c389": "حجم فایل بیشتر از حد مجاز (%{size}) است.",
"fileupload_error_9eedee68": "خطا در آپلود فایل",
"finish_this_chat_87b79542": "پایان این گفتگو",
Expand All @@ -43,14 +43,14 @@
"i_need_help_with_61054e21": "نیاز به کمک در بخش زیر را دارم",
"if_you_have_any_other_questions_just_press_the_but_ceaadfa0": "اگر هرگونه سوال دیگری دارید، بر روی دکمه ی زیر کلیک کنید تا یک گفتگو آغاز شود",
"insert_your_field_here_d631e875": "Insert your %{field} here...",
"invalid_email_e82f3682": "Invalid email",
"invalid_value_12ca12c2": "Invalid value",
"invalid_email_e82f3682": "ایمیل نامعتبر",
"invalid_value_12ca12c2": "مقدار نامعتبر",
"leave_a_message_5b581048": "یک پیغام بگذارید",
"livechat_connected_afee1c5b": "Livechat وصل شد.",
"livechat_is_not_connected_b40328ca": "Livechat وصل نشد.",
"media_types_not_accepted_4e25676a": "این نوع فایل قابل قبول نیست.",
"message_5c38209d": "پیام",
"messages_64e7435f": "Messages",
"messages_64e7435f": "پیام‌ها",
"minimize_chat_804b3135": "کوچک کردن گفتگو",
"name_1aed4a1b": "نام",
"need_help_803a61": "نیاز به کمک دارید?",
Expand Down Expand Up @@ -88,4 +88,4 @@
"your_spot_is_spot_a35cd288": "Your spot is #%{spot}",
"your_spot_is_spot_estimated_wait_time_estimatedwai_d0ff46e0": "Your spot is #%{spot} (Estimated wait time: %{estimatedWaitTime})"
}
}
}
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 ca2d04e

Please sign in to comment.