feat - new changes

This commit is contained in:
juan 2025-08-13 19:29:19 +02:00
parent 96ebac5547
commit 974337af2b
4 changed files with 35 additions and 23 deletions

View File

@ -1,9 +1,14 @@
import { redirect } from "@tanstack/react-router" import { redirect } from "@tanstack/react-router"
import { createServerFn } from "@tanstack/react-start" import { createServerFn } from "@tanstack/react-start"
import { eq } from "drizzle-orm"
import { db } from "@/integrations/drizzle" import { db } from "@/integrations/drizzle"
import { profiles, users } from "@/integrations/drizzle/db/schema" import { profiles, users } from "@/integrations/drizzle/db/schema"
import { getSupabaseServerClient } from "@/integrations/supabase/supabase" 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 () => { export const getUser = createServerFn().handler(async () => {
const supabase = getSupabaseServerClient() const supabase = getSupabaseServerClient()
@ -85,7 +90,6 @@ export const signupUser = createServerFn({ method: "POST" })
}) })
}) })
export const getAllUsers = createServerFn().handler(async () => { export const getAllUsers = createServerFn().handler(async () => {
const response = await db.select().from(users) const response = await db.select().from(users)
return response return response
@ -94,17 +98,18 @@ export const getAllUsers = createServerFn().handler(async () => {
export const createProfile = createServerFn({ method: "POST" }) export const createProfile = createServerFn({ method: "POST" })
.validator(profileFormSchema) .validator(profileFormSchema)
.handler(async ({ data }) => { .handler(async ({ data }) => {
await db.insert(profiles).values(data).returning(); await db.insert(profiles).values(data).returning()
}) })
export const getProfile = createServerFn().handler(async (data) => { export const getProfile = createServerFn({ method: "POST" })
const { id } = data; .validator((data: { id: string }) => data)
.handler(async ({ data }) => {
const { id } = data
const response = await db const response = await db
.select() .select()
.from(profiles) .from(profiles)
.where(eq(profiles.id, id)) .where(eq(profiles.id, id))
.limit(1); .limit(1)
return response[0] ?? null; return response[0] ?? null
}); })

View File

@ -12,7 +12,5 @@ export const signupFormSchema = z.object({
}) })
export const profileFormSchema= z.object({ export const profileFormSchema= z.object({
id: z.uuid(), id: z.uuid()
firstName: z.string(),
lastName: z.string().optional()
}) })

View File

@ -1,4 +1,4 @@
import { createFileRoute, redirect } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import { getProfile } from "@/lib/server/user" import { getProfile } from "@/lib/server/user"
export const Route = createFileRoute("/_authed")({ export const Route = createFileRoute("/_authed")({
@ -8,10 +8,11 @@ export const Route = createFileRoute("/_authed")({
// TODO: Redirect to login page // TODO: Redirect to login page
} }
}, },
loader: (context) => { loader: ({context: {queryClient, user}} ) => {
console.log(context); queryClient.ensureQueryData({
const profile = getProfile() queryKey: ["profile"],
console.log(profile) queryFn: () => getProfile({ data: { id: user?.id as string } })
})
}, },
errorComponent: ({ error }) => { errorComponent: ({ error }) => {
if (error.message === "Not authenticated") { if (error.message === "Not authenticated") {

View File

@ -1,7 +1,7 @@
import { Button } from "@heroui/react" import { Button } from "@heroui/react"
import { useQuery } from "@tanstack/react-query" import { useQuery } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router" 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")({ export const Route = createFileRoute("/_authed/post")({
component: RouteComponent component: RouteComponent
@ -9,7 +9,7 @@ export const Route = createFileRoute("/_authed/post")({
function RouteComponent() { function RouteComponent() {
const navigate = Route.useNavigate() const navigate = Route.useNavigate()
const { user } = Route.useRouteContext()
const { data } = useQuery({ const { data } = useQuery({
queryKey: ["users"], queryKey: ["users"],
queryFn: async () => { 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(data)
console.log(profile)
return ( return (
<div> <div>