Skip to content

Commit

Permalink
Rewrite form handler to support Laravel Precognition (#13)
Browse files Browse the repository at this point in the history
* Remove form handler

* Add back the form handler but new style

* Add a success hook

* Fix merge errors

* Hook everything up properly
  • Loading branch information
robdekort committed Dec 1, 2023
1 parent 8a340ac commit a4d3e46
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Statamic Peak Tools

This is an addon for [Statamic Peak](https://github.com/studio1902/statamic-peak) that contains tools like the CSRF token logic, picture partial, the noscript partial, the pagination partial, the toolbar and the missing alt text widget.
This is an addon for [Statamic Peak](https://github.com/studio1902/statamic-peak) that contains tools like the picture partial, the noscript partial, the pagination partial, the toolbar and the missing alt text widget.

Maintaining Peak demands a lot of my time and it probably saves you a lot. Your sponsoring would mean a great deal to me as it makes it much easier for me to maintain this project and keep improving it. [Sponsor me](https://github.com/sponsors/studio1902).

Expand Down
69 changes: 27 additions & 42 deletions resources/views/snippets/_form_handler.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,39 @@
{{ once }}
{{# The Statamic helpers for conditional fields. #}}
<script src="/vendor/statamic/frontend/js/helpers.js"></script>
{{# The form script handling validation and submission via AJAX with `fetch()`. #}}
<script>
document.addEventListener('alpine:initializing', () => {
Alpine.data('formHandler', () => {
return {
error: false,
errors: [],
sending: false,
success: false,
sendForm: async function() {
this.sending = true
// Post the form.
fetch(this.$refs.form.action, {
headers: {
'X-Requested-With' : 'XMLHttpRequest',
'X-CSRF-Token' : await getToken()
},
method: 'POST',
body: new FormData(this.$refs.form)})
.then(res => res.json())
.then(json => {
if (json['success']) {
this.errors = []
this.success = true
this.error = false
this.sending = false
this.$refs.form.reset()
setTimeout(() => {
this.success = false
}, 4500)
}
if (json['error']) {
this.sending = false
this.error = true
Alpine.data('formHandler', () => ({
success: false,
form: '',
init() {
this.form = this.$form(
'post',
this.$refs.form.getAttribute('action'),
JSON.parse(this.$refs.form.getAttribute('x-data')).dynamic_form
)
},
succesHook() {
{{ success_hook }}
},
submit() {
this.form.submit()
.then(response => {
this.form.reset()
this.$refs.form.reset()
this.success = true
this.succesHook()
setTimeout(() => {
this.success = false
this.errors = json['error']
}
}, 4500)
})
.catch(err => {
err.text().then( errorMessage => {
this.sending = false
})
.then(this.$refs.form.scrollIntoView())
.catch(error => {
this.$focus.focus(document.querySelector('#summary').querySelector('a'))
})
}
}
})
}))
})
</script>
{{ /once }}
Expand Down
7 changes: 0 additions & 7 deletions routes/actions.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Http/Controllers/DynamicToken.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

class ServiceProvider extends AddonServiceProvider
{
protected $routes = [
'actions' => __DIR__ . '/../routes/actions.php',
];

protected $subscribe = [
UpdateImagesMissingAltCacheListener::class,
];
Expand Down

0 comments on commit a4d3e46

Please sign in to comment.