iPheNoMeNaL-oG
iPheNoMeNaL-oG
PPrisma
Created by iPheNoMeNaL-oG on 7/15/2024 in #help-and-questions
Using upsert when `Notifications` table doesn't exist yet for user
It seems this is the way to create if it doesn't exist yet
await prisma.notifications.upsert({
where: { id },
update: {
type,
communication,
marketing,
social,
security,
mobile,
},
create: {
id,
userId,
type,
communication,
marketing,
social,
security,
mobile,
},
});
await prisma.notifications.upsert({
where: { id },
update: {
type,
communication,
marketing,
social,
security,
mobile,
},
create: {
id,
userId,
type,
communication,
marketing,
social,
security,
mobile,
},
});
but userId is not defined in create
3 replies
PPrisma
Created by iPheNoMeNaL-oG on 6/13/2024 in #help-and-questions
Updating user info in db using forms
this work correctly for and username is updated in the db
10 replies
PPrisma
Created by iPheNoMeNaL-oG on 6/13/2024 in #help-and-questions
Updating user info in db using forms
const defaultValues: Partial<ProfileFormValues> = {
bio: "Full stack software engineer interested in web3.",
urls: [
{ value: "https://shadcn.com" },
{ value: "http://twitter.com/something" },
],
};

export function ProfileForm({ session, updateUser }) {
const [username, setUsername] = useState(session.user.username);

useEffect(() => {
console.log("current session data", session);
}, []);

const form = useForm<ProfileFormValues>({
resolver: zodResolver(profileSchema),
defaultValues: {
...defaultValues,
username: username,
},
mode: "onChange",
});

const { fields, append } = useFieldArray({
name: "urls",
control: form.control,
});

async function onSubmit(data: ProfileFormValues) {
console.log("making sure this is original username", username);
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
});
updateUser(session.user.id, data.username);
}
const defaultValues: Partial<ProfileFormValues> = {
bio: "Full stack software engineer interested in web3.",
urls: [
{ value: "https://shadcn.com" },
{ value: "http://twitter.com/something" },
],
};

export function ProfileForm({ session, updateUser }) {
const [username, setUsername] = useState(session.user.username);

useEffect(() => {
console.log("current session data", session);
}, []);

const form = useForm<ProfileFormValues>({
resolver: zodResolver(profileSchema),
defaultValues: {
...defaultValues,
username: username,
},
mode: "onChange",
});

const { fields, append } = useFieldArray({
name: "urls",
control: form.control,
});

async function onSubmit(data: ProfileFormValues) {
console.log("making sure this is original username", username);
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
});
updateUser(session.user.id, data.username);
}
server action
async function updateUser(id: string, username: string) {
"use server";
console.log("updating user", id, username);
await prisma.user.update({
where: {
id: id,
},
data: {
username: username,
},
});
}

export default async function Profile() {
const session = await auth();
return (
<div className="flex min-h-screen w-full flex-col">
<main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-muted/40 p-4 md:gap-8 md:p-10">
<div className="mx-auto grid w-full max-w-6xl gap-2">
<h1 className="text-3xl font-semibold">Settings</h1>
</div>
{session && <ProfileForm session={session} updateUser={updateUser} />}
</main>
</div>
);
}
async function updateUser(id: string, username: string) {
"use server";
console.log("updating user", id, username);
await prisma.user.update({
where: {
id: id,
},
data: {
username: username,
},
});
}

export default async function Profile() {
const session = await auth();
return (
<div className="flex min-h-screen w-full flex-col">
<main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-muted/40 p-4 md:gap-8 md:p-10">
<div className="mx-auto grid w-full max-w-6xl gap-2">
<h1 className="text-3xl font-semibold">Settings</h1>
</div>
{session && <ProfileForm session={session} updateUser={updateUser} />}
</main>
</div>
);
}
10 replies
PPrisma
Created by iPheNoMeNaL-oG on 6/13/2024 in #help-and-questions
Updating user info in db using forms
Then inside my <ProfileForm/> client component I have the submit function of the form as the following
async function onSubmit(data: ProfileFormValues) {
console.log("making sure this is original username", username);
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
});
// update user using data from the form
}
async function onSubmit(data: ProfileFormValues) {
console.log("making sure this is original username", username);
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
});
// update user using data from the form
}
10 replies
PPrisma
Created by iPheNoMeNaL-oG on 6/13/2024 in #help-and-questions
Updating user info in db using forms
@Nurul (Prisma) Here's what I have setup for using server actions. In page.tsx for profile
import { ProfileForm } from "@/components/forms/profile-form";
import { auth } from "@/auth";
import prisma from "@/lib/prisma";

async function updateUser(id: string, data) {
"use server";
await prisma.user.update({
where: {
id: id,
},
data: {
username: data.username,
},
});
}

export default async function Profile() {
const session = await auth();
return (
<div className="flex min-h-screen w-full flex-col">
<main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-muted/40 p-4 md:gap-8 md:p-10">
<div className="mx-auto grid w-full max-w-6xl gap-2">
<h1 className="text-3xl font-semibold">Settings</h1>
</div>

<ProfileForm session={session} />
</main>
</div>
);
}
import { ProfileForm } from "@/components/forms/profile-form";
import { auth } from "@/auth";
import prisma from "@/lib/prisma";

async function updateUser(id: string, data) {
"use server";
await prisma.user.update({
where: {
id: id,
},
data: {
username: data.username,
},
});
}

export default async function Profile() {
const session = await auth();
return (
<div className="flex min-h-screen w-full flex-col">
<main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-muted/40 p-4 md:gap-8 md:p-10">
<div className="mx-auto grid w-full max-w-6xl gap-2">
<h1 className="text-3xl font-semibold">Settings</h1>
</div>

<ProfileForm session={session} />
</main>
</div>
);
}
I need to pass updateUser as a prop to <ProfileForm updateUser={updateUser}/> right?
10 replies
PPrisma
Created by iPheNoMeNaL-oG on 6/13/2024 in #help-and-questions
Updating user info in db using forms
Also is best to update by id instead of username?
10 replies