-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
40 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
This file was deleted.
Oops, something went wrong.
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,185 @@ | ||
"use client"; | ||
|
||
import type { z } from "zod"; | ||
import { CreateLinkSchema } from "@/server/schemas"; | ||
import { useState, type ReactNode } from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { toast } from "sonner"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
|
||
import { api } from "@/server/trpc/react"; | ||
|
||
import Alert from "@/ui/alert"; | ||
import { Button } from "@/ui/button"; | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/ui/dialog"; | ||
import { | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/ui/form"; | ||
import { Input, Textarea } from "@/ui/input"; | ||
import { LoaderIcon, PlusIcon, RocketIcon, ShuffleIcon } from "lucide-react"; | ||
|
||
interface CreateLinkProps { | ||
children: ReactNode; | ||
} | ||
|
||
export function CreateLink(props: CreateLinkProps) { | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const [open, setOpen] = useState<boolean>(false); | ||
const [message, setMessage] = useState<string>(""); | ||
const [isError, setError] = useState<boolean>(false); | ||
|
||
// Main form: | ||
const form = useForm<z.infer<typeof CreateLinkSchema>>({ | ||
resolver: zodResolver(CreateLinkSchema), | ||
defaultValues: { | ||
url: "", | ||
slug: "", | ||
description: "", | ||
}, | ||
}); | ||
|
||
// Create link mutation: | ||
const { mutate } = api.links.createLink.useMutation({ | ||
onSuccess: () => { | ||
setLoading(false); | ||
setOpen(false); | ||
toast("Link created successfully"); | ||
form.reset(); | ||
}, | ||
onError: () => { | ||
setLoading(false); | ||
setError(true); | ||
setMessage( | ||
"Slug already exists. Please try another one or click 'Randomize' button.", | ||
); | ||
}, | ||
}); | ||
|
||
// Form Submit method: | ||
const onSubmit = (values: z.infer<typeof CreateLinkSchema>) => { | ||
// Check if slug & url are equals to prevent infinite redirect => | ||
setError(false); | ||
setMessage(""); | ||
if (values.slug === values.url) { | ||
setLoading(false); | ||
setError(true); | ||
setMessage("The URL and the slug cannot be the same"); | ||
return; | ||
} | ||
setLoading(true); | ||
mutate(values); | ||
}; | ||
|
||
const handleGenerateRandomSlug = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
const randomSlug = Math.random().toString(36).substring(7); | ||
form.setValue("slug", randomSlug); | ||
}; | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild>{props.children}</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader className="mb-2"> | ||
<DialogTitle>Create new link</DialogTitle> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> | ||
<div className="space-y-5"> | ||
<FormField | ||
control={form.control} | ||
name="url" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Destination URL:</FormLabel> | ||
<FormControl> | ||
<Input | ||
{...field} | ||
placeholder="https://" | ||
disabled={loading} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="slug" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Short link:</FormLabel> | ||
<FormControl> | ||
<div className="relative flex items-center"> | ||
<Input | ||
{...field} | ||
placeholder="mylink" | ||
disabled={loading} | ||
/> | ||
<Button | ||
onClick={handleGenerateRandomSlug} | ||
variant="secondary" | ||
className="absolute right-0" | ||
> | ||
<ShuffleIcon size={14} /> | ||
<span>Randomize</span> | ||
</Button> | ||
</div> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="description" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Description (optional):</FormLabel> | ||
<FormControl> | ||
<Textarea | ||
{...field} | ||
placeholder="Enter a description" | ||
disabled={loading} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
{isError && <Alert variant="error">{message}</Alert>} | ||
</div> | ||
<DialogFooter> | ||
<DialogClose asChild> | ||
<Button variant="ghost" disabled={loading}> | ||
Cancel | ||
</Button> | ||
</DialogClose> | ||
<Button type="submit" disabled={loading}> | ||
{loading ? ( | ||
<LoaderIcon size={16} className="animate-spin" /> | ||
) : ( | ||
<RocketIcon size={16} /> | ||
)} | ||
<span>{loading ? "Creating..." : "Create"}</span> | ||
</Button> | ||
</DialogFooter> | ||
</form> | ||
</Form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
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