1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 13:15:18 +02:00

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 { import {
saveAdventure, saveAdventure,
removeAdventure, removeAdventure,
addAdventure,
} from "../../services/adventureService.js"; } from "../../services/adventureService.js";
import SucessToast from "$lib/components/SucessToast.svelte"; import SucessToast from "$lib/components/SucessToast.svelte";
import mapDrawing from "$lib/assets/adventure_map.svg"; 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; isShowingMoreFields = false;
let detailAdventure = event.detail; let newArray = await addAdventure(event.detail, plans);
console.log("Event" + event.detail.name); if (newArray.length > 0) {
plans = newArray;
fetch("/api/planner", { showToast("Adventure added successfully!");
method: "POST", } else {
headers: { showToast("Failed to add adventure");
"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);
});
}; };
</script> </script>

View file

@ -1,6 +1,8 @@
import type { Adventure } from "$lib/utils/types"; import type { Adventure } from "$lib/utils/types";
// json that maps the type to api routes /**
* Object containing the API routes for the different types of adventures.
*/
const apiRoutes: { [key: string]: string } = { const apiRoutes: { [key: string]: string } = {
planner: "/api/planner", planner: "/api/planner",
mylog: "/api/visits", mylog: "/api/visits",
@ -50,6 +52,12 @@ export async function saveAdventure(
return adventureArray; return adventureArray;
} }
/**
* Removes an adventure from the adventure array and sends a delete request to the server.
* @param adventure - The adventure to be removed.
* @param adventureArray - The array of adventures.
* @returns A promise that resolves to the updated adventure array.
*/
export async function removeAdventure( export async function removeAdventure(
adventure: Adventure, adventure: Adventure,
adventureArray: Adventure[] adventureArray: Adventure[]
@ -81,28 +89,33 @@ export async function removeAdventure(
} }
/** /**
* function removeAdventure(event: { detail: number }) { * Adds an adventure to the adventure array and sends a POST request to the specified URL.
console.log("Event ID " + event.detail); * @param {Adventure} adventure - The adventure to be added.
// send delete request to server at /api/visits * @param {Adventure[]} adventureArray - The array of adventures.
fetch("/api/visits", { * @returns {Promise<Adventure[]>} - A promise that resolves to the updated adventure array.
method: "DELETE", */
export async function addAdventure(
adventure: Adventure,
adventureArray: Adventure[]
): Promise<Adventure[]> {
let url = apiRoutes[adventure.type];
let detailAdventure = adventure;
// post request to /api/visits with adventure data
const response = await fetch(url, {
method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ id: event.detail }), body: JSON.stringify({ detailAdventure }),
}) })
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
console.log("Success:", data); adventureArray.push(data.adventure);
// remove adventure from array where id matches
plans = plans.filter((adventure) => adventure.id !== event.detail);
// showToast("Adventure removed successfully!");
// visitCount.update((n) => n - 1);
}) })
.catch((error) => { .catch((error) => {
console.error("Error:", error); console.error("Error:", error);
return [];
}); });
return adventureArray;
} }
*
*
*/