-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a custom onPageExpired handler callback
- Loading branch information
Willem Poortman
committed
Mar 21, 2023
1 parent
ae985c2
commit dc6ce0d
Showing
3 changed files
with
52 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/view/frontend/templates/page/js/magewire/plugin/error.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
46 changes: 0 additions & 46 deletions
46
src/view/frontend/templates/page/js/magewire/plugin/exception.phtml
This file was deleted.
Oops, something went wrong.