|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +use Loki\Components\Util\ComponentUtil; |
| 4 | +use Magento\Framework\Escaper; |
| 5 | +use Magento\Framework\View\Element\Template; |
| 6 | +use Loki\Components\Config\Config; |
| 7 | +use Loki\Components\Factory\ViewModelFactory; |
| 8 | + |
| 9 | +/** @var Template $block */ |
| 10 | +/** @var ViewModelFactory $viewModelFactory */ |
| 11 | +/** @var Config $config */ |
| 12 | +/** @var Config $componentUtil */ |
| 13 | +/** @var ComponentUtil $componentUtil */ |
| 14 | +/** @var Escaper $escaper */ |
| 15 | + |
| 16 | +$config = $viewModelFactory->create(Config::class); |
| 17 | +$componentUtil = $viewModelFactory->create(ComponentUtil::class); |
| 18 | +$interval = $this->getPollingInterval(); |
| 19 | +if ($interval < 500) { |
| 20 | + return; |
| 21 | +} |
| 22 | +?> |
| 23 | +<script> |
| 24 | + (() => { |
| 25 | + setInterval(() => { |
| 26 | + fetch('<?= $escaper->escapeUrl($componentUtil->getPollingUrl()) ?>?form_key=' + LokiComponentFormKey + '&isAjax=true', { |
| 27 | + method: 'GET', |
| 28 | + headers: { |
| 29 | + 'X-Requested-With': 'XMLHttpRequest', |
| 30 | + 'Content-Type': 'application/json' |
| 31 | + }, |
| 32 | + }) |
| 33 | + .then(response => { |
| 34 | + if (response.ok) { |
| 35 | + return response.text() |
| 36 | + } |
| 37 | + |
| 38 | + LokiComponentsLogger.error('Fetch error', response); |
| 39 | + throw new Error(response.statusText); |
| 40 | + }) |
| 41 | + .then(html => { |
| 42 | + try { |
| 43 | + const data = JSON.parse(html); |
| 44 | + if (data) { |
| 45 | + data.forEach(dataPart => { |
| 46 | + if (dataPart.message) { |
| 47 | + const messageType = dataPart.messageType ?? 'notice'; |
| 48 | + Alpine.store('Message').addMessage(messageType, dataPart.message); |
| 49 | + } |
| 50 | + |
| 51 | + if (dataPart.redirectUrl) { |
| 52 | + if (dataPart.redirectMessage) { |
| 53 | + Alpine.store('Message').addWarningMessage(dataPart.redirectMessage); |
| 54 | + } |
| 55 | + |
| 56 | + const seconds = 3; |
| 57 | + setTimeout(() => { |
| 58 | + window.location = dataPart.redirectUrl; |
| 59 | + }, seconds * 1000) |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + } catch (e) { |
| 64 | + } |
| 65 | + }) |
| 66 | + .catch(error => { |
| 67 | + Alpine.store('Message').addErrorMessage(error); |
| 68 | + }); |
| 69 | + }, <?= $interval ?>); |
| 70 | + })(); |
| 71 | +</script> |
0 commit comments