svrjs-nextjs-website/app/(auth)/admin/downloads/page.tsx

135 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-06-20 17:37:45 +02:00
"use client";
2024-06-20 19:46:48 +02:00
import React from "react";
import { useForm, SubmitHandler } from "react-hook-form";
2024-06-20 17:37:45 +02:00
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
2024-07-10 08:10:55 +02:00
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
2024-06-20 17:37:45 +02:00
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
2024-06-20 19:46:48 +02:00
import { UploadButton, UploadDropzone } from "@/lib/uploadthing";
import { downloadSchema } from "@/lib/validations/validation";
import { useToast } from "@/components/ui/use-toast";
2024-06-20 17:37:45 +02:00
2024-07-10 08:10:55 +02:00
const DownloadsPage = () => {
const { toast } = useToast();
2024-07-10 08:10:55 +02:00
const form = useForm<z.infer<typeof downloadSchema>>({
resolver: zodResolver(downloadSchema),
defaultValues: {
fileName: "",
version: "",
downloadLink: "",
fileSize: "",
},
});
2024-06-20 17:37:45 +02:00
2024-07-10 08:10:55 +02:00
const onSubmit: SubmitHandler<z.infer<typeof downloadSchema>> = async (
data
) => {
const response = await fetch("/api/upload", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
2024-06-20 17:37:45 +02:00
2024-07-10 08:10:55 +02:00
if (response.ok) {
form.reset();
toast({ description: "Download Sucessfully Updated" });
2024-07-10 08:10:55 +02:00
console.log("Upload successful");
} else {
console.error("Upload failed");
toast({ description: "Uploading Failed", variant: "destructive" });
2024-07-10 08:10:55 +02:00
}
};
2024-06-20 17:37:45 +02:00
2024-07-10 08:10:55 +02:00
return (
<section id="downloads-page" className="wrapper container">
<h1 className="text-3xl font-bold py-6">Mods Form</h1>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="fileName"
render={({ field }) => (
<FormItem>
<FormLabel>File Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="version"
render={({ field }) => (
<FormItem>
<FormLabel>Version</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="downloadLink"
render={({ field }) => (
<FormItem>
<FormLabel>Download Link</FormLabel>
<UploadButton
endpoint="imageUploader"
onClientUploadComplete={(res) => {
field.onChange(res[0].url);
}}
onUploadError={(error: Error) => {
alert(`ERROR! ${error.message}`);
}}
/>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="fileSize"
render={({ field }) => (
<FormItem>
<FormLabel>File Size</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button
type="submit"
className="w-full text-lg rounded-full"
size={"lg"}
>
Submit
</Button>
</form>
</Form>
</section>
);
2024-06-20 17:37:45 +02:00
};
2024-07-10 08:10:55 +02:00
export default DownloadsPage;