-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderAuthDropdown.js
More file actions
116 lines (100 loc) · 4.2 KB
/
renderAuthDropdown.js
File metadata and controls
116 lines (100 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint-disable import/prefer-default-export */
import * as authApi from '@dropins/storefront-auth/api.js';
import { render as authRenderer } from '@dropins/storefront-auth/render.js';
import { SignIn } from '@dropins/storefront-auth/containers/SignIn.js';
import { events } from '@dropins/tools/event-bus.js';
import { getCookie } from '../../scripts/configs.js';
import { CUSTOMER_FORGOTPASSWORD_PATH } from '../../scripts/constants.js';
import { rootLink } from '../../scripts/scripts.js';
function checkAndRedirect(redirections) {
Object.entries(redirections).some(([currentPath, redirectPath]) => {
if (window.location.pathname.includes(currentPath)) {
window.location.href = redirectPath;
return true;
}
return false;
});
}
function renderSignIn(element) {
authRenderer.render(SignIn, {
onSuccessCallback: () => {},
formSize: 'small',
routeForgotPassword: () => rootLink(CUSTOMER_FORGOTPASSWORD_PATH),
})(element);
}
export function renderAuthDropdown(navTools) {
const dropdownElement = document.createRange().createContextualFragment(`
<div class="dropdown-wrapper nav-tools-wrapper">
<button type="button" class="nav-dropdown-button" aria-haspopup="dialog" aria-expanded="false" aria-controls="login-modal"></button>
<div class="nav-auth-menu-panel nav-tools-panel">
<div id="auth-dropin-container"></div>
<ul class="authenticated-user-menu">
<li><a href="${rootLink('/customer/account')}">My Account</a></li>
<li><button>Logout</button></li>
</ul>
</div>
</div>`);
navTools.append(dropdownElement);
const authDropDownPanel = navTools.querySelector('.nav-auth-menu-panel');
const authDropDownMenuList = navTools.querySelector(
'.authenticated-user-menu',
);
const authDropinContainer = navTools.querySelector('#auth-dropin-container');
const loginButton = navTools.querySelector('.nav-dropdown-button');
const logoutButtonElement = navTools.querySelector(
'.authenticated-user-menu > li > button',
);
authDropDownPanel.addEventListener('click', (e) => e.stopPropagation());
async function toggleDropDownAuthMenu(state) {
const show = state ?? !authDropDownPanel.classList.contains('nav-tools-panel--show');
authDropDownPanel.classList.toggle('nav-tools-panel--show', show);
authDropDownPanel.setAttribute('role', 'dialog');
authDropDownPanel.setAttribute('aria-hidden', 'false');
authDropDownPanel.setAttribute('aria-labelledby', 'modal-title');
authDropDownPanel.setAttribute('aria-describedby', 'modal-description');
authDropDownPanel.focus();
}
loginButton.addEventListener('click', () => toggleDropDownAuthMenu());
document.addEventListener('click', async (e) => {
const clickOnDropDownPanel = authDropDownPanel.contains(e.target);
const clickOnLoginButton = loginButton.contains(e.target);
if (!clickOnDropDownPanel && !clickOnLoginButton) {
await toggleDropDownAuthMenu(false);
}
});
logoutButtonElement.addEventListener('click', async () => {
await authApi.revokeCustomerToken();
checkAndRedirect({
'/customer': rootLink('/customer/login'),
'/order-details': rootLink('/'),
});
});
renderSignIn(authDropinContainer);
const updateDropDownUI = (isAuthenticated) => {
const getUserTokenCookie = getCookie('auth_dropin_user_token');
const getUserNameCookie = getCookie('auth_dropin_firstname');
if (isAuthenticated || getUserTokenCookie) {
authDropDownMenuList.style.display = 'block';
authDropinContainer.style.display = 'none';
loginButton.textContent = `Hi, ${getUserNameCookie}`;
} else {
authDropDownMenuList.style.display = 'none';
authDropinContainer.style.display = 'block';
loginButton.innerHTML = `
<svg
width="25"
height="25"
viewBox="0 0 24 24"
aria-label="My Account"
>
<g fill="none" stroke="#000000" stroke-width="1.5">
<circle cx="12" cy="6" r="4"></circle>
<path d="M20 17.5c0 2.485 0 4.5-8 4.5s-8-2.015-8-4.5S7.582 13 12 13s8 2.015 8 4.5Z"></path></g></svg>
`;
}
};
events.on('authenticated', (isAuthenticated) => {
updateDropDownUI(isAuthenticated);
});
updateDropDownUI();
}