Add POST endpoint to create new adventure and update adventureService

This commit is contained in:
Sean Morley
2024-04-10 14:51:51 +00:00
parent 7ba879f7d9
commit ed9a579fc2
3 changed files with 88 additions and 30 deletions

View File

@@ -53,17 +53,40 @@
const createNewAdventure = () => {
let currentDate = new Date();
let dateString = currentDate.toISOString().slice(0, 10); // Get date in "yyyy-mm-dd" format
const newAdventure: Adventure = {
id: getNextId(),
name: newName,
location: newLocation,
created: dateString,
};
addAdventure(newAdventure);
newName = ""; // Reset newName and newLocation after adding adventure
newLocation = "";
adventures = getAdventures(); // add to local array
showToast("added");
// post to /api/visits
fetch("/api/visits", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: newName,
location: newLocation,
created: dateString,
}),
})
.then((response) => response.json())
.then((data) => {
console.log("Success:", data);
let newId = data.id;
console.log("New ID: " + newId);
console.log("New Name: " + newName);
adventures = [
...adventures,
{
id: newId,
name: newName,
location: newLocation,
created: dateString,
},
];
newName = ""; // Reset newName and newLocation after adding adventure
newLocation = "";
showToast("added");
})
.catch((error) => {
console.error("Error:", error);
});
};
// function triggerRemoveAdventure(event: { detail: number }) {