-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pagination and filtering to tags
- Loading branch information
1 parent
0011aaa
commit c0a369d
Showing
8 changed files
with
175 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { withWorkspace } from "@/lib/auth"; | ||
import { prisma } from "@/lib/prisma"; | ||
import { getTagsCountQuerySchema } from "@/lib/zod/schemas/tags"; | ||
import { getSearchParams } from "@dub/utils"; | ||
import { NextResponse } from "next/server"; | ||
|
||
// GET /api/tags - get all tags for a workspace | ||
export const GET = withWorkspace( | ||
async ({ req, workspace, headers }) => { | ||
const searchParams = getSearchParams(req.url); | ||
const { search } = getTagsCountQuerySchema.parse(searchParams); | ||
|
||
const count = await prisma.tag.count({ | ||
where: { | ||
projectId: workspace.id, | ||
...(search && { | ||
name: { | ||
contains: search, | ||
}, | ||
}), | ||
}, | ||
}); | ||
|
||
return NextResponse.json(count, { headers }); | ||
}, | ||
{ | ||
requiredPermissions: ["tags.read"], | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { fetcher } from "@dub/utils"; | ||
import useSWR from "swr"; | ||
import { z } from "zod"; | ||
import { getTagsCountQuerySchema } from "../zod/schemas/tags"; | ||
import useWorkspace from "./use-workspace"; | ||
|
||
const partialQuerySchema = getTagsCountQuerySchema.partial(); | ||
|
||
export default function useTagsCount({ | ||
query, | ||
}: { query?: z.infer<typeof partialQuerySchema> } = {}) { | ||
const { id } = useWorkspace(); | ||
|
||
const { data, error } = useSWR<number>( | ||
id && | ||
`/api/tags/count?${new URLSearchParams({ workspaceId: id, ...query } as Record<string, any>).toString()}`, | ||
fetcher, | ||
{ | ||
dedupingInterval: 60000, | ||
}, | ||
); | ||
|
||
return { | ||
data, | ||
loading: !error && data === undefined, | ||
error, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters