Created
July 11, 2019 16:06
-
-
Save jmas/30a731475189c3476254a1a4fee177b2 to your computer and use it in GitHub Desktop.
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
<?php | |
// process form | |
// respond as HTML with updated state |
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
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')); | |
} | |
} |
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
<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