video-submission-form.tsx123 lines, 3.7 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { Card } from "@/components/ui/card"; |
| 5 | import { |
| 6 | Form, |
| 7 | FormControl, |
| 8 | FormField, |
| 9 | FormItem, |
| 10 | FormLabel, |
| 11 | } from "@/components/ui/form"; |
| 12 | import { Input } from "@/components/ui/input"; |
| 13 | import { VideoFormValues, videoSchema } from "@/lib/validations/videos"; |
| 14 | import { Link1Icon } from "@radix-ui/react-icons"; |
| 15 | import { useForm } from "react-hook-form"; |
| 16 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 17 | import { useState } from "react"; |
| 18 | import { useRouter } from "next/navigation"; |
| 19 | import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 20 | import { submitVideo } from "@/lib/api/videos"; |
| 21 | import { toast } from "sonner"; |
| 22 | |
| 23 | export default function VideoSubmissionForm() { |
| 24 | const [isSubmitting, setIsSubmitting] = useState(false); |
| 25 | const router = useRouter(); |
| 26 | const queryClient = useQueryClient(); |
| 27 | |
| 28 | const form = useForm<VideoFormValues>({ |
| 29 | resolver: zodResolver(videoSchema), |
| 30 | defaultValues: { |
| 31 | url: "", |
| 32 | }, |
| 33 | }); |
| 34 | |
| 35 | const { mutate } = useMutation({ |
| 36 | mutationFn: submitVideo, |
| 37 | onMutate: () => { |
| 38 | setIsSubmitting(true); |
| 39 | return { url: form.getValues().url }; |
| 40 | }, |
| 41 | onSuccess: () => { |
| 42 | toast.success("Video submitted successfully", { |
| 43 | description: "Your video will be processed shortly.", |
| 44 | }); |
| 45 | form.reset(); |
| 46 | queryClient.invalidateQueries({ queryKey: ["jobs"] }); |
| 47 | queryClient.invalidateQueries({ queryKey: ["videos"] }); |
| 48 | }, |
| 49 | onError: (error: Error | unknown) => { |
| 50 | // Extract the error message from the Axios error response |
| 51 | const errorResponse = error as { |
| 52 | response?: { |
| 53 | data?: { message?: string; error?: { message?: string } }; |
| 54 | status?: number; |
| 55 | }; |
| 56 | }; |
| 57 | const errorMessage = |
| 58 | errorResponse.response?.data?.message || |
| 59 | errorResponse.response?.data?.error?.message || |
| 60 | "Failed to submit video"; |
| 61 | |
| 62 | // For subscription errors (status 402 Payment Required), provide a link to upgrade |
| 63 | if (errorResponse.response?.status === 402) { |
| 64 | toast.error(errorMessage, { |
| 65 | description: "Click here to view subscription plans", |
| 66 | action: { |
| 67 | label: "Upgrade", |
| 68 | onClick: () => router.push("/subscriptions"), |
| 69 | }, |
| 70 | duration: 5000, |
| 71 | }); |
| 72 | } else { |
| 73 | toast.error("Failed to submit video", { |
| 74 | description: errorMessage, |
| 75 | }); |
| 76 | } |
| 77 | }, |
| 78 | onSettled: () => { |
| 79 | setIsSubmitting(false); |
| 80 | }, |
| 81 | }); |
| 82 | |
| 83 | const onSubmit = (data: VideoFormValues) => { |
| 84 | mutate(data); |
| 85 | }; |
| 86 | |
| 87 | return ( |
| 88 | <Card className="max-w-2xl p-6 shadow-lg transition-all hover:shadow-xl"> |
| 89 | <Form {...form}> |
| 90 | <form onSubmit={form.handleSubmit(onSubmit)}> |
| 91 | <FormField |
| 92 | control={form.control} |
| 93 | name="url" |
| 94 | render={({ field }) => ( |
| 95 | <FormItem> |
| 96 | <FormLabel>YouTube URL</FormLabel> |
| 97 | <FormControl> |
| 98 | <div className="relative"> |
| 99 | <div className="absolute left-3 top-3 text-gray-400"> |
| 100 | <Link1Icon className="size-4" /> |
| 101 | </div> |
| 102 | <Input |
| 103 | className="pl-10" |
| 104 | placeholder="https://www.youtube.com/watch?v=dQw4w9WgXcQ" |
| 105 | {...field} |
| 106 | /> |
| 107 | </div> |
| 108 | </FormControl> |
| 109 | </FormItem> |
| 110 | )} |
| 111 | /> |
| 112 | <Button |
| 113 | type="submit" |
| 114 | disabled={isSubmitting} |
| 115 | className="w-full transition-all mt-4" |
| 116 | > |
| 117 | {isSubmitting ? "Submitting..." : "Submit Video"} |
| 118 | </Button> |
| 119 | </form> |
| 120 | </Form> |
| 121 | </Card> |
| 122 | ); |
| 123 | } |