Skip to content

Commit

Permalink
feat: ssr rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Sep 14, 2023
1 parent 288e20f commit 50e4d0b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 52 deletions.
7 changes: 5 additions & 2 deletions src/components/profile/profile-details.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TDictionary } from "@/lib/langs"
import { serverTrpc } from "@/lib/trpc/server"
import UpdateAccount from "./update-account"

export default function ProfileDetails({ dictionary }: { dictionary: TDictionary }) {
export default async function ProfileDetails({ dictionary }: { dictionary: TDictionary }) {
const serverAccount = await serverTrpc.me.getAccount()

return (
<section className="mt-4 p-2 text-foreground">
<header>
Expand All @@ -10,7 +13,7 @@ export default function ProfileDetails({ dictionary }: { dictionary: TDictionary
{dictionary.profilePage.profileDetails.updateAccountDescription}
</p>
</header>
<UpdateAccount dictionary={dictionary} />
<UpdateAccount dictionary={dictionary} serverAccount={serverAccount} />
</section>
)
}
16 changes: 12 additions & 4 deletions src/components/profile/update-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as z from "zod"
import { useAccount } from "@/contexts/account"
import { TDictionary } from "@/lib/langs"
import { logger } from "@/lib/logger"
import { updateUserSchema } from "@/lib/schemas/user"
import { getAccountResponseSchema, updateUserSchema } from "@/lib/schemas/user"
import { trpc } from "@/lib/trpc/client"
import { handleMutationError } from "@/lib/utils/client-utils"
import NeedSavePopup from "../need-save-popup"
Expand All @@ -21,12 +21,20 @@ const nonSensibleSchema = updateUserSchema

type INonSensibleForm = z.infer<ReturnType<typeof nonSensibleSchema>>

export default function UpdateAccount({ dictionary }: { dictionary: TDictionary }) {
export default function UpdateAccount({
dictionary,
serverAccount,
}: {
dictionary: TDictionary
serverAccount: z.infer<ReturnType<typeof getAccountResponseSchema>>
}) {
const router = useRouter()
const utils = trpc.useContext()

const { update } = useSession()
const account = useAccount(dictionary)
const account = useAccount(dictionary, {
initialData: serverAccount,
})

const updateUserMutation = trpc.me.updateUser.useMutation({
onError: (error) => handleMutationError(error, dictionary, router),
Expand Down Expand Up @@ -75,7 +83,7 @@ export default function UpdateAccount({ dictionary }: { dictionary: TDictionary
label={dictionary.profilePage.profileDetails.username.label}
placeholder={dictionary.profilePage.profileDetails.username.placeholder}
type="text"
disabled={updateUserMutation.isLoading || !account.isFetched}
disabled={updateUserMutation.isLoading || account.isInitialLoading}
form={form}
name="username"
/>
Expand Down
14 changes: 11 additions & 3 deletions src/lib/server/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { initTRPC, TRPCError } from "@trpc/server"
import superjson from "superjson"
import { ZodError } from "zod"
import { getAuthApi } from "@/components/auth/require-auth"
import { apiRateLimiter } from "../rate-limit"
import { Context } from "../trpc/context"
import { throwableErrorsMessages } from "../utils"
Expand Down Expand Up @@ -41,11 +42,18 @@ const hasRateLimit = middleware(async (opts) => {
})
export const publicProcedure = t.procedure.use(hasRateLimit)
const isAuthenticated = middleware(async (opts) => {
const { ctx } = opts
if (!ctx.session) {
const { session } = await getAuthApi()

if (!session) {
throw new TRPCError({ code: "UNAUTHORIZED", message: throwableErrorsMessages.unauthorized })
}
return opts.next()

return opts.next({
ctx: {
...opts.ctx,
session,
},
})
})
const hasVerifiedEmail = middleware(async (opts) => {
const { ctx } = opts
Expand Down
38 changes: 0 additions & 38 deletions src/lib/trpc/api.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/lib/trpc/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch"
import { getAuthApi } from "@/components/auth/require-auth"
import { ITrpcContext } from "@/types"

export async function createContext(opts?: FetchCreateContextFnOptions) {
const { session } = await getAuthApi()

const response: ITrpcContext = {
session,
session: null,
headers: opts && Object.fromEntries(opts.req.headers),
req: opts && opts.req,
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/trpc/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createContext } from "./context"
import { appRouter } from "../server/routers/_app"

/**
* This client invokes procedures directly on the server without fetching over HTTP.
*/
export const serverTrpc = appRouter.createCaller(await createContext())
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Next.js",
"compilerOptions": {
"target": "es5",
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 50e4d0b

Please sign in to comment.