Skip to content

Commit

Permalink
Add progressive enhancement for asynchronous request
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 20, 2023
1 parent da08840 commit 9d0174d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/realtime-compiler/resources/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<td class="text-end">
<div class="d-flex justify-content-end">
@if($dashboard->enableEditor())
<form action="" method="POST">
<form class="openInEditorForm" action="" method="POST">
<input type="hidden" name="action" value="openInEditor">
<input type="hidden" name="routeKey" value="{{ $route->getRouteKey() }}">
<button type="submit" class="btn btn-outline-primary btn-sm me-2" title="Open in system default application">Edit</button>
Expand Down Expand Up @@ -126,5 +126,35 @@
<div class="col-lg-3"></div>
</div>
</footer>
@if($dashboard->enableEditor())
<script>
/**
* Progressive enhancement when JavaScript is enabled to intercept form requests
* and instead handle them with an asynchronous Fetch instead of refreshing the page.
*/
const forms = document.querySelectorAll(".openInEditorForm");
forms.forEach(form => {
form.addEventListener("submit", function (event) {
// Disable default form submit
event.preventDefault();
fetch("", {
method: "POST",
body: new FormData(event.target),
}).then(response => {
if (response.ok) {
// Request was successful, no need to do anything.
} else {
// Request failed, let's log it.
console.error("Fetch request failed.");
}
}).catch(error => {
// Handle any network-related errors
console.error("Network error:", error);
});
});
});
</script>
@endif
</body>
</html>

0 comments on commit 9d0174d

Please sign in to comment.