1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-29 09:49:38 +02:00

Update Navbar component to display visit count

This commit is contained in:
Sean Morley 2024-04-02 18:28:14 +00:00
parent 4b2306f812
commit ad568bb3fa
3 changed files with 28 additions and 6 deletions

View file

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { getNumberOfAdventures } from "../../services/adventureService"; import { visitCount } from '$lib/utils/stores/visitCountStore';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
async function goHome() { async function goHome() {
@ -11,6 +11,23 @@
async function goToFeatured() { async function goToFeatured() {
goto('/featured'); goto('/featured');
} }
let count = 0;
visitCount.subscribe((value) => {
count = value;
});
// Set the visit count to the number of adventures stored in local storage
const isBrowser = typeof window !== 'undefined';
if (isBrowser) {
const storedAdventures = localStorage.getItem('adventures');
if (storedAdventures) {
let parsed = JSON.parse(storedAdventures);
visitCount.set (parsed.length);
}
}
</script> </script>
<div class="navbar bg-base-100 flex flex-col md:flex-row"> <div class="navbar bg-base-100 flex flex-col md:flex-row">
<div class="navbar-start flex justify-around md:justify-start"> <div class="navbar-start flex justify-around md:justify-start">
@ -22,6 +39,6 @@
<a class="btn btn-ghost text-xl" href="/">AdventureLog 🗺️</a> <a class="btn btn-ghost text-xl" href="/">AdventureLog 🗺️</a>
</div> </div>
<div class="navbar-end flex justify-around md:justify-end mr-4"> <div class="navbar-end flex justify-around md:justify-end mr-4">
<p>Adventures: {getNumberOfAdventures()} </p> <p>Adventures: {count} </p>
</div> </div>
</div> </div>

View file

@ -0,0 +1,4 @@
import { onMount } from "svelte";
import { writable } from "svelte/store";
export const visitCount = writable(0);

View file

@ -2,9 +2,12 @@ import type { Adventure } from '$lib/utils/types';
let adventures: Adventure[] = []; let adventures: Adventure[] = [];
import { visitCount } from '$lib/utils/stores/visitCountStore';
// Check if localStorage is available (browser environment) // Check if localStorage is available (browser environment)
const isBrowser = typeof window !== 'undefined'; const isBrowser = typeof window !== 'undefined';
// Load adventures from localStorage on startup (only in the browser) // Load adventures from localStorage on startup (only in the browser)
if (isBrowser) { if (isBrowser) {
const storedAdventures = localStorage.getItem('adventures'); const storedAdventures = localStorage.getItem('adventures');
@ -28,6 +31,7 @@ export function addAdventure(adventure: Adventure) {
localStorage.setItem('adventures', JSON.stringify(adventures)); localStorage.setItem('adventures', JSON.stringify(adventures));
} }
console.log(adventures); console.log(adventures);
visitCount.update((n) => n + 1);
} }
export function getAdventures(): Adventure[] { export function getAdventures(): Adventure[] {
@ -39,6 +43,7 @@ export function removeAdventure(event: { detail: number; }) {
if (isBrowser) { if (isBrowser) {
localStorage.setItem('adventures', JSON.stringify(adventures)); localStorage.setItem('adventures', JSON.stringify(adventures));
} }
visitCount.update((n) => n - 1);
} }
export function saveEdit(adventure:Adventure) { export function saveEdit(adventure:Adventure) {
@ -61,10 +66,6 @@ export function saveEdit(adventure:Adventure) {
} }
} }
export function getNumberOfAdventures() {
return adventures.length;
}
export function clearAdventures() { export function clearAdventures() {
adventures = []; adventures = [];
if (isBrowser) { if (isBrowser) {