feat: Add addAdventure function to adventureService

The code changes include adding a new function called addAdventure to the adventureService module. This function is responsible for sending a POST request to the corresponding API endpoint to add a new adventure. If the request is successful, the adventure is added to the local plans array and a success toast is displayed. If the request fails, an error toast is displayed. This functionality allows users to add adventures to the planner page.
This commit is contained in:
Sean Morley
2024-05-04 17:14:20 +00:00
parent 79cf19ccb2
commit 609d3743ed
2 changed files with 47 additions and 58 deletions

View File

@@ -7,6 +7,7 @@
import {
saveAdventure,
removeAdventure,
addAdventure,
} from "../../services/adventureService.js";
import SucessToast from "$lib/components/SucessToast.svelte";
import mapDrawing from "$lib/assets/adventure_map.svg";
@@ -76,40 +77,15 @@
}
}
const createNewAdventure = (event: { detail: Adventure }) => {
const createNewAdventure = async (event: { detail: Adventure }) => {
isShowingMoreFields = false;
let detailAdventure = event.detail;
console.log("Event" + event.detail.name);
fetch("/api/planner", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
detailAdventure,
}),
})
.then((response) => {
if (!response.ok) {
return response.json().then((data) => {
throw new Error(
data.error || `Failed to add adventure - ${data?.message}`
);
});
}
return response.json();
})
.then((data) => {
// add to local array for instant view update
plans = [...plans, data.adventure];
// showToast("Adventure added successfully!");
// visitCount.update((n) => n + 1);
})
.catch((error) => {
console.error("Error:", error);
// showToast(error.message);
});
let newArray = await addAdventure(event.detail, plans);
if (newArray.length > 0) {
plans = newArray;
showToast("Adventure added successfully!");
} else {
showToast("Failed to add adventure");
}
};
</script>