Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
Almost working.
  • Loading branch information
cammoore committed Oct 8, 2024
1 parent 933281a commit 5b5ff46
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
21 changes: 11 additions & 10 deletions src/components/ProfileForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
<Container>
Expand All @@ -72,7 +73,7 @@ const ProfileForm = ({
<Col xs={4}>
<Form.Group controlId="email">
<Form.Label>Email</Form.Label>
<Form.Control type="text" {...register('email')} defaultValue={profile.email!} readOnly disabled />
<Form.Control type="text" {...register('email')} defaultValue={profile.email!} readOnly />
<Form.Text className="text-danger">{errors.email?.message}</Form.Text>
</Form.Group>
</Col>
Expand Down Expand Up @@ -103,7 +104,7 @@ const ProfileForm = ({
<Multiselect
options={interestNames}
isObject={false}
showCheckbox
// showCheckbox
hidePlaceholder
closeOnSelect={false}
onSelect={onChange}
Expand All @@ -124,7 +125,7 @@ const ProfileForm = ({
<Multiselect
options={projectNames}
isObject={false}
showCheckbox
// showCheckbox
hidePlaceholder
closeOnSelect={false}
onSelect={onChange}
Expand Down
45 changes: 35 additions & 10 deletions src/lib/dbActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function upsertProject(project: any) {
export async function updateProfile(profile: any) {
console.log(`updateProfile data: ${JSON.stringify(profile, null, 2)}`);
const dbProfile = await prisma.profile.upsert({
where: { email: profile.name },
where: { email: profile.email },
update: {
firstName: profile.firstName,
lastName: profile.lastName,
Expand All @@ -115,20 +115,45 @@ export async function updateProfile(profile: any) {
email: profile.email,
},
});
profile.interests.forEach(async (intere: string) => {
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;
}

0 comments on commit 5b5ff46

Please sign in to comment.