From 87cc6da518a9ecb39c217a6c796d1f1b9a5c50ce Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Sat, 4 May 2024 17:27:51 +0000 Subject: [PATCH] feat: Add validation for adventure name in CreateNewAdventure component The code changes include adding validation for the adventure name in the CreateNewAdventure component. If the name is empty, an alert is displayed and the adventure creation is prevented. This enhancement ensures that users provide a name for the adventure before creating it. --- src/lib/components/CreateNewAdventure.svelte | 5 +++++ src/services/adventureService.ts | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/lib/components/CreateNewAdventure.svelte b/src/lib/components/CreateNewAdventure.svelte index 615b8e4..7dd1639 100644 --- a/src/lib/components/CreateNewAdventure.svelte +++ b/src/lib/components/CreateNewAdventure.svelte @@ -26,6 +26,10 @@ function create() { addActivityType(); + if (newAdventure.name.trim() === "") { + alert("Name is required"); + return; + } dispatch("create", newAdventure); console.log(newAdventure); } @@ -70,6 +74,7 @@ diff --git a/src/services/adventureService.ts b/src/services/adventureService.ts index b39492d..8559702 100644 --- a/src/services/adventureService.ts +++ b/src/services/adventureService.ts @@ -1,3 +1,4 @@ +import { visitCount } from "$lib/utils/stores/visitCountStore"; import type { Adventure } from "$lib/utils/types"; /** @@ -111,6 +112,9 @@ export async function addAdventure( .then((response) => response.json()) .then((data) => { adventureArray.push(data.adventure); + if (data.adventure.type === "mylog") { + incrementVisitCount(1); + } }) .catch((error) => { console.error("Error:", error); @@ -119,3 +123,11 @@ export async function addAdventure( return adventureArray; } + +/** + * Increments the visit count by the specified amount. + * @param {number} amount - The amount to increment the visit count by. + */ +export function incrementVisitCount(amount: number) { + visitCount.update((n) => n + 1); +}