diff --git a/src/lib/server/user.ts b/src/lib/server/user.ts index ba111ac..925fea9 100644 --- a/src/lib/server/user.ts +++ b/src/lib/server/user.ts @@ -1,9 +1,14 @@ import { redirect } from "@tanstack/react-router" import { createServerFn } from "@tanstack/react-start" +import { eq } from "drizzle-orm" import { db } from "@/integrations/drizzle" import { profiles, users } from "@/integrations/drizzle/db/schema" import { getSupabaseServerClient } from "@/integrations/supabase/supabase" -import { loginFormSchema, profileFormSchema, signupFormSchema } from "../validation/user" +import { + loginFormSchema, + profileFormSchema, + signupFormSchema +} from "../validation/user" export const getUser = createServerFn().handler(async () => { const supabase = getSupabaseServerClient() @@ -85,7 +90,6 @@ export const signupUser = createServerFn({ method: "POST" }) }) }) - export const getAllUsers = createServerFn().handler(async () => { const response = await db.select().from(users) return response @@ -94,17 +98,18 @@ export const getAllUsers = createServerFn().handler(async () => { export const createProfile = createServerFn({ method: "POST" }) .validator(profileFormSchema) .handler(async ({ data }) => { - await db.insert(profiles).values(data).returning(); + await db.insert(profiles).values(data).returning() }) -export const getProfile = createServerFn().handler(async (data) => { - const { id } = data; +export const getProfile = createServerFn({ method: "POST" }) + .validator((data: { id: string }) => data) + .handler(async ({ data }) => { + const { id } = data + const response = await db + .select() + .from(profiles) + .where(eq(profiles.id, id)) + .limit(1) - const response = await db - .select() - .from(profiles) - .where(eq(profiles.id, id)) - .limit(1); - - return response[0] ?? null; -}); \ No newline at end of file + return response[0] ?? null + }) diff --git a/src/lib/validation/user.ts b/src/lib/validation/user.ts index 130388c..d6a93af 100644 --- a/src/lib/validation/user.ts +++ b/src/lib/validation/user.ts @@ -12,7 +12,5 @@ export const signupFormSchema = z.object({ }) export const profileFormSchema= z.object({ - id: z.uuid(), - firstName: z.string(), - lastName: z.string().optional() + id: z.uuid() }) \ No newline at end of file diff --git a/src/routes/_authed.tsx b/src/routes/_authed.tsx index fd6ece9..5258201 100644 --- a/src/routes/_authed.tsx +++ b/src/routes/_authed.tsx @@ -1,4 +1,4 @@ -import { createFileRoute, redirect } from "@tanstack/react-router" +import { createFileRoute } from "@tanstack/react-router" import { getProfile } from "@/lib/server/user" export const Route = createFileRoute("/_authed")({ @@ -8,10 +8,11 @@ export const Route = createFileRoute("/_authed")({ // TODO: Redirect to login page } }, - loader: (context) => { - console.log(context); - const profile = getProfile() - console.log(profile) + loader: ({context: {queryClient, user}} ) => { + queryClient.ensureQueryData({ + queryKey: ["profile"], + queryFn: () => getProfile({ data: { id: user?.id as string } }) + }) }, errorComponent: ({ error }) => { if (error.message === "Not authenticated") { diff --git a/src/routes/_authed/post.tsx b/src/routes/_authed/post.tsx index f42d444..5f31cac 100644 --- a/src/routes/_authed/post.tsx +++ b/src/routes/_authed/post.tsx @@ -1,7 +1,7 @@ import { Button } from "@heroui/react" import { useQuery } from "@tanstack/react-query" import { createFileRoute } from "@tanstack/react-router" -import { getAllUsers } from "@/lib/server/user" +import { getAllUsers, getProfile } from "@/lib/server/user" export const Route = createFileRoute("/_authed/post")({ component: RouteComponent @@ -9,7 +9,7 @@ export const Route = createFileRoute("/_authed/post")({ function RouteComponent() { const navigate = Route.useNavigate() - + const { user } = Route.useRouteContext() const { data } = useQuery({ queryKey: ["users"], queryFn: async () => { @@ -17,7 +17,15 @@ function RouteComponent() { } }) + const { data: profile } = useQuery({ + queryKey: ["profile"], + queryFn: async () => { + return getProfile({ data: { id: user?.id as string } }) + } + }) + console.log(data) + console.log(profile) return (