diff --git a/src/javascript/_common/base/livechat.js b/src/javascript/_common/base/livechat.js index ecc9185cfc0..1031ffbaf20 100644 --- a/src/javascript/_common/base/livechat.js +++ b/src/javascript/_common/base/livechat.js @@ -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) => { @@ -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 = () => { @@ -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) { diff --git a/src/javascript/_common/utility.js b/src/javascript/_common/utility.js index 20b0bf28bd3..618f9961a55 100644 --- a/src/javascript/_common/utility.js +++ b/src/javascript/_common/utility.js @@ -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); diff --git a/src/javascript/app/base/client.js b/src/javascript/app/base/client.js index f115e3437a8..5498fe8ee10 100644 --- a/src/javascript/app/base/client.js +++ b/src/javascript/app/base/client.js @@ -108,9 +108,9 @@ 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({ @@ -118,8 +118,8 @@ const Client = (() => { 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'); if (threadId) { customerSDK.deactivateChat({ chatId }).catch(() => null); } diff --git a/src/javascript/app/hooks/use-freshChat.js b/src/javascript/app/hooks/use-freshChat.js index 43185b2d593..2bc079319c7 100644 --- a/src/javascript/app/hooks/use-freshChat.js +++ b/src/javascript/app/hooks/use-freshChat.js @@ -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', diff --git a/src/javascript/app/pages/livechat.jsx b/src/javascript/app/pages/livechat.jsx index 5630e0aab4a..4a88ededdfd 100644 --- a/src/javascript/app/pages/livechat.jsx +++ b/src/javascript/app/pages/livechat.jsx @@ -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',