Skip to content

Commit 30ad4a9

Browse files
committed
feat: add board slug availability check with real-time validation UI
1 parent 3202cc4 commit 30ad4a9

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

apps/web/src/views/board/components/UpdateBoardSlugForm.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { zodResolver } from "@hookform/resolvers/zod";
22
import { env } from "next-runtime-env";
33
import { useEffect } from "react";
44
import { useForm } from "react-hook-form";
5-
import { HiXMark } from "react-icons/hi2";
5+
import { HiCheck, HiMiniStar, HiXMark } from "react-icons/hi2";
66
import { z } from "zod";
77

88
import Button from "~/components/Button";
99
import Input from "~/components/Input";
10+
import { useDebounce } from "~/hooks/useDebounce";
1011
import { useModal } from "~/providers/modal";
1112
import { usePopup } from "~/providers/popup";
1213
import { api } from "~/utils/api";
@@ -50,6 +51,7 @@ export function UpdateBoardSlugForm({
5051
register,
5152
handleSubmit,
5253
formState: { isDirty, errors },
54+
watch,
5355
} = useForm<FormValues>({
5456
resolver: zodResolver(schema),
5557
values: {
@@ -58,6 +60,10 @@ export function UpdateBoardSlugForm({
5860
mode: "onChange",
5961
});
6062

63+
const slug = watch("slug");
64+
65+
const [debouncedSlug] = useDebounce(slug, 500);
66+
6167
const updateBoardSlug = api.board.update.useMutation({
6268
onError: () => {
6369
showPopup({
@@ -72,13 +78,29 @@ export function UpdateBoardSlugForm({
7278
},
7379
});
7480

81+
const checkBoardSlugAvailability =
82+
api.board.checkSlugAvailability.useQuery(
83+
{
84+
boardSlug: debouncedSlug,
85+
},
86+
{
87+
enabled:
88+
!!debouncedSlug && debouncedSlug !== boardSlug && !errors.slug,
89+
},
90+
);
91+
92+
const isBoardSlugAvailable = checkBoardSlugAvailability.data;
93+
7594
useEffect(() => {
7695
const nameElement: HTMLElement | null =
7796
document.querySelector<HTMLElement>("#board-slug");
7897
if (nameElement) nameElement.focus();
7998
}, []);
8099

81100
const onSubmit = (data: FormValues) => {
101+
if (!isBoardSlugAvailable) return;
102+
if (isBoardSlugAvailable?.isReserved) return;
103+
82104
updateBoardSlug.mutate({
83105
slug: data.slug,
84106
boardPublicId,
@@ -107,14 +129,23 @@ export function UpdateBoardSlugForm({
107129
<Input
108130
id="board-slug"
109131
{...register("slug")}
110-
errorMessage={errors.slug?.message}
132+
errorMessage={errors.slug?.message || (isBoardSlugAvailable?.isReserved
133+
? "This board URL has already been taken"
134+
: undefined)}
111135
prefix={`${env("NEXT_PUBLIC_BASE_URL")}/${workspaceSlug}/`}
112136
onKeyDown={async (e) => {
113137
if (e.key === "Enter") {
114138
e.preventDefault();
115139
await handleSubmit(onSubmit)();
116140
}
117141
}}
142+
iconRight={
143+
!!errors.slug?.message || isBoardSlugAvailable?.isReserved ? (
144+
<HiXMark className="h-4 w-4 text-red-500" />
145+
) : (
146+
<HiCheck className="h-4 w-4 dark:text-dark-1000" />
147+
)
148+
}
118149
/>
119150
</div>
120151
<div className="mt-12 flex items-center justify-end border-t border-light-600 px-5 pb-5 pt-5 dark:border-dark-600">
@@ -125,7 +156,8 @@ export function UpdateBoardSlugForm({
125156
disabled={
126157
!isDirty ||
127158
updateBoardSlug.isPending ||
128-
errors.slug?.message !== undefined
159+
errors.slug?.message !== undefined ||
160+
isBoardSlugAvailable?.isReserved
129161
}
130162
>
131163
Update

packages/api/src/routers/board.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TRPCError } from "@trpc/server";
22
import { z } from "zod";
33

44
import * as boardRepo from "@kan/db/repository/board.repo";
5+
import * as boardSlugRepo from "@kan/db/repository/boardSlug.repo";
56
import * as cardRepo from "@kan/db/repository/card.repo";
67
import * as activityRepo from "@kan/db/repository/cardActivity.repo";
78
import * as listRepo from "@kan/db/repository/list.repo";
@@ -352,4 +353,36 @@ export const boardRouter = createTRPCRouter({
352353

353354
return { success: true };
354355
}),
356+
checkSlugAvailability: publicProcedure
357+
.meta({
358+
openapi: {
359+
summary: "Check if a board slug is available",
360+
method: "GET",
361+
path: "/boards/check-slug-availability",
362+
description: "Checks if a board slug is available",
363+
tags: ["Boards"],
364+
protect: true,
365+
},
366+
})
367+
.input(
368+
z.object({
369+
boardSlug: z
370+
.string()
371+
.min(3)
372+
.max(24)
373+
.regex(/^(?![-]+$)[a-zA-Z0-9-]+$/),
374+
}),
375+
)
376+
.output(
377+
z.object({
378+
isReserved: z.boolean(),
379+
}),
380+
)
381+
.query(async ({ ctx, input }) => {
382+
const slug = input.boardSlug.toLowerCase();
383+
const existingBoard = await boardSlugRepo.getBoardSlug(ctx.db, slug);
384+
return {
385+
isReserved: !!existingBoard
386+
};
387+
}),
355388
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { eq } from "drizzle-orm";
2+
3+
import type { dbClient } from "@kan/db/client";
4+
import { boards } from "@kan/db/schema";
5+
6+
export const getBoardSlug = (db: dbClient, slug: string) => {
7+
return db.query.boards.findFirst({
8+
columns: {
9+
slug: true,
10+
},
11+
where: eq(boards.slug, slug),
12+
});
13+
};

0 commit comments

Comments
 (0)