diff --git a/src/lib/server/sort-filter-util.ts b/src/lib/server/sort-filter-util.ts new file mode 100644 index 0000000..2093503 --- /dev/null +++ b/src/lib/server/sort-filter-util.ts @@ -0,0 +1,55 @@ +import type { Prisma } from "@prisma/client"; + +export const createFilter = (filter: string | null) => { + const search: Prisma.ItemWhereInput = {}; + if (filter === "unclaimed") { + search.AND = [ + { + pledgedById: null + }, + { + publicPledgedById: null + } + ]; + } else if (filter === "claimed") { + search.OR = [ + { + pledgedById: { + not: null + } + }, + { + publicPledgedById: { + not: null + } + } + ]; + } + return search; +}; + +export const createSorts = (sort: string | null, direction: string | null) => { + let orderBy: Prisma.ItemOrderByWithRelationInput[] = []; + if (sort === "price" && direction && (direction === "asc" || direction === "desc")) { + orderBy = [ + { + itemPrice: { + value: direction + } + } + ]; + } else { + orderBy = [ + { + displayOrder: { + sort: "asc", + nulls: "last" + } + }, + { + id: "asc" + } + ]; + } + return orderBy; +}; diff --git a/src/routes/lists/[id]/+page.server.ts b/src/routes/lists/[id]/+page.server.ts index 745e4fc..50479bb 100644 --- a/src/routes/lists/[id]/+page.server.ts +++ b/src/routes/lists/[id]/+page.server.ts @@ -2,8 +2,9 @@ import { client } from "$lib/server/prisma"; import { error } from "@sveltejs/kit"; import type { PageServerLoad } from "./$types"; import { getConfig } from "$lib/server/config"; +import { createFilter, createSorts } from "$lib/server/sort-filter-util"; -export const load = (async ({ params }) => { +export const load = (async ({ params, url }) => { const list = await client.publicList.findUnique({ select: { user: true, @@ -22,11 +23,17 @@ export const load = (async ({ params }) => { error(404, "Public list not found"); } + const filter = createFilter(url.searchParams.get("filter")); + filter.userId = list.user.id; + filter.groupId = list.groupId; + + const sort = url.searchParams.get("sort"); + const direction = url.searchParams.get("dir"); + const orderBy = createSorts(sort, direction); + const items = await client.item.findMany({ - where: { - userId: list.user.id, - groupId: list.groupId - }, + where: filter, + orderBy: orderBy, include: { addedBy: { select: { @@ -56,6 +63,11 @@ export const load = (async ({ params }) => { } }); + if (sort === "price" && direction === "asc") { + // need to re-sort when descending since Prisma can't order with nulls last + items.sort((a, b) => (a.itemPrice?.value ?? Infinity) - (b.itemPrice?.value ?? Infinity)); + } + return { user: { name: list.user.name diff --git a/src/routes/lists/[id]/+page.svelte b/src/routes/lists/[id]/+page.svelte index 1f301df..3eca11e 100644 --- a/src/routes/lists/[id]/+page.svelte +++ b/src/routes/lists/[id]/+page.svelte @@ -44,6 +44,10 @@ }); onDestroy(() => eventSource?.close()); + $effect(() => { + allItems = data.items; + }); + const subscribeToEvents = () => { eventSource = new EventSource(`${$page.url.pathname}/events`); eventSource.addEventListener(SSEvents.item.update, (e) => { @@ -85,12 +89,10 @@