Skip to content

Commit

Permalink
Updated - Profile - Profile photo
Browse files Browse the repository at this point in the history
- updated profile photo handling after uploadthing done
  • Loading branch information
pdovhomilja committed Nov 13, 2023
1 parent 73d372b commit 889c373
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
10 changes: 9 additions & 1 deletion app/[locale]/(routes)/profile/components/ProfilePhotoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useToast } from "@/components/ui/use-toast";
import { FileUploaderDropzone } from "@/components/ui/file-uploader-dropzone";

import useAvatarStore from "@/store/useAvatarStore";
import axios from "axios";

interface ProfileFormProps {
data: Users;
Expand All @@ -25,17 +26,24 @@ export function ProfilePhotoForm({ data }: ProfileFormProps) {
setAvatar(data.avatar);
}, [data.avatar, toast]);

const handleUploadSuccess = (newAvatar: string) => {
const handleUploadSuccess = async (newAvatar: string) => {
try {
setAvatar(newAvatar);
setAvatarStore(newAvatar);
await axios.put("/api/profile/updateProfilePhoto", { avatar: newAvatar });
toast({
title: "Profile photo updated.",
description: "Your profile photo has been updated.",
duration: 5000,
});
} catch (e) {
console.log(e);
toast({
variant: "default",
title: "Error updating profile photo.",
description: "There was an error updating your profile photo.",
duration: 5000,
});
} finally {
router.refresh();
}
Expand Down
52 changes: 52 additions & 0 deletions app/api/profile/updateProfilePhoto/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { authOptions } from "@/lib/auth";
import { prismadb } from "@/lib/prisma";
import { getServerSession } from "next-auth";
import { NextResponse } from "next/server";

export async function PUT(req: Request) {
const session = await getServerSession(authOptions);

if (!session) {
return NextResponse.json(
{ message: "Unauthorized" },
{
status: 401,
}
);
}

const body = await req.json();

if (!body.avatar) {
return NextResponse.json(
{ message: "No avatar provided" },
{
status: 400,
}
);
}

try {
await prismadb.users.update({
where: {
id: session.user.id,
},
data: {
avatar: body.avatar,
},
});
console.log("Profile photo updated");
return NextResponse.json(
{ message: "Profile photo updated" },
{ status: 200 }
);
} catch (e) {
console.log(e);
return NextResponse.json(
{ message: "Error updating profile photo" },
{
status: 500,
}
);
}
}
15 changes: 1 addition & 14 deletions app/api/uploadthing/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,7 @@ export const ourFileRouter = {
// 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);
//TODO: save file.url to database
await prismadb.users.update({
data: {
avatar: file.url,
},
where: {
id: metadata.userId,
},
});
}),
.onUploadComplete(async ({ metadata, file }) => {}),

//FileRoute for documents
pdfUploader: f({ pdf: { maxFileSize: "64MB", maxFileCount: 1 } })
Expand Down

6 comments on commit 889c373

@vercel
Copy link

@vercel vercel bot commented on 889c373 Nov 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nextcrm-others – ./

nextcrm-others-git-main-e-osvc.vercel.app
nextcrm-others-e-osvc.vercel.app
others.nextcrm.io

@vercel
Copy link

@vercel vercel bot commented on 889c373 Nov 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nextcrm-demo – ./

demo.nextcrm.io
nextcrm-demo-e-osvc.vercel.app
nextcrm-demo-git-main-e-osvc.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 889c373 Nov 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nextcrm-test – ./

nextcrm-test-git-main-e-osvc.vercel.app
nextcrm-test-e-osvc.vercel.app
test.nextcrm.io

@vercel
Copy link

@vercel vercel bot commented on 889c373 Nov 13, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 889c373 Nov 13, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 889c373 Nov 13, 2023

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.