Skip to content

Commit

Permalink
Added a custom onPageExpired handler callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Poortman committed Mar 21, 2023
1 parent ae985c2 commit dc6ce0d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/view/frontend/layout/default_hyva.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
ifconfig="dev/magewire/loader/enable"
template="Magewirephp_Magewire::page/js/magewire/plugin/loader.phtml"/>

<block name="magewire.plugin.exception"
template="Magewirephp_Magewire::page/js/magewire/plugin/exception.phtml"
<block name="magewire.plugin.error"
template="Magewirephp_Magewire::page/js/magewire/plugin/error.phtml"
>
<arguments>
<argument name="view_model" xsi:type="object">
Expand Down
50 changes: 50 additions & 0 deletions src/view/frontend/templates/page/js/magewire/plugin/error.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);
/**
* Copyright © Willem Poortman 2021-present. All rights reserved.
*
* Please read the README and LICENSE files for more
* details on copyrights and license information.
*/

/** @var Magewire $magewireScripts */
/** @var Escaper $escaper */
/** @var Template $block */

use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magewirephp\Magewire\ViewModel\Magewire;

$magewireScripts = $block->getViewModel();
$messages = $block->getData('status_messages');
?>
<script>
'use strict';

Magewire.onPageExpired(() => {
alert('<?= $escaper->escapeJs(__($messages[419] ?? 'Your session has expired due to inactivity.')) ?>')
window.location.reload()
})

Magewire.onError((status, response) => {
<?php /* We need to make sure Magewire reaches the native 419 handling. */ ?>
if (status === 419) {
return
}

<?php if ($magewireScripts->isProductionMode()): ?>
response.text().then((result) => {
let messages = <?= /** @noEscape */ json_encode(array_map('__', $messages)) ?>
result = JSON.parse(result)

console.error(
`Magewire: ${ messages[status] || result.message || 'Something went wrong' } (${ status })`
)
}).catch((exception) => {
console.error(exception)
})

return false
<?php endif ?>
})
</script>

This file was deleted.

0 comments on commit dc6ce0d

Please sign in to comment.