Created
February 6, 2023 23:10
-
-
Save jongan69/ee0131b441c18b609b64d64d8f8ae75a to your computer and use it in GitHub Desktop.
A dynamic form post function
This file contains hidden or 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
| const handleSubmit = async e => { | |
| e.preventDefault(); | |
| // @TODO - handle validation | |
| // ... | |
| // @TODO - handle submit based on operation | |
| switch (operation) { | |
| case 'add': | |
| console.log(dataType, state); | |
| // Attempt to publish the data | |
| await fetch(`http://localhost:8080/${dataType.toLowerCase()}`, { | |
| method: 'POST', | |
| // mode: 'cors', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify(state), | |
| }) | |
| .then((data) => data.json()) | |
| .then((data) => { | |
| // Display the server response | |
| console.log(data.message); | |
| window.alert(data.message); | |
| }); | |
| return alert(JSON.stringify(state)); | |
| // case 'get': | |
| // case 'update': | |
| default: | |
| alert(JSON.stringify(state)); | |
| break; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment