Updated form
This commit is contained in:
@@ -1,216 +0,0 @@
|
|||||||
"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 { createAdvanceUrl } from "@/lib/actions/url"
|
|
||||||
import { advancedUrlSchema } from "@/lib/schema/url"
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
|
||||||
import { useForm } from "react-hook-form"
|
|
||||||
import { toast } from "sonner"
|
|
||||||
import { z } from "zod"
|
|
||||||
|
|
||||||
type AdvancedUrlFormValues = z.infer<typeof advancedUrlSchema>
|
|
||||||
|
|
||||||
export function AdvancedUrlFormCard() {
|
|
||||||
const form = useForm<AdvancedUrlFormValues>({
|
|
||||||
resolver: zodResolver(advancedUrlSchema),
|
|
||||||
defaultValues: {
|
|
||||||
url: "",
|
|
||||||
slug: "",
|
|
||||||
title: "",
|
|
||||||
maxVisits: 0,
|
|
||||||
expDate: undefined,
|
|
||||||
forwardQueryParams: true,
|
|
||||||
crawlable: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
async function handleSubmit(data: AdvancedUrlFormValues) {
|
|
||||||
const res = await createAdvanceUrl(data)
|
|
||||||
|
|
||||||
if (res.error) {
|
|
||||||
toast.error(res.message)
|
|
||||||
} else {
|
|
||||||
toast.success(res.message)
|
|
||||||
form.reset()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Card>
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle>Create Advanced Short Link</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Create a short link with advanced configuration options.
|
|
||||||
</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}
|
|
||||||
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>
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
className="w-full"
|
|
||||||
disabled={form.formState.isSubmitting}
|
|
||||||
>
|
|
||||||
{form.formState.isSubmitting ? "Creating..." : "Create Short Link"}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,229 +0,0 @@
|
|||||||
"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>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
100
src/app/(admin)/dashboard/_components/simple-url-form-card.tsx
Normal file
100
src/app/(admin)/dashboard/_components/simple-url-form-card.tsx
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
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 { addUrl } from "@/lib/actions/url"
|
||||||
|
import { urlFormSchema } from "@/lib/schema/url"
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { useForm } from "react-hook-form"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
type UrlFormValues = z.infer<typeof urlFormSchema>
|
||||||
|
|
||||||
|
export function UrlFormCard() {
|
||||||
|
const form = useForm<UrlFormValues>({
|
||||||
|
resolver: zodResolver(urlFormSchema),
|
||||||
|
defaultValues: {
|
||||||
|
url: "",
|
||||||
|
slug: ""
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
async function handleSubmit(data: UrlFormValues) {
|
||||||
|
const res = await addUrl(data)
|
||||||
|
|
||||||
|
if (res.error) {
|
||||||
|
toast.error(res.message)
|
||||||
|
} else {
|
||||||
|
toast.success(res.message)
|
||||||
|
form.reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Create Short Link</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Enter a URL and create a custom slug for your short link.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Form {...form}>
|
||||||
|
<form
|
||||||
|
onSubmit={form.handleSubmit(handleSubmit)}
|
||||||
|
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</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder="my-custom-link"
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>
|
||||||
|
A unique identifier for your short link.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
className="w-full"
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
>
|
||||||
|
{form.formState.isSubmitting ? "Creating..." : "Create Short Link"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,29 +1,39 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
|
import { DatePicker } from "@/components/date-picker"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
|
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
|
||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
import { addUrl } from "@/lib/actions/url"
|
import { Switch } from "@/components/ui/switch"
|
||||||
import { urlFormSchema } from "@/lib/schema/url"
|
import { createAdvanceUrl, updateUrl } from "@/lib/actions/url"
|
||||||
|
import { urls } from "@/lib/drizzle/schema"
|
||||||
|
import { advancedUrlSchema, editUrlSchema } from "@/lib/schema/url"
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
|
import { useRouter } from "next/navigation"
|
||||||
import { useForm } from "react-hook-form"
|
import { useForm } from "react-hook-form"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
type UrlFormValues = z.infer<typeof urlFormSchema>
|
type AdvancedUrlFormValues = z.infer<typeof advancedUrlSchema>
|
||||||
|
type EditUrlFormValues = z.infer<typeof editUrlSchema>
|
||||||
|
|
||||||
export function UrlFormCard() {
|
function AdvancedUrlForm() {
|
||||||
const form = useForm<UrlFormValues>({
|
const form = useForm<AdvancedUrlFormValues>({
|
||||||
resolver: zodResolver(urlFormSchema),
|
resolver: zodResolver(advancedUrlSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
url: "",
|
url: "",
|
||||||
slug: ""
|
slug: "",
|
||||||
|
title: "",
|
||||||
|
maxVisits: 0,
|
||||||
|
expDate: undefined,
|
||||||
|
forwardQueryParams: true,
|
||||||
|
crawlable: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
async function handleSubmit(data: UrlFormValues) {
|
async function handleSubmit(formData: AdvancedUrlFormValues) {
|
||||||
const res = await addUrl(data)
|
const res = await createAdvanceUrl(formData)
|
||||||
|
|
||||||
if (res.error) {
|
if (res.error) {
|
||||||
toast.error(res.message)
|
toast.error(res.message)
|
||||||
@@ -34,19 +44,10 @@ export function UrlFormCard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
|
||||||
<CardHeader>
|
|
||||||
<CardTitle>Create Short Link</CardTitle>
|
|
||||||
<CardDescription>
|
|
||||||
Enter a URL and create a custom slug for your short link.
|
|
||||||
</CardDescription>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form
|
<form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-4">
|
||||||
onSubmit={form.handleSubmit(handleSubmit)}
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
className="space-y-4"
|
<div className="space-y-4">
|
||||||
>
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="url"
|
name="url"
|
||||||
@@ -54,10 +55,192 @@ export function UrlFormCard() {
|
|||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Original URL</FormLabel>
|
<FormLabel>Original URL</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input placeholder="https://example.com" {...field} />
|
||||||
placeholder="https://example.com"
|
</FormControl>
|
||||||
{...field}
|
<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
|
||||||
|
</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}
|
||||||
|
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>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
>
|
||||||
|
{form.formState.isSubmitting ? "Creating..." : "Create Short Link"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function EditUrlForm({ 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
router.push("/dashboard/list")
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<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>
|
</FormControl>
|
||||||
<FormDescription>
|
<FormDescription>
|
||||||
The URL you want to shorten.
|
The URL you want to shorten.
|
||||||
@@ -73,27 +256,187 @@ export function UrlFormCard() {
|
|||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Custom Slug</FormLabel>
|
<FormLabel>Custom Slug</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input placeholder="my-custom-link" {...field} />
|
||||||
placeholder="my-custom-link"
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormDescription>
|
<FormDescription>
|
||||||
A unique identifier for your short link.
|
A unique identifier for your short link (max 10 characters).
|
||||||
</FormDescription>
|
</FormDescription>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</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
|
||||||
|
</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</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</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</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
|
<Button
|
||||||
type="submit"
|
type="submit"
|
||||||
className="w-full"
|
className="flex-1"
|
||||||
disabled={form.formState.isSubmitting}
|
disabled={form.formState.isSubmitting}
|
||||||
>
|
>
|
||||||
{form.formState.isSubmitting ? "Creating..." : "Create Short Link"}
|
{form.formState.isSubmitting ? "Updating..." : "Update Short Link"}
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={handleCancel}
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type UrlFormCardProps = {
|
||||||
|
editMode: true
|
||||||
|
data: typeof urls.$inferSelect
|
||||||
|
} | {
|
||||||
|
editMode?: false
|
||||||
|
data?: never
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UrlFormCard({ editMode, data }: UrlFormCardProps) {
|
||||||
|
const getTitle = () => {
|
||||||
|
switch (editMode) {
|
||||||
|
case true:
|
||||||
|
return "Edit Short Link"
|
||||||
|
default:
|
||||||
|
return "Create Short Link"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getDescription = () => {
|
||||||
|
switch (editMode) {
|
||||||
|
case true:
|
||||||
|
return "Update the details of your short link."
|
||||||
|
default:
|
||||||
|
return "Create a short link with advanced configuration options."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const renderForm = () => {
|
||||||
|
switch (editMode) {
|
||||||
|
case true:
|
||||||
|
return <EditUrlForm data={data} />
|
||||||
|
default:
|
||||||
|
return <AdvancedUrlForm />
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>{getTitle()}</CardTitle>
|
||||||
|
<CardDescription>{getDescription()}</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
{renderForm()}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getSession } from "@/lib/auth/session"
|
import { getSession } from "@/lib/auth/session"
|
||||||
import { AdvancedUrlFormCard } from "../_components/advanced-url-form-card"
|
import { UrlFormCard } from "../_components/url-form-card"
|
||||||
|
|
||||||
export default async function DashboardCreatePage() {
|
export default async function DashboardCreatePage() {
|
||||||
const { session, redirect } = await getSession()
|
const { session, redirect } = await getSession()
|
||||||
@@ -13,7 +13,7 @@ export default async function DashboardCreatePage() {
|
|||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-foreground mb-2">Create Short Link</h1>
|
<h1 className="text-2xl font-bold text-foreground mb-2">Create Short Link</h1>
|
||||||
</div>
|
</div>
|
||||||
<AdvancedUrlFormCard />
|
<UrlFormCard />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export default function NotFound() {
|
|||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle>URL Not Found</CardTitle>
|
<CardTitle>URL Not Found</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
The short link you're trying to edit could not be found.
|
The short link you're trying to edit could not be found.
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
@@ -20,7 +20,7 @@ export default function NotFound() {
|
|||||||
</p>
|
</p>
|
||||||
<ul className="list-disc list-inside text-sm text-muted-foreground space-y-1">
|
<ul className="list-disc list-inside text-sm text-muted-foreground space-y-1">
|
||||||
<li>The link has been deleted</li>
|
<li>The link has been deleted</li>
|
||||||
<li>You don't have permission to edit this link</li>
|
<li>You don't have permission to edit this link</li>
|
||||||
<li>The link ID is invalid</li>
|
<li>The link ID is invalid</li>
|
||||||
</ul>
|
</ul>
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getSession } from "@/lib/auth/session"
|
import { getSession } from "@/lib/auth/session"
|
||||||
import { getUrlById } from "@/lib/db/urls"
|
import { getUrlById } from "@/lib/db/urls"
|
||||||
import { notFound } from "next/navigation"
|
import { notFound } from "next/navigation"
|
||||||
import { EditUrlFormCard } from "../../_components/edit-url-form-card"
|
import { UrlFormCard } from "../../_components/url-form-card"
|
||||||
|
|
||||||
export default async function EditPage({
|
export default async function EditPage({
|
||||||
params
|
params
|
||||||
@@ -27,7 +27,7 @@ export default async function EditPage({
|
|||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-foreground mb-2">Edit Short Link</h1>
|
<h1 className="text-2xl font-bold text-foreground mb-2">Edit Short Link</h1>
|
||||||
</div>
|
</div>
|
||||||
<EditUrlFormCard data={url} />
|
<UrlFormCard editMode data={url} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { getSession } from "@/lib/auth/session"
|
import { getSession } from "@/lib/auth/session"
|
||||||
import { getDashboardStats } from "@/lib/dashboard/stats"
|
import { getDashboardStats } from "@/lib/dashboard/stats"
|
||||||
import { LinkIcon, MousePointerClick, TrendingUp } from "lucide-react"
|
import { LinkIcon, MousePointerClick, TrendingUp } from "lucide-react"
|
||||||
|
import { UrlFormCard } from "./_components/simple-url-form-card"
|
||||||
import { StatsCard } from "./_components/stats-card"
|
import { StatsCard } from "./_components/stats-card"
|
||||||
import { UrlFormCard } from "./_components/url-form-card"
|
|
||||||
|
|
||||||
export default async function Dashboard() {
|
export default async function Dashboard() {
|
||||||
const { session, redirect } = await getSession()
|
const { session, redirect } = await getSession()
|
||||||
|
|||||||
Reference in New Issue
Block a user