|
| 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