From 5b5ff46c26fc381838e8748ad19c12f1bcee1699 Mon Sep 17 00:00:00 2001 From: Cam Moore Date: Tue, 8 Oct 2024 10:17:56 -1000 Subject: [PATCH] Checkpoint Almost working. --- src/components/ProfileForm.tsx | 21 ++++++++-------- src/lib/dbActions.ts | 45 ++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/components/ProfileForm.tsx b/src/components/ProfileForm.tsx index a0c00ba..6b3d39a 100644 --- a/src/components/ProfileForm.tsx +++ b/src/components/ProfileForm.tsx @@ -10,6 +10,7 @@ import swal from 'sweetalert'; import Multiselect from 'multiselect-react-dropdown'; import { IProfile, ProfileSchema } from '@/lib/validationSchemas'; import { Interest, Profile, Project } from '@prisma/client'; +import { updateProfile } from '@/lib/dbActions'; const ProfileForm = ({ profile, @@ -41,13 +42,13 @@ const ProfileForm = ({ const onSubmit = async (data: IProfile) => { console.log(data); - // const result = await upsertProject(data); - // if (result) { - // swal('Success!', 'Project data saved successfully!', 'success'); - // reset(); - // } else { - // swal('Error!', 'Failed to save project data!', 'error'); - // } + const result = await updateProfile(data); + if (result) { + swal('Success!', 'Project data saved successfully!', 'success'); + reset(); + } else { + swal('Error!', 'Failed to save project data!', 'error'); + } }; return ( @@ -72,7 +73,7 @@ const ProfileForm = ({ Email - + {errors.email?.message} @@ -103,7 +104,7 @@ const ProfileForm = ({ { - const dbInterest = await prisma.interest.findUnique({ - where: { name: intere }, - }); - const dbProfileInterest = await prisma.profileInterest.findMany({ - where: { profileId: dbProfile.id, interestId: dbInterest!.id }, + if (profile.interests) { + // Delete all profile interests + await prisma.profileInterest.deleteMany({ + where: { profileId: dbProfile.id }, }); - if (dbProfileInterest.length === 0) { + // Add the new profile interests + profile.interests.forEach(async (intere: string) => { + const dbInterest = await prisma.interest.findUnique({ + where: { name: intere }, + }); await prisma.profileInterest.create({ data: { profileId: dbProfile.id, interestId: dbInterest!.id, }, }); - } - }); + }); + } + if (profile.projects) { + // Delete all profile projects + await prisma.profileProject.deleteMany({ + where: { profileId: dbProfile.id }, + }); + // Delete all the profile projects + await prisma.profileProject.deleteMany({ + where: { profileId: dbProfile.id }, + }); + // Add the new profile projects + profile.projects.forEach(async (projectName: string) => { + const dbProject = await prisma.project.findUnique({ + where: { name: projectName }, + }); + await prisma.profileProject.create({ + data: { + profileId: dbProfile.id, + projectId: dbProject!.id, + }, + }); + }); + } + return dbProfile; }