Skip to content

Commit

Permalink
feat: search by alternative titles in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
dinkelspiel committed May 5, 2024
1 parent 32aa82b commit 2889822
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
34 changes: 27 additions & 7 deletions app/(app)/dashboard/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { FilterStyle, useDashboardStore } from './state';
import useSwr from 'swr';
import fetcher from '@/client/fetcher';

const Dashboard = ({
userEntries: originalUserEntries,
Expand All @@ -45,13 +47,21 @@ const Dashboard = ({

userEntries,
setUserEntries,
setUserEntry,
} = useDashboardStore();

useEffect(() => {
setUserEntries(originalUserEntries);
}, [originalUserEntries]);

const {
data: queryResults,
error: queryError,
isLoading: queryIsLoading,
} = useSwr<number[], { error: string }>(
`/api/user/entries/search?q=${filterTitle}`,
fetcher
);

return (
<>
<Header>
Expand Down Expand Up @@ -158,13 +168,23 @@ const Dashboard = ({
}
})
.map(userEntry => {
if (
if (filterTitle !== '' && (queryIsLoading || queryError)) {
if (
filterTitle !== '' &&
!userEntry.entry.originalTitle
.toLowerCase()
.includes(filterTitle.toLowerCase())
) {
return;
}
} else if (
filterTitle !== '' &&
!userEntry.entry.originalTitle
.toLowerCase()
.includes(filterTitle.toLowerCase())
!queryIsLoading &&
queryResults
) {
return;
if (!queryResults.includes(userEntry.id)) {
return;
}
}

// if (
Expand Down
36 changes: 36 additions & 0 deletions app/api/user/entries/search/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { validateSessionToken } from '@/server/auth/validateSession';
import prisma from '@/server/db';
import { NextRequest } from 'next/server';

export const GET = async (request: NextRequest) => {
const user = await validateSessionToken();

if (!user) {
return Response.json({ error: 'You are not logged in' }, { status: 400 });
}

const query = request.nextUrl.searchParams.get('q');

if (!query) {
return Response.json({ error: 'No query provided' }, { status: 400 });
}

return Response.json(
(
await prisma.userEntry.findMany({
where: {
userId: user.id,
entry: {
alternativeTitles: {
some: {
title: {
contains: query,
},
},
},
},
},
})
).map(e => e.id)
);
};
7 changes: 7 additions & 0 deletions client/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default async function fetcher<JSON = any>(
input: RequestInfo,
init?: RequestInit
): Promise<JSON> {
const res = await fetch(input, init);
return res.json();
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"react-dom": "18.2.0",
"server-only": "^0.0.1",
"sonner": "^1.4.0",
"swr": "^2.2.5",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7",
"usehooks-ts": "^2.16.0",
Expand Down

0 comments on commit 2889822

Please sign in to comment.