Skip to content

Instantly share code, notes, and snippets.

@jmas
Created July 11, 2019 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmas/30a731475189c3476254a1a4fee177b2 to your computer and use it in GitHub Desktop.
Save jmas/30a731475189c3476254a1a4fee177b2 to your computer and use it in GitHub Desktop.
<?php
// process form
// respond as HTML with updated state
import { Controller } from 'stimulus';
export default class extends Controller {
submit(event) {
this.inProgress(true);
window.fetch('/send', {
body: new FormData(event.target)
}).then(response => {
if (response.status !== 200) {
throw new Error(`Failed!`);
}
return response.json();
}).then(
data => {
this.setInProgress(false);
},
error => {
this.setInProgress(false);
}
);
}
set inProgress(value) {
this.data.set('inProgress', true);
}
get inProgress() {
return JSON.parse(this.data.get('inProgress'));
}
}
<form action="/send.php" data-controller="send_form" data-action="send_form#submit" data-in-progress="true">
<input type="text" />
<button>Send</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment