Added edit page
This commit is contained in:
229
src/app/(admin)/dashboard/_components/edit-url-form-card.tsx
Normal file
229
src/app/(admin)/dashboard/_components/edit-url-form-card.tsx
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { DatePicker } from "@/components/date-picker"
|
||||||
|
import { Button } from "@/components/ui/button"
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
|
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
|
||||||
|
import { Input } from "@/components/ui/input"
|
||||||
|
import { Switch } from "@/components/ui/switch"
|
||||||
|
import { updateUrl } from "@/lib/actions/url"
|
||||||
|
import { urls } from "@/lib/drizzle/schema"
|
||||||
|
import { editUrlSchema } from "@/lib/schema/url"
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { useRouter } from "next/navigation"
|
||||||
|
import { useForm } from "react-hook-form"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
type EditUrlFormValues = z.infer<typeof editUrlSchema>
|
||||||
|
|
||||||
|
export function EditUrlFormCard({ data }: { data: typeof urls.$inferSelect }) {
|
||||||
|
const router = useRouter()
|
||||||
|
const form = useForm<EditUrlFormValues>({
|
||||||
|
resolver: zodResolver(editUrlSchema),
|
||||||
|
defaultValues: {
|
||||||
|
url: data.url,
|
||||||
|
slug: data.slug,
|
||||||
|
title: data.title || "",
|
||||||
|
maxVisits: data.maxVisits || undefined,
|
||||||
|
expDate: data.expDate || undefined,
|
||||||
|
forwardQueryParams: data.forwardQueryParams || undefined,
|
||||||
|
crawlable: data.crawlable || undefined
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
async function handleSubmit(formData: EditUrlFormValues) {
|
||||||
|
const res = await updateUrl(data.id, formData)
|
||||||
|
|
||||||
|
if (res.error) {
|
||||||
|
toast.error(res.message)
|
||||||
|
} else {
|
||||||
|
toast.success(res.message)
|
||||||
|
router.push("/dashboard/list")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Edit Short Link</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Update the details of your short link.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Form {...form}>
|
||||||
|
<form
|
||||||
|
onSubmit={form.handleSubmit(handleSubmit)}
|
||||||
|
className="space-y-4"
|
||||||
|
>
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
|
<div className="space-y-4">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="url"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Original URL</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder="https://example.com"
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
The URL you want to shorten.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="slug"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Custom Slug (Optional)</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder="my-custom-link"
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
A unique identifier for your short link (max 10 characters).
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="forwardQueryParams"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex items-center justify-between rounded-lg border p-4">
|
||||||
|
<div className="space-y-0.5">
|
||||||
|
<FormLabel className="text-base">
|
||||||
|
Forward Query Parameters
|
||||||
|
</FormLabel>
|
||||||
|
<FormDescription>
|
||||||
|
Forward query parameters from the short link to the destination URL.
|
||||||
|
</FormDescription>
|
||||||
|
</div>
|
||||||
|
<FormControl>
|
||||||
|
<Switch
|
||||||
|
checked={field.value}
|
||||||
|
onCheckedChange={field.onChange}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="crawlable"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex items-center justify-between rounded-lg border p-4">
|
||||||
|
<div className="space-y-0.5">
|
||||||
|
<FormLabel className="text-base">
|
||||||
|
Crawlable (Optional)
|
||||||
|
</FormLabel>
|
||||||
|
<FormDescription>
|
||||||
|
Allow search engines to crawl and index this link.
|
||||||
|
</FormDescription>
|
||||||
|
</div>
|
||||||
|
<FormControl>
|
||||||
|
<Switch
|
||||||
|
checked={field.value ?? false}
|
||||||
|
onCheckedChange={field.onChange}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="title"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Title (Optional)</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder="My Link Title"
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
A descriptive title for your link (max 100 characters).
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="maxVisits"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Max Visits (Optional)</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
placeholder="100"
|
||||||
|
{...field}
|
||||||
|
value={field.value || ""}
|
||||||
|
onChange={(e) => field.onChange(e.target.value ? parseInt(e.target.value) : undefined)}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
Maximum number of visits before the link expires.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="expDate"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Expiration Date (Optional)</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<DatePicker
|
||||||
|
value={field.value}
|
||||||
|
onChange={field.onChange}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
When this link should expire and become inaccessible.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
className="flex-1"
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
>
|
||||||
|
{form.formState.isSubmitting ? "Updating..." : "Update Short Link"}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => router.push("/dashboard/list")}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -37,7 +37,7 @@ type UrlRecord = typeof urls.$inferSelect & {
|
|||||||
visits?: (typeof visits.$inferSelect)[]
|
visits?: (typeof visits.$inferSelect)[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const columns: ColumnDef<UrlRecord>[] = [
|
const columns: ColumnDef<UrlRecord>[] = [
|
||||||
{
|
{
|
||||||
accessorKey: "slug",
|
accessorKey: "slug",
|
||||||
header: ({ column }) => {
|
header: ({ column }) => {
|
||||||
@@ -282,6 +282,12 @@ export function UrlsDataTable({ data }: UrlsDataTableProps) {
|
|||||||
<Copy className="mr-2 h-4 w-4" />
|
<Copy className="mr-2 h-4 w-4" />
|
||||||
Copy URL
|
Copy URL
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem asChild>
|
||||||
|
<Link href={`/dashboard/edit/${urlRecord.id}`}>
|
||||||
|
<Copy className="mr-2 h-4 w-4" />
|
||||||
|
Edit URL
|
||||||
|
</Link>
|
||||||
|
</DropdownMenuItem>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
className="text-red-600"
|
className="text-red-600"
|
||||||
|
|||||||
44
src/app/(admin)/dashboard/edit/[id]/not-found.tsx
Normal file
44
src/app/(admin)/dashboard/edit/[id]/not-found.tsx
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { Button } from "@/components/ui/button"
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
|
import Link from "next/link"
|
||||||
|
|
||||||
|
export default function NotFound() {
|
||||||
|
return (
|
||||||
|
<div className="container mx-auto py-8">
|
||||||
|
<div className="max-w-2xl mx-auto">
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>URL Not Found</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
The short link you're trying to edit could not be found.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
This could happen if:
|
||||||
|
</p>
|
||||||
|
<ul className="list-disc list-inside text-sm text-muted-foreground space-y-1">
|
||||||
|
<li>The link has been deleted</li>
|
||||||
|
<li>You don't have permission to edit this link</li>
|
||||||
|
<li>The link ID is invalid</li>
|
||||||
|
</ul>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button asChild>
|
||||||
|
<Link href="/dashboard/list">
|
||||||
|
View All Links
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
<Button asChild variant="outline">
|
||||||
|
<Link href="/dashboard/create">
|
||||||
|
Create New Link
|
||||||
|
</Link>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
33
src/app/(admin)/dashboard/edit/[id]/page.tsx
Normal file
33
src/app/(admin)/dashboard/edit/[id]/page.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { getSession } from "@/lib/auth/session"
|
||||||
|
import { getUrlById } from "@/lib/db/urls"
|
||||||
|
import { notFound } from "next/navigation"
|
||||||
|
import { EditUrlFormCard } from "../../_components/edit-url-form-card"
|
||||||
|
|
||||||
|
export default async function EditPage({
|
||||||
|
params
|
||||||
|
}: {
|
||||||
|
params: Promise<{ id: string }>
|
||||||
|
}) {
|
||||||
|
const { session, redirect } = await getSession()
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
redirect("/sign-in")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { id } = await params
|
||||||
|
|
||||||
|
const url = await getUrlById(id)
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
notFound()
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="p-6 space-y-6">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-2xl font-bold text-foreground mb-2">Edit Short Link</h1>
|
||||||
|
</div>
|
||||||
|
<EditUrlFormCard data={url} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -27,26 +27,30 @@ export async function getUrlBySlug(slug: string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function insertUrl(data: typeof urls.$inferInsert) {
|
export async function getUrlById(id: string) {
|
||||||
"use cache"
|
"use cache"
|
||||||
|
|
||||||
revalidateTag("url")
|
cacheTag("urls")
|
||||||
|
|
||||||
|
return await db.query.urls.findFirst({
|
||||||
|
where: eq(urls.id, id)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function insertUrl(data: typeof urls.$inferInsert) {
|
||||||
|
revalidateTag("urls")
|
||||||
|
|
||||||
return await db.insert(urls).values(data)
|
return await db.insert(urls).values(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateUrl(id: string, data: Omit<Partial<typeof urls.$inferInsert>, "id">) {
|
export async function updateUrl(id: string, data: Omit<Partial<typeof urls.$inferInsert>, "id">) {
|
||||||
"use cache"
|
revalidateTag("urls")
|
||||||
|
|
||||||
revalidateTag("url")
|
|
||||||
|
|
||||||
return await db.update(urls).set(data).where(eq(urls.id, id))
|
return await db.update(urls).set(data).where(eq(urls.id, id))
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteUrl(id: string) {
|
export async function deleteUrl(id: string) {
|
||||||
"use cache"
|
revalidateTag("urls")
|
||||||
|
|
||||||
revalidateTag("url")
|
|
||||||
|
|
||||||
return await db.delete(urls).where(eq(urls.id, id))
|
return await db.delete(urls).where(eq(urls.id, id))
|
||||||
}
|
}
|
||||||
@@ -60,8 +64,6 @@ export async function getVisitsBySlugById(id: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function trackVisit(data: typeof visits.$inferInsert) {
|
export async function trackVisit(data: typeof visits.$inferInsert) {
|
||||||
"use cache"
|
|
||||||
|
|
||||||
revalidateTag("visits")
|
revalidateTag("visits")
|
||||||
|
|
||||||
return await db.insert(visits).values(data)
|
return await db.insert(visits).values(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user