Skip to content

Commit eee8f36

Browse files
committed
Add top links component
1 parent e4f7337 commit eee8f36

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

etc/frontend/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<item name="form.subscribe" xsi:type="string">LumaNewsletter</item>
3030
<item name="catalog.topnav" xsi:type="string">LumaMainMenu</item>
3131
<item name="minicart" xsi:type="string">LumaMiniCart</item>
32+
<item name="top.links" xsi:type="string">LumaTopLinks</item>
3233
</argument>
3334
</arguments>
3435
</type>

view/frontend/layout/loki_theme_default.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<block name="loki.script.components.newsletter" template="Loki_Theme::script/components/newsletter.phtml"/>
1717
<block name="loki.script.components.messages" template="Loki_Theme::script/components/messages.phtml"/>
1818
<block name="loki.script.components.minicart" template="Loki_Theme::script/components/minicart.phtml"/>
19+
<block name="loki.script.components.top-links" template="Loki_Theme::script/components/top-links.phtml"/>
1920
</referenceContainer>
2021

2122
<referenceContainer name="minicart.addons">
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/** @version 0.0.4 */
5+
6+
use Magento\Framework\View\Element\Template;
7+
8+
/** @var Template $block */
9+
10+
$customerAccountLogoutUrl = $block->getUrl('customer/account/logout');
11+
$customerAccountLoginUrl = $block->getUrl('customer/account/login');
12+
?>
13+
<script>
14+
document.addEventListener('alpine:init', () => {
15+
Alpine.data('LumaTopLinks', () => ({
16+
init() {
17+
Alpine.effect(() => {
18+
const customerSection = Alpine.store('LumaLocalStorage').get('customer');
19+
20+
if (customerSection.id) {
21+
this.showCustomerUrls();
22+
this.hideGuestUrls();
23+
this.showGreeting();
24+
} else {
25+
this.hideCustomerUrls();
26+
this.showGuestUrls();
27+
this.hideGreeting();
28+
}
29+
});
30+
},
31+
showCustomerUrls() {
32+
this.getCustomerElements().forEach(element => {
33+
if (element) element.parentNode.style.display = 'block';
34+
});
35+
},
36+
hideCustomerUrls() {
37+
this.getCustomerElements().forEach(element => {
38+
if (element) element.parentNode.style.display = 'none';
39+
});
40+
},
41+
showGuestUrls() {
42+
this.getGuestElements().forEach(element => {
43+
if (element) element.parentNode.style.display = 'inline-block';
44+
});
45+
},
46+
hideGuestUrls() {
47+
this.getGuestElements().forEach(element => {
48+
if (element) element.parentNode.style.display = 'none';
49+
});
50+
},
51+
showGreeting() {
52+
const customerSection = Alpine.store('LumaLocalStorage').get('customer');
53+
this.$el.querySelector('.logged-in').innerHTML = '<?= /* @noEscape */ __('Welcome, %1') ?>'
54+
.replace('%1', customerSection.fullname);
55+
},
56+
hideGreeting() {
57+
this.$el.querySelector('.greet.welcome').style.display = 'none';
58+
},
59+
getGuestElements() {
60+
return [
61+
this.$el.querySelector('a[href="<?= /* @noEscape */ $customerAccountLoginUrl ?>"]')
62+
];
63+
},
64+
getCustomerElements() {
65+
return [
66+
this.$el.querySelector('a[href="<?= /* @noEscape */ $customerAccountLogoutUrl ?>"]')
67+
];
68+
}
69+
}));
70+
});
71+
</script>

0 commit comments

Comments
 (0)