Skip to content

Instantly share code, notes, and snippets.

@jongan69
Created February 6, 2023 23:10
Show Gist options
  • Select an option

  • Save jongan69/ee0131b441c18b609b64d64d8f8ae75a to your computer and use it in GitHub Desktop.

Select an option

Save jongan69/ee0131b441c18b609b64d64d8f8ae75a to your computer and use it in GitHub Desktop.
A dynamic form post function
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