Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UPM-1303]/evgeniy/livechat name and email not populated #16676

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5c39c37
fix: [UPM-1303]/evgeniy/livechat name and email not pipulated
yauheni-deriv Aug 29, 2024
689d960
chore: remove comments
yauheni-deriv Aug 30, 2024
16e940b
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Aug 30, 2024
34fe7e7
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 2, 2024
1dc1f5a
refactor: livechat init and usage
yauheni-deriv Sep 4, 2024
0d80eae
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 5, 2024
c810f9d
chore: remove old hook from redirect component
yauheni-deriv Sep 5, 2024
de223d9
chore: add tests for hooks
yauheni-deriv Sep 5, 2024
54e0e37
fix: failing test case
yauheni-deriv Sep 5, 2024
f448979
chore: removed unused import
yauheni-deriv Sep 5, 2024
d1936c3
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 5, 2024
03e0d59
fix: resolve conflict
yauheni-deriv Sep 6, 2024
f01b39e
chore: update package-lock
yauheni-deriv Sep 6, 2024
55b21b8
chore: remove icons, isMounted
yauheni-deriv Sep 9, 2024
93a4965
fix: reverted icons
yauheni-deriv Sep 9, 2024
f90348c
fix: revert useremote default
yauheni-deriv Sep 9, 2024
55167b0
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 9, 2024
6af8aa8
chore: review comments, refactor appcontent
yauheni-deriv Sep 9, 2024
909c4cf
fix: lc call in cashier
yauheni-deriv Sep 9, 2024
d3c37cd
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 10, 2024
e5bb049
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 11, 2024
2d1cfc4
fix: resolve conflict
yauheni-deriv Sep 11, 2024
a933a07
fix: resolve conflicts
yauheni-deriv Sep 13, 2024
ef7c7ce
fix: resolve conflicts
yauheni-deriv Sep 13, 2024
2784d3a
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 17, 2024
8b47483
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 18, 2024
9b16560
fix: resolve conflicts
yauheni-deriv Sep 18, 2024
9399ef3
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 18, 2024
f633f83
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 19, 2024
47fd86a
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 19, 2024
400534c
chore: change name of client store new field
yauheni-deriv Sep 19, 2024
7f3e79e
fix: resolve conflicts
yauheni-deriv Sep 20, 2024
e2a7fad
fix: resolve conflicts
yauheni-deriv Sep 23, 2024
29f05b2
Merge branch 'master' into livechat/upm-1303-name_and_email_not_popul…
yauheni-deriv Sep 23, 2024
70bb92f
fix: ts error
yauheni-deriv Sep 23, 2024
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
134 changes: 62 additions & 72 deletions packages/core/src/App/Components/Elements/LiveChat/use-livechat.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,93 @@
import { useEffect, useState } from 'react';
import Cookies from 'js-cookie';
import { deriv_urls, getActionFromUrl } from '@deriv/shared';
import { useDevice } from '@deriv-com/ui';
import { useIsMounted } from 'usehooks-ts';

// Todo: Should break this into smaller hooks or utility functions.
const useLiveChat = (has_cookie_account = false, active_loginid?: string) => {
const url_query_string = window.location.search;
const url_params = new URLSearchParams(url_query_string);
const reset_password = getActionFromUrl() === 'reset_password';
const should_disable_livechat = url_params.get('code') && reset_password;
const { isDesktop } = useDevice();

const [isReady, setIsReady] = useState(false);
const isMounted = useIsMounted();

const liveChatSetup = (is_logged_in: boolean) => {
if (window.LiveChatWidget && !should_disable_livechat) {
window.LiveChatWidget?.on('ready', () => {
let client_first_name = '';
let client_last_name = '';
const domain = /^(.)*deriv\.(com|me|be)$/gi.test(window.location.hostname)
? deriv_urls.DERIV_HOST_NAME
: 'binary.sx';
const client_information = Cookies.getJSON('client_information', {
domain,
});
const utm_data = Cookies.getJSON('utm_data', { domain });
window.LiveChatWidget?.on('ready', () => {
let client_first_name = '';
let client_last_name = '';
const domain = /^(.)*deriv\.(com|me|be)$/gi.test(window.location.hostname)
? deriv_urls.DERIV_HOST_NAME
: 'binary.sx';
const client_information = Cookies.getJSON('client_information', {
domain,
});

const { utm_source, utm_medium, utm_campaign } = utm_data || {};
const utm_data = Cookies.getJSON('utm_data', { domain });

const { loginid, email, landing_company_shortcode, currency, residence, first_name, last_name } =
client_information || {};
const { utm_source, utm_medium, utm_campaign } = utm_data || {};

client_first_name = first_name ?? ' ';
client_last_name = last_name ?? ' ';
const { loginid, email, landing_company_shortcode, currency, residence, first_name, last_name } =
client_information || {};

/* the session variables are sent to CS team dashboard to notify user has logged in
client_first_name = first_name ?? ' ';
client_last_name = last_name ?? ' ';

/* the session variables are sent to CS team dashboard to notify user has logged in
and also acts as custom variables to trigger targeted engagement */
const session_variables = {
is_logged_in: !!is_logged_in,
loginid: loginid ?? ' ',
landing_company_shortcode: landing_company_shortcode ?? ' ',
currency: currency ?? ' ',
residence: residence ?? ' ',
email: email ?? ' ',
utm_source: utm_source ?? ' ',
utm_medium: utm_medium ?? ' ',
utm_campaign: utm_campaign ?? ' ',
};
window.LiveChatWidget?.call('set_session_variables', session_variables);
const session_variables = {
is_logged_in: !!is_logged_in,
loginid: loginid ?? ' ',
landing_company_shortcode: landing_company_shortcode ?? ' ',
currency: currency ?? ' ',
residence: residence ?? ' ',
email: email ?? ' ',
utm_source: utm_source ?? ' ',
utm_medium: utm_medium ?? ' ',
utm_campaign: utm_campaign ?? ' ',
};
window.LiveChatWidget?.call('set_session_variables', session_variables);

if (is_logged_in) {
// client logged in
// prepfill name and email
window.LiveChatWidget?.call('set_customer_email', session_variables.email);
window.LiveChatWidget?.call('set_customer_name', `${client_first_name} ${client_last_name}`);
if (is_logged_in) {
// client logged in
yauheni-deriv marked this conversation as resolved.
Show resolved Hide resolved
// prepfill name and email
window.LiveChatWidget?.call('set_customer_email', session_variables.email);
window.LiveChatWidget?.call('set_customer_name', `${client_first_name} ${client_last_name}`);

// prefill name and email fields after chat has ended
if (window.LC_API?.on_chat_ended) {
window.LC_API.on_chat_ended = () => {
window.LiveChatWidget?.call('set_customer_email', session_variables.email);
window.LiveChatWidget?.call(
'set_customer_name',
`${client_first_name} ${client_last_name}`
);
};
}
} else {
// client not logged in
// clear name and email fields
window.LiveChatWidget?.call('set_customer_email', ' ');
window.LiveChatWidget?.call('set_customer_name', ' ');
// clear name and email fields after chat has ended
if (window.LC_API?.on_chat_ended) {
window.LC_API.on_chat_ended = () => {
window.LiveChatWidget?.call('set_customer_email', ' ');
window.LiveChatWidget?.call('set_customer_name', ' ');
};
}
// prefill name and email fields after chat has ended
if (window.LC_API?.on_chat_ended) {
window.LC_API.on_chat_ended = () => {
window.LiveChatWidget?.call('set_customer_email', session_variables.email);
window.LiveChatWidget?.call('set_customer_name', `${client_first_name} ${client_last_name}`);
};
}
});
}
};

useEffect(() => {
window.LiveChatWidget?.on('ready', () => {
if (isMounted() && !should_disable_livechat) setIsReady(true);
} else {
// client not logged in
// clear name and email fields
window.LiveChatWidget?.call('set_customer_email', ' ');
window.LiveChatWidget?.call('set_customer_name', ' ');
// clear name and email fields after chat has ended
if (window.LC_API?.on_chat_ended) {
window.LC_API.on_chat_ended = () => {
window.LiveChatWidget?.call('set_customer_email', ' ');
window.LiveChatWidget?.call('set_customer_name', ' ');
};
}
}
setIsReady(true);
});
}, [isMounted]);
};

useEffect(() => {
if (!should_disable_livechat) {
if (isMounted() && !should_disable_livechat) {
liveChatSetup(has_cookie_account);
}
}, [has_cookie_account, should_disable_livechat]);

useEffect(() => {
if (should_disable_livechat) return;
liveChatSetup(has_cookie_account);
}, [has_cookie_account, active_loginid, should_disable_livechat]);
window.onbeforeunload = () => {
if (!isDesktop) window.LiveChatWidget?.call('hide');
};
}, [isMounted, has_cookie_account, should_disable_livechat, active_loginid, isDesktop]);

return {
isReady,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import NetworkStatus from 'App/Components/Layout/Footer';
import ServerTime from 'App/Containers/server-time.jsx';
import getRoutesConfig from 'App/Constants/routes-config';
import LiveChat from 'App/Components/Elements/LiveChat';
import useLiveChat from 'App/Components/Elements/LiveChat/use-livechat.ts';
import { MenuTitle, MobileLanguageMenu } from './Components/ToggleMenu';
import MenuLink from './menu-link';
import PlatformSwitcher from './platform-switcher';
Expand All @@ -42,7 +41,6 @@ const ToggleMenuDrawer = observer(({ platform_config }) => {
is_logged_in,
is_logging_in,
is_virtual,
loginid,
logout: logoutClient,
should_allow_authentication,
should_allow_poinc_authentication,
Expand Down Expand Up @@ -76,7 +74,6 @@ const ToggleMenuDrawer = observer(({ platform_config }) => {
const { data } = useRemoteConfig(isMounted());
const { cs_chat_livechat, cs_chat_whatsapp } = data;

const liveChat = useLiveChat(false, loginid);
const [is_open, setIsOpen] = React.useState(false);
const [transitionExit, setTransitionExit] = React.useState(false);
const [primary_routes_config, setPrimaryRoutesConfig] = React.useState([]);
Expand Down Expand Up @@ -428,7 +425,7 @@ const ToggleMenuDrawer = observer(({ platform_config }) => {
/>
</MobileDrawer.Item>
)}
{liveChat.isReady && cs_chat_whatsapp && (
{cs_chat_whatsapp && (
<MobileDrawer.Item className='header__menu-mobile-whatsapp'>
<Icon icon='IcWhatsApp' className='drawer-icon' />
<a
Expand Down
Loading