diff --git a/src/i18n/fa.json b/src/i18n/fa.json index 27b295b4a..98fbb43dd 100644 --- a/src/i18n/fa.json +++ b/src/i18n/fa.json @@ -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": "یک گزینه را انتخاب کنید", @@ -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": "پایان این گفتگو", @@ -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": "نیاز به کمک دارید?", @@ -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})" } -} \ No newline at end of file +} diff --git a/src/lib/hooks.js b/src/lib/hooks.js index 72bc3e457..59ddeda0c 100644 --- a/src/lib/hooks.js +++ b/src/lib/hooks.js @@ -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'; @@ -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: '' }); }, diff --git a/src/lib/main.js b/src/lib/main.js index a38e4def3..55fc79f70 100644 --- a/src/lib/main.js +++ b/src/lib/main.js @@ -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; @@ -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, diff --git a/src/lib/room.js b/src/lib/room.js index 270098fa5..0e97c1022 100644 --- a/src/lib/room.js +++ b/src/lib/room.js @@ -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); } diff --git a/src/store/Store.js b/src/store/Store.js index 0a14c7cd0..3c1061f85 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -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; diff --git a/src/store/index.js b/src/store/index.js index 84706a8a1..0d35104d3 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -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']; diff --git a/src/widget.js b/src/widget.js index e45942ad0..b809faca0 100644 --- a/src/widget.js +++ b/src/widget.js @@ -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); } @@ -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; @@ -436,6 +448,8 @@ window.RocketChat.livechat = { hideWidget, maximizeWidget, minimizeWidget, + setBusinessUnit, + clearBusinessUnit, // callbacks onChatMaximized(fn) { registerCallback('chat-maximized', fn); }, diff --git a/widget-demo.html b/widget-demo.html index 058961f03..9634f6981 100644 --- a/widget-demo.html +++ b/widget-demo.html @@ -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 + }); + });