diff --git a/app/AddAccountForm.tsx b/app/AddAccountForm.tsx index 7bc1b3d..ef4c2c8 100644 --- a/app/AddAccountForm.tsx +++ b/app/AddAccountForm.tsx @@ -65,7 +65,7 @@ const FormSchema = z.object({ const AddAccountForm: FC = () => { const router = useRouter(); - const { isLoaded, userId } = useAuth(); + const { isLoaded } = useAuth(); const form = useForm>({ mode: "onChange", @@ -78,20 +78,7 @@ const AddAccountForm: FC = () => { const onSubmit = async (values: z.infer) => { try { - if (!userId) { - throw new Error("User not found."); - } - - const valuesWithUserId: { - address: string; - name: string; - userId: string; - } = { - ...values, - userId: userId, - }; - - saveOrUpdateAccount(valuesWithUserId); + saveOrUpdateAccount(values); toast.success("Address has been created"); form.reset(); router.refresh(); diff --git a/app/actions/saveOrUpdateAccount.ts b/app/actions/saveOrUpdateAccount.ts index 4c2a339..e9d63e2 100644 --- a/app/actions/saveOrUpdateAccount.ts +++ b/app/actions/saveOrUpdateAccount.ts @@ -1,15 +1,19 @@ "use server"; import { database } from "@/lib/database"; +import { auth } from "@clerk/nextjs"; import { accounts, accountsToChains, chains } from "db/schema"; import { eq } from "drizzle-orm"; export const saveOrUpdateAccount = async (values: { address: string; name: string; - userId: string; }) => { const db = await database(); + const { userId } = auth(); + if (!userId) { + throw new Error("User not found."); + } const _chains = await db .select() .from(chains) @@ -23,14 +27,14 @@ export const saveOrUpdateAccount = async (values: { .values({ name: values.name, address: values.address, - userId: values.userId, + userId: userId, }) .onConflictDoUpdate({ target: accounts.address, set: { address: values.address, name: values.name, - userId: values.userId, + userId: userId, }, }) .returning();