diff --git a/app/api/uploadthing/core.ts b/app/api/uploadthing/core.ts new file mode 100644 index 0000000..6b36404 --- /dev/null +++ b/app/api/uploadthing/core.ts @@ -0,0 +1,34 @@ +import { createUploadthing, type FileRouter } from "uploadthing/next"; +import { UploadThingError } from "uploadthing/server"; + +const f = createUploadthing(); + +const auth = (req: Request) => ({ id: "fakeId" }); // Fake auth function + +// FileRouter for your app, can contain multiple FileRoutes +export const ourFileRouter = { + // Define as many FileRoutes as you like, each with a unique routeSlug + imageUploader: f({ image: { maxFileSize: "4MB" } }) + // Set permissions and file types for this FileRoute + .middleware(async ({ req }) => { + // This code runs on your server before upload + const user = await auth(req); + + // If you throw, the user will not be able to upload + if (!user) throw new UploadThingError("Unauthorized"); + + // Whatever is returned here is accessible in onUploadComplete as `metadata` + return { userId: user.id }; + }) + .onUploadComplete(async ({ metadata, file }) => { + // This code RUNS ON YOUR SERVER after upload + console.log("Upload complete for userId:", metadata.userId); + + console.log("file url", file.url); + + // !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback + return { uploadedBy: metadata.userId }; + }), +} satisfies FileRouter; + +export type OurFileRouter = typeof ourFileRouter; diff --git a/app/api/uploadthing/route.ts b/app/api/uploadthing/route.ts new file mode 100644 index 0000000..be94bb0 --- /dev/null +++ b/app/api/uploadthing/route.ts @@ -0,0 +1,8 @@ +import { createNextRouteHandler } from "uploadthing/next"; + +import { ourFileRouter } from "./core"; + +// Export routes for Next App Router +export const { GET, POST } = createNextRouteHandler({ + router: ourFileRouter, +}); diff --git a/components/shared/EventForm.tsx b/components/shared/EventForm.tsx index e84b65b..be1023d 100644 --- a/components/shared/EventForm.tsx +++ b/components/shared/EventForm.tsx @@ -30,6 +30,7 @@ import { useRouter } from "next/navigation"; import { createEvent, updateEvent } from "@/lib/actions/event.actions"; import { IEvent } from "@/lib/database/models/event.model"; import Dropdown from "./Dropdown"; +import { FileUploader } from "./FileUploader"; type EventFormProps = { userId: string; @@ -39,6 +40,8 @@ type EventFormProps = { }; const EventForm = ({ userId, type, event, eventId }: EventFormProps) => { + const [files, setFiles] = useState([]); + const initialValues = event && type === "Update" ? { @@ -116,16 +119,22 @@ const EventForm = ({ userId, type, event, eventId }: EventFormProps) => { )} /> - {/* ( - + + + )} - /> */} + />
diff --git a/components/shared/FileUploader.tsx b/components/shared/FileUploader.tsx new file mode 100644 index 0000000..b00dc7b --- /dev/null +++ b/components/shared/FileUploader.tsx @@ -0,0 +1,68 @@ +/* eslint-disable react-hooks/exhaustive-deps */ +"use client"; + +import { useCallback, Dispatch, SetStateAction } from "react"; +// import type { FileWithPath } from "@uploadthing/react"; +import { useDropzone } from "@uploadthing/react/hooks"; +import { generateClientDropzoneAccept } from "uploadthing/client"; + +import { Button } from "@/components/ui/button"; +import { convertFileToUrl } from "@/lib/utils"; +import Image from "next/image"; + +type FileUploaderProps = { + onFieldChange: (url: string) => void; + imageUrl: string; + setFiles: Dispatch>; +}; + +export function FileUploader({ + imageUrl, + onFieldChange, + setFiles, +}: FileUploaderProps) { + const onDrop = useCallback((acceptedFiles: File[]) => { + setFiles(acceptedFiles); + onFieldChange(convertFileToUrl(acceptedFiles[0])); + }, []); + + const { getRootProps, getInputProps } = useDropzone({ + onDrop, + accept: "image/*" ? generateClientDropzoneAccept(["image/*"]) : undefined, + }); + + return ( +
+ + + {imageUrl ? ( +
+ image +
+ ) : ( +
+ file upload +

Drag photo here

+

SVG, PNG, JPG

+ +
+ )} +
+ ); +} diff --git a/lib/uploadthing.ts b/lib/uploadthing.ts new file mode 100644 index 0000000..352adc4 --- /dev/null +++ b/lib/uploadthing.ts @@ -0,0 +1,6 @@ +import { generateReactHelpers } from "@uploadthing/react/hooks"; + +import type { OurFileRouter } from "@/app/api/uploadthing/core"; + +export const { useUploadThing, uploadFiles } = + generateReactHelpers(); diff --git a/package-lock.json b/package-lock.json index 849ab47..b7c245b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@radix-ui/react-separator": "^1.1.0", "@radix-ui/react-slot": "^1.1.0", "@stripe/stripe-js": "^2.2.1", + "@uploadthing/react": "^6.0.3", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "lucide-react": "^0.399.0", @@ -32,7 +33,7 @@ "svix": "^1.24.0", "tailwind-merge": "^2.3.0", "tailwindcss-animate": "^1.0.7", - "uploadthing": "^6.13.2", + "uploadthing": "^6.0.4", "zod": "^3.23.8" }, "devDependencies": { @@ -1605,11 +1606,60 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "node_modules/@uploadthing/dropzone": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@uploadthing/dropzone/-/dropzone-0.4.1.tgz", + "integrity": "sha512-RHSpo/2kg/mrRSYQA4EKlyvkOCYWOeE2+QQYW9YiUvWCuawnTfD7DQvk8RN/nYXi1Sw7/v0NegmQpiVELVGtnA==", + "dependencies": { + "file-selector": "^0.6.0" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0", + "solid-js": "^1.7.11", + "svelte": "^4.2.12", + "vue": "^3.4.0" + }, + "peerDependenciesMeta": { + "react": { + "optional": true + }, + "solid-js": { + "optional": true + }, + "svelte": { + "optional": true + }, + "vue": { + "optional": true + } + } + }, "node_modules/@uploadthing/mime-types": { "version": "0.2.10", "resolved": "https://registry.npmjs.org/@uploadthing/mime-types/-/mime-types-0.2.10.tgz", "integrity": "sha512-kz3F0oEgAyts25NAGXlUBCWh3mXonbSOQJFGFMawHuIgbUbnzXbe4w5WI+0XdneCbjNmikfWrdWrs8m/7HATfQ==" }, + "node_modules/@uploadthing/react": { + "version": "6.7.2", + "resolved": "https://registry.npmjs.org/@uploadthing/react/-/react-6.7.2.tgz", + "integrity": "sha512-9Mdslj1S24qbISA/LE3In5OFQ/U7OxdMbBlTPkJxKPkh+UW7BqZS0XuRTfkZYPkdBGDoXw5aH7VG3D/vGgpmxg==", + "dependencies": { + "@uploadthing/dropzone": "0.4.1", + "@uploadthing/shared": "6.7.8", + "file-selector": "^0.6.0", + "tailwind-merge": "^2.2.1" + }, + "peerDependencies": { + "next": "*", + "react": "^17.0.2 || ^18.0.0", + "uploadthing": "6.13.2" + }, + "peerDependenciesMeta": { + "next": { + "optional": true + } + } + }, "node_modules/@uploadthing/shared": { "version": "6.7.8", "resolved": "https://registry.npmjs.org/@uploadthing/shared/-/shared-6.7.8.tgz", @@ -3197,6 +3247,17 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/file-selector": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/file-selector/-/file-selector-0.6.0.tgz", + "integrity": "sha512-QlZ5yJC0VxHxQQsQhXvBaC7VRJ2uaxTf+Tfpu4Z/OcVQJVpZO+DGU0rkoVW5ce2SccxugvpBJoMvUs59iILYdw==", + "dependencies": { + "tslib": "^2.4.0" + }, + "engines": { + "node": ">= 12" + } + }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", diff --git a/package.json b/package.json index 91186bf..fd2fbc3 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@radix-ui/react-separator": "^1.1.0", "@radix-ui/react-slot": "^1.1.0", "@stripe/stripe-js": "^2.2.1", + "@uploadthing/react": "^6.0.3", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "lucide-react": "^0.399.0", @@ -33,7 +34,7 @@ "svix": "^1.24.0", "tailwind-merge": "^2.3.0", "tailwindcss-animate": "^1.0.7", - "uploadthing": "^6.13.2", + "uploadthing": "^6.0.4", "zod": "^3.23.8" }, "devDependencies": {