Skip to content

Commit

Permalink
fix: condition to check livechatWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhy-Deriv committed Nov 4, 2024
1 parent 60c6b38 commit 1d423e9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/javascript/_common/base/livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ const LiveChat = (() => {
...utm_campaign && { utm_campaign },
...utm_medium && { utm_medium },
};
window.LiveChatWidget.call('set_session_variables', session_variables);
window.LiveChatWidget?.call('set_session_variables', session_variables);
};

const setNameEmail = () => {
if (client_email) window.LiveChatWidget.call('set_customer_email', client_email);
if (first_name && last_name) window.LiveChatWidget.call('set_customer_name', `${first_name} ${last_name}`);
if (client_email) window.LiveChatWidget?.call('set_customer_email', client_email);
if (first_name && last_name) window.LiveChatWidget?.call('set_customer_name', `${first_name} ${last_name}`);
};

BinarySocket.wait('get_settings').then((response) => {
Expand All @@ -55,12 +55,12 @@ const LiveChat = (() => {

const initialize = () => {
if (window.LiveChatWidget) {
window.LiveChatWidget.on('ready', () => {
window.LiveChatWidget?.on('ready', () => {
setSessionVariables();
if (!ClientBase.isLoggedIn()){
window.LC_API.on_chat_ended = () => {
window.LiveChatWidget.call('set_customer_email', ' ');
window.LiveChatWidget.call('set_customer_name', ' ');
window.LiveChatWidget?.call('set_customer_email', ' ');
window.LiveChatWidget?.call('set_customer_name', ' ');
};
} else {
window.LC_API.on_chat_ended = () => {
Expand All @@ -74,10 +74,10 @@ const LiveChat = (() => {
// Delete existing LiveChat instance when there is no chat running
const livechatDeletion = () => new Promise ((resolve) => {
if (window.LiveChatWidget){
window.LiveChatWidget.on('ready', () => {
window.LiveChatWidget?.on('ready', () => {
try {
if (window.LiveChatWidget.get('customer_data').status !== 'chatting') {
window.LiveChatWidget.call('destroy');
if (window.LiveChatWidget?.get('customer_data').status !== 'chatting') {
window.LiveChatWidget?.call('destroy');
resolve();
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/_common/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ const openChatWithParam = () => {
window.fcWidget.open();
clearInterval(interval);
} else if (window.LiveChatWidget) {
window.LiveChatWidget.on('ready', () => {
window.LiveChatWidget?.on('ready', () => {
window.LC_API.open_chat_window();
});
clearInterval(interval);
Expand Down
10 changes: 5 additions & 5 deletions src/javascript/app/base/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ const Client = (() => {
// Called when logging out to end ongoing chats if there is any
const endLiveChat = () => new Promise ((resolve) => {
const session_variables = { loginid: '', landing_company_shortcode: '', currency: '', residence: '', email: '' };
window.LiveChatWidget.call('set_session_variables', session_variables);
window.LiveChatWidget.call('set_customer_email', ' ');
window.LiveChatWidget.call('set_customer_name', ' ');
window.LiveChatWidget?.call('set_session_variables', session_variables);
window.LiveChatWidget?.call('set_customer_email', ' ');
window.LiveChatWidget?.call('set_customer_name', ' ');

try {
const customerSDK = init({
licenseId: licenseID,
clientId : clientID,
});
customerSDK.on('connected', () => {
if (window.LiveChatWidget.get('chat_data')) {
const { chatId, threadId } = window.LiveChatWidget.get('chat_data');
if (window.LiveChatWidget?.get('chat_data')) {
const { chatId, threadId } = window.LiveChatWidget?.get('chat_data');

Check failure on line 122 in src/javascript/app/base/client.js

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError
if (threadId) {
customerSDK.deactivateChat({ chatId }).catch(() => null);
}
Expand Down
4 changes: 1 addition & 3 deletions src/javascript/app/hooks/use-freshChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { useScript } from 'usehooks-ts';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';

const useFreshChat = (token) => {
const scriptStatus = useScript(
'https://static.deriv.com/scripts/freshchat.js'
);
const scriptStatus = useScript('https://static.deriv.com/scripts/freshchat.js');
const [isReady, setIsReady] = useState(false);
const [isFreshChatEnabled] = useGrowthbookGetFeatureValue({
featureFlag: 'enable_freshworks_live_chat',
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/app/pages/livechat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const LiveChat = () => {

const loginid = localStorage.getItem('active_loginid');
const client_info = loginid && JSON.parse(localStorage.getItem('client.accounts') || '{}')[loginid];
const token = client_info ? client_info.token : null;
const token = client_info?.token ?? null;

const [isFreshChatEnabled] = useGrowthbookGetFeatureValue({
featureFlag: 'enable_freshworks_live_chat',
Expand Down

0 comments on commit 1d423e9

Please sign in to comment.