1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-29 17:59:36 +02:00

Add visit count functionality to Navbar and Log page

This commit is contained in:
Sean Morley 2024-04-10 17:47:20 +00:00
parent ae5e77f670
commit bc0e1b4db2
5 changed files with 59 additions and 7 deletions

View file

@ -5,6 +5,7 @@
import type { DatabaseUser } from "$lib/server/auth"; import type { DatabaseUser } from "$lib/server/auth";
export let user: any; export let user: any;
import UserAvatar from "./UserAvatar.svelte"; import UserAvatar from "./UserAvatar.svelte";
import { onMount } from "svelte";
async function goHome() { async function goHome() {
goto("/"); goto("/");
} }
@ -22,6 +23,15 @@
} }
let count = 0; let count = 0;
// get value from fetch /api/visitcount
onMount(async () => {
const res = await fetch("/api/visitcount");
const data = await res.json();
visitCount.set(data.visitCount);
});
visitCount.subscribe((value) => { visitCount.subscribe((value) => {
count = value; count = value;
}); });
@ -30,10 +40,6 @@
const isBrowser = typeof window !== "undefined"; const isBrowser = typeof window !== "undefined";
if (isBrowser) { if (isBrowser) {
const storedAdventures = localStorage.getItem("adventures"); const storedAdventures = localStorage.getItem("adventures");
if (storedAdventures) {
let parsed = JSON.parse(storedAdventures);
visitCount.set(parsed.length);
}
} }
</script> </script>
@ -57,12 +63,12 @@
<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: {count}</p>
{#if !user} {#if !user}
<button class="btn btn-primary ml-4" on:click={toToLogin}>Login</button> <button class="btn btn-primary ml-4" on:click={toToLogin}>Login</button>
<button class="btn btn-primary ml-4" on:click={toToSignup}>Signup</button> <button class="btn btn-primary ml-4" on:click={toToSignup}>Signup</button>
{/if} {/if}
{#if user} {#if user}
<p>Adventures: {count}</p>
<UserAvatar {user} /> <UserAvatar {user} />
<form method="post" action="/" use:enhance> <form method="post" action="/" use:enhance>
<button class="btn btn-primary ml-4">Logout</button> <button class="btn btn-primary ml-4">Logout</button>

View file

@ -1,3 +1,5 @@
import { writable } from "svelte/store"; import { writable } from "svelte/store";
export const visitCount = writable(0); let value = 0;
export const visitCount = writable(value);

View file

@ -9,4 +9,4 @@ export const load: LayoutServerLoad = async (event) => {
return { return {
user: null, user: null,
}; };
}; };

View file

@ -0,0 +1,36 @@
import type { RequestEvent } from "@sveltejs/kit";
import { count } from 'drizzle-orm';
import { eq } from "drizzle-orm";
import { userVisitedAdventures } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
// get the count of the number of adventures the user has visited
let result = await db
.select({ count: count() })
.from(userVisitedAdventures)
.where(eq(userVisitedAdventures.userId,event.locals.user.id))
.execute();
console.log(result[0].count);
return new Response(
JSON.stringify({
visitCount: result[0].count,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}

View file

@ -19,6 +19,7 @@
import mapDrawing from "$lib/assets/adventure_map.svg"; import mapDrawing from "$lib/assets/adventure_map.svg";
import EditModal from "$lib/components/EditModal.svelte"; import EditModal from "$lib/components/EditModal.svelte";
import { generateRandomString } from "$lib"; import { generateRandomString } from "$lib";
import { visitCount } from "$lib/utils/stores/visitCountStore";
let newName = ""; let newName = "";
let newLocation = ""; let newLocation = "";
@ -37,6 +38,11 @@
adventures = data.result.adventures; adventures = data.result.adventures;
}); });
let count = 0;
visitCount.subscribe((value) => {
count = value;
});
function showToast(action: string) { function showToast(action: string) {
toastAction = action; toastAction = action;
isShowingToast = true; isShowingToast = true;
@ -80,6 +86,7 @@
newName = ""; // Reset newName and newLocation after adding adventure newName = ""; // Reset newName and newLocation after adding adventure
newLocation = ""; newLocation = "";
showToast("added"); showToast("added");
visitCount.update((n) => n + 1);
}) })
.catch((error) => { .catch((error) => {
console.error("Error:", error); console.error("Error:", error);
@ -183,6 +190,7 @@
(adventure) => adventure.id !== event.detail, (adventure) => adventure.id !== event.detail,
); );
showToast("removed"); showToast("removed");
visitCount.update((n) => n - 1);
}) })
.catch((error) => { .catch((error) => {
console.error("Error:", error); console.error("Error:", error);