Skip to content

Commit

Permalink
Work on ecommerce image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Scc33 committed Sep 13, 2024
1 parent 94e883d commit 36ccb4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 4 additions & 10 deletions app/ecommerce/api/uploadthing/core.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { createUploadthing, type FileRouter } from "uploadthing/next";
import { UploadThingError } from "uploadthing/server";
import { z } from "zod";

const f = createUploadthing();

const auth = (req: Request) => ({ id: "fakeId" }); // Fake auth function

// This runs on the server but because it is all in one repository we get the type safety in the frontend
export const ourFileRouter = {
imageUploader: f({ image: { maxFileSize: "4MB" } })
.input(z.object({ configId: z.string().optional() }))
.middleware(async ({ req }) => {
const user = await auth(req);

if (!user) throw new UploadThingError("Unauthorized");

return { userId: user.id };
.middleware(async ({ input }) => {
return { input }
})
.onUploadComplete(async ({ metadata, file }) => {
return { uploadedBy: metadata.userId };
const { configId } = metadata.input;
return { configId };
}),
} satisfies FileRouter;

Expand Down
10 changes: 9 additions & 1 deletion app/ecommerce/configure/upload/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ import Dropzone, { FileRejection } from "react-dropzone";
import { Image, Loader2, MousePointerSquareDashed } from "lucide-react";
import { Progress } from "../../components/progress";
import { useUploadThing } from "../../lib/uploadthing";
import { useRouter } from "next/navigation";

const Page = () => {
const [isDragOver, setIsDragOver] = useState<boolean>(false);
const [uploadProgress, setUploadProgress] = useState<number>(0);
const router = useRouter();

const {} = useUploadThing("imageUploader", {
onClientUploadComplete: ([data]) => {

const configId = data.serverData.configId;
startTransition(() => {
router.push(`/configure/design?id=${configId}`)
})
},
onUploadProgress(p) {
setUploadProgress(p);
}
})

Expand Down

1 comment on commit 36ccb4b

@vercel
Copy link

@vercel vercel bot commented on 36ccb4b Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.