mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 04:35:19 +02:00
add search bar to navbar
This commit is contained in:
parent
23916e9e38
commit
c91c212fc0
5 changed files with 146 additions and 27 deletions
|
@ -1,30 +1,36 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { enhance } from "$app/forms";
|
import { enhance } from "$app/forms";
|
||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
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";
|
|
||||||
import InfoModal from "./InfoModal.svelte";
|
import InfoModal from "./InfoModal.svelte";
|
||||||
import infoIcon from "$lib/assets/info.svg";
|
|
||||||
import type { SubmitFunction } from "@sveltejs/kit";
|
import type { SubmitFunction } from "@sveltejs/kit";
|
||||||
async function goHome() {
|
import { onMount } from "svelte";
|
||||||
goto("/");
|
|
||||||
}
|
let searchVal: string = "";
|
||||||
async function goToLog() {
|
|
||||||
goto("/log");
|
|
||||||
}
|
|
||||||
async function goToFeatured() {
|
|
||||||
goto("/featured");
|
|
||||||
}
|
|
||||||
async function toToLogin() {
|
async function toToLogin() {
|
||||||
goto("/login");
|
goto("/login");
|
||||||
}
|
}
|
||||||
async function goToSignup() {
|
async function goToSignup() {
|
||||||
goto("/signup");
|
goto("/signup");
|
||||||
}
|
}
|
||||||
async function goToWorldTravel() {
|
|
||||||
goto("/worldtravel");
|
onMount(() => {
|
||||||
|
if (window.location.pathname === "/search") {
|
||||||
|
searchVal = new URLSearchParams(window.location.search).get("all") || "";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function goToSearch() {
|
||||||
|
let reload: boolean = false;
|
||||||
|
if (window.location.pathname === "/search") {
|
||||||
|
reload = true;
|
||||||
|
}
|
||||||
|
await goto("/search?all=" + searchVal);
|
||||||
|
if (reload) {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const submitUpdateTheme: SubmitFunction = ({ action }) => {
|
const submitUpdateTheme: SubmitFunction = ({ action }) => {
|
||||||
|
@ -86,6 +92,24 @@
|
||||||
<li>
|
<li>
|
||||||
<button on:click={() => goto("/featured")}>Featured</button>
|
<button on:click={() => goto("/featured")}>Featured</button>
|
||||||
</li>
|
</li>
|
||||||
|
{#if user}
|
||||||
|
<li>
|
||||||
|
<label class="input input-bordered flex items-center gap-2">
|
||||||
|
<input type="text" class="grow" placeholder="Search" />
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="currentColor"
|
||||||
|
class="w-4 h-4 opacity-70"
|
||||||
|
><path
|
||||||
|
fill-rule="evenodd"
|
||||||
|
d="M9.965 11.026a5 5 0 1 1 1.06-1.06l2.755 2.754a.75.75 0 1 1-1.06 1.06l-2.755-2.754ZM10.5 7a3.5 3.5 0 1 1-7 0 3.5 3.5 0 0 1 7 0Z"
|
||||||
|
clip-rule="evenodd"
|
||||||
|
/></svg
|
||||||
|
>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
{/if}
|
||||||
{#if !user}
|
{#if !user}
|
||||||
<li>
|
<li>
|
||||||
<button class="btn btn-primary" on:click={toToLogin}>Login</button>
|
<button class="btn btn-primary" on:click={toToLogin}>Login</button>
|
||||||
|
@ -125,6 +149,38 @@
|
||||||
>Featured</button
|
>Featured</button
|
||||||
>
|
>
|
||||||
</li>
|
</li>
|
||||||
|
{#if user}
|
||||||
|
<li>
|
||||||
|
<label class="input input-bordered flex items-center gap-2">
|
||||||
|
<form on:submit={() => goto("/search?all=" + searchVal)}>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="grow"
|
||||||
|
placeholder="Search"
|
||||||
|
bind:value={searchVal}
|
||||||
|
on:keydown={(event) => {
|
||||||
|
if (event.key === "Enter") {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
goToSearch();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="currentColor"
|
||||||
|
class="w-4 h-4 opacity-70"
|
||||||
|
><path
|
||||||
|
fill-rule="evenodd"
|
||||||
|
d="M9.965 11.026a5 5 0 1 1 1.06-1.06l2.755 2.754a.75.75 0 1 1-1.06 1.06l-2.755-2.754ZM10.5 7a3.5 3.5 0 1 1-7 0 3.5 3.5 0 0 1 7 0Z"
|
||||||
|
clip-rule="evenodd"
|
||||||
|
/></svg
|
||||||
|
>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
{/if}
|
||||||
{#if !user}
|
{#if !user}
|
||||||
<li>
|
<li>
|
||||||
<button class="btn btn-primary" on:click={toToLogin}>Login</button>
|
<button class="btn btn-primary" on:click={toToLogin}>Login</button>
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let data;
|
export let data;
|
||||||
import Footer from "$lib/components/Footer.svelte";
|
// import Footer from "$lib/components/Footer.svelte";
|
||||||
import Navbar from "$lib/components/Navbar.svelte";
|
import Navbar from "$lib/components/Navbar.svelte";
|
||||||
import type { SubmitFunction } from "@sveltejs/kit";
|
|
||||||
import "../app.css";
|
import "../app.css";
|
||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
|
@ -36,5 +35,5 @@
|
||||||
<main class="flex-grow">
|
<main class="flex-grow">
|
||||||
<slot />
|
<slot />
|
||||||
</main>
|
</main>
|
||||||
<Footer />
|
<!-- <Footer /> -->
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -132,8 +132,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(activityTypes);
|
|
||||||
|
|
||||||
// insert the adventure to the user's visited list
|
// insert the adventure to the user's visited list
|
||||||
let res = await db
|
let res = await db
|
||||||
.insert(adventureTable)
|
.insert(adventureTable)
|
||||||
|
@ -153,7 +151,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
let insertedId = res[0].insertedId;
|
let insertedId = res[0].insertedId;
|
||||||
console.log(insertedId);
|
|
||||||
|
|
||||||
body.detailAdventure.id = insertedId;
|
body.detailAdventure.id = insertedId;
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { redirect } from "@sveltejs/kit";
|
||||||
import type { PageServerLoad } from "./$types";
|
import type { PageServerLoad } from "./$types";
|
||||||
import { db } from "$lib/db/db.server";
|
import { db } from "$lib/db/db.server";
|
||||||
import { adventureTable } from "$lib/db/schema";
|
import { adventureTable } from "$lib/db/schema";
|
||||||
import { and, eq, arrayContains } from "drizzle-orm";
|
import { and, eq, arrayContains, ilike } from "drizzle-orm";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the page data based on the provided URL and locals.
|
* Loads the page data based on the provided URL and locals.
|
||||||
|
@ -21,11 +21,47 @@ export const load: PageServerLoad = async ({ url, locals }) => {
|
||||||
const params = Array.from(url.searchParams.entries());
|
const params = Array.from(url.searchParams.entries());
|
||||||
param = params[0][0];
|
param = params[0][0];
|
||||||
value = params[0][1];
|
value = params[0][1];
|
||||||
|
} else {
|
||||||
|
param = "all";
|
||||||
|
value = "";
|
||||||
}
|
}
|
||||||
// Activity type search
|
// Activity type search
|
||||||
if (param === "activity") {
|
if (param === "activity") {
|
||||||
|
return {
|
||||||
|
props: await activitySearch(value, locals),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (param === "location") {
|
||||||
|
return {
|
||||||
|
props: await locationSearch(value, locals),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (param === "name") {
|
||||||
|
return {
|
||||||
|
props: await nameSearch(value, locals),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (param == "all" || param == "") {
|
||||||
|
console.log("all");
|
||||||
|
const activityResults = await activitySearch(value, locals);
|
||||||
|
const locationResults = await locationSearch(value, locals);
|
||||||
|
const namesResults = await nameSearch(value, locals);
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
adventures: [
|
||||||
|
...activityResults.adventures,
|
||||||
|
...locationResults.adventures,
|
||||||
|
...namesResults.adventures,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function activitySearch(value: string, locals: any) {
|
||||||
let arr: string[] = [];
|
let arr: string[] = [];
|
||||||
arr.push(value);
|
arr.push(value.toLowerCase());
|
||||||
let res = await db
|
let res = await db
|
||||||
.select()
|
.select()
|
||||||
.from(adventureTable)
|
.from(adventureTable)
|
||||||
|
@ -36,12 +72,43 @@ export const load: PageServerLoad = async ({ url, locals }) => {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.execute();
|
.execute();
|
||||||
console.log(res);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
adventures: res,
|
||||||
adventures: res,
|
};
|
||||||
},
|
}
|
||||||
|
|
||||||
|
async function locationSearch(value: string, locals: any) {
|
||||||
|
let res = await db
|
||||||
|
.select()
|
||||||
|
.from(adventureTable)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
ilike(adventureTable.location, value),
|
||||||
|
eq(adventureTable.userId, locals.user.id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
return {
|
||||||
|
adventures: res,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function nameSearch(value: string, locals: any) {
|
||||||
|
let res = await db
|
||||||
|
.select()
|
||||||
|
.from(adventureTable)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
ilike(adventureTable.name, value),
|
||||||
|
eq(adventureTable.userId, locals.user.id)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
return {
|
||||||
|
adventures: res,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,6 +19,6 @@
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
<p>No results found</p>
|
<h1 class="text-center text-4xl font-bold mt-16">No Results Found</h1>
|
||||||
{/if}
|
{/if}
|
||||||
</main>
|
</main>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue