discord-questions.trpc.io/m/1311807422939140147

Preview meta tags from the discord-questions.trpc.io website.

Linked Hostnames

5

Thumbnail

Search Engine Appearance

Google

https://discord-questions.trpc.io/m/1311807422939140147

Possible to build this custom hook? - tRPC

Hello! I'm facing a challenge and hope to get some guidance here. I'm building an admin dashboard for creating and editing data - let's just say it's product data. I built a product management form that I want to use for both creating and editing a product, because the fields are the same. I'd like to build a custom hook which returns multiple `useQuery`s or `useMutation`s for a tRPC route. The hook might look like this: ```ts // single query/mutation const mutation = useApi((router) => router.admin.product.edit) const query = useApi((router) => router.public.product.details) // or multiple hooks for a router const { editMutation, createMutation } = useApi((router) => router.admin.product) // ^ // | with type error, if tRPC route (e.g. `admin.product.edit`) does not exist ``` I've tried creating such a hook but it's not very great and absolutely not type-safe. Any idea how this could be properly implemented? Would appreciate any help :) ```ts export const useManager = <FormData extends FieldValues>( router: keyof AppRouter["admin"], dataMode: "create" | "edit" ) => { const nextRouter = useRouter() function getMutation(): UseTRPCMutationResult<FormData, any, any, any> { if (dataMode === "create") { // @ts-ignore return trpc.admin[router].create.useMutation() } else { // @ts-ignore return trpc.admin[router].edit.useMutation() } } const mutation = getMutation() async function submit(data: FormData) { return await mutation.mutateAsync(data) as FormData } // @ts-ignore const deleteMutation: UseTRPCMutationResult<FormData, any, any, any> = trpc.admin[router].delete.useMutation() async function purge(slug: string) { return await deleteMutation.mutateAsync({ slug }) } const isLoading = mutation.isPending || deleteMutation.isPending return { submit, isLoading, redirect: nextRouter.push, purge } } ```



Bing

Possible to build this custom hook? - tRPC

https://discord-questions.trpc.io/m/1311807422939140147

Hello! I'm facing a challenge and hope to get some guidance here. I'm building an admin dashboard for creating and editing data - let's just say it's product data. I built a product management form that I want to use for both creating and editing a product, because the fields are the same. I'd like to build a custom hook which returns multiple `useQuery`s or `useMutation`s for a tRPC route. The hook might look like this: ```ts // single query/mutation const mutation = useApi((router) => router.admin.product.edit) const query = useApi((router) => router.public.product.details) // or multiple hooks for a router const { editMutation, createMutation } = useApi((router) => router.admin.product) // ^ // | with type error, if tRPC route (e.g. `admin.product.edit`) does not exist ``` I've tried creating such a hook but it's not very great and absolutely not type-safe. Any idea how this could be properly implemented? Would appreciate any help :) ```ts export const useManager = <FormData extends FieldValues>( router: keyof AppRouter["admin"], dataMode: "create" | "edit" ) => { const nextRouter = useRouter() function getMutation(): UseTRPCMutationResult<FormData, any, any, any> { if (dataMode === "create") { // @ts-ignore return trpc.admin[router].create.useMutation() } else { // @ts-ignore return trpc.admin[router].edit.useMutation() } } const mutation = getMutation() async function submit(data: FormData) { return await mutation.mutateAsync(data) as FormData } // @ts-ignore const deleteMutation: UseTRPCMutationResult<FormData, any, any, any> = trpc.admin[router].delete.useMutation() async function purge(slug: string) { return await deleteMutation.mutateAsync({ slug }) } const isLoading = mutation.isPending || deleteMutation.isPending return { submit, isLoading, redirect: nextRouter.push, purge } } ```



DuckDuckGo

https://discord-questions.trpc.io/m/1311807422939140147

Possible to build this custom hook? - tRPC

Hello! I'm facing a challenge and hope to get some guidance here. I'm building an admin dashboard for creating and editing data - let's just say it's product data. I built a product management form that I want to use for both creating and editing a product, because the fields are the same. I'd like to build a custom hook which returns multiple `useQuery`s or `useMutation`s for a tRPC route. The hook might look like this: ```ts // single query/mutation const mutation = useApi((router) => router.admin.product.edit) const query = useApi((router) => router.public.product.details) // or multiple hooks for a router const { editMutation, createMutation } = useApi((router) => router.admin.product) // ^ // | with type error, if tRPC route (e.g. `admin.product.edit`) does not exist ``` I've tried creating such a hook but it's not very great and absolutely not type-safe. Any idea how this could be properly implemented? Would appreciate any help :) ```ts export const useManager = <FormData extends FieldValues>( router: keyof AppRouter["admin"], dataMode: "create" | "edit" ) => { const nextRouter = useRouter() function getMutation(): UseTRPCMutationResult<FormData, any, any, any> { if (dataMode === "create") { // @ts-ignore return trpc.admin[router].create.useMutation() } else { // @ts-ignore return trpc.admin[router].edit.useMutation() } } const mutation = getMutation() async function submit(data: FormData) { return await mutation.mutateAsync(data) as FormData } // @ts-ignore const deleteMutation: UseTRPCMutationResult<FormData, any, any, any> = trpc.admin[router].delete.useMutation() async function purge(slug: string) { return await deleteMutation.mutateAsync({ slug }) } const isLoading = mutation.isPending || deleteMutation.isPending return { submit, isLoading, redirect: nextRouter.push, purge } } ```

  • General Meta Tags

    6
    • title
      Possible to build this custom hook? - tRPC
    • charset
      utf-8
    • viewport
      width=device-width, initial-scale=1
    • next-size-adjust
    • description
      Hello! I'm facing a challenge and hope to get some guidance here. I'm building an admin dashboard for creating and editing data - let's just say it's product data. I built a product management form that I want to use for both creating and editing a product, because the fields are the same. I'd like to build a custom hook which returns multiple `useQuery`s or `useMutation`s for a tRPC route. The hook might look like this: ```ts // single query/mutation const mutation = useApi((router) => router.admin.product.edit) const query = useApi((router) => router.public.product.details) // or multiple hooks for a router const { editMutation, createMutation } = useApi((router) => router.admin.product) // ^ // | with type error, if tRPC route (e.g. `admin.product.edit`) does not exist ``` I've tried creating such a hook but it's not very great and absolutely not type-safe. Any idea how this could be properly implemented? Would appreciate any help :) ```ts export const useManager = <FormData extends FieldValues>( router: keyof AppRouter["admin"], dataMode: "create" | "edit" ) => { const nextRouter = useRouter() function getMutation(): UseTRPCMutationResult<FormData, any, any, any> { if (dataMode === "create") { // @ts-ignore return trpc.admin[router].create.useMutation() } else { // @ts-ignore return trpc.admin[router].edit.useMutation() } } const mutation = getMutation() async function submit(data: FormData) { return await mutation.mutateAsync(data) as FormData } // @ts-ignore const deleteMutation: UseTRPCMutationResult<FormData, any, any, any> = trpc.admin[router].delete.useMutation() async function purge(slug: string) { return await deleteMutation.mutateAsync({ slug }) } const isLoading = mutation.isPending || deleteMutation.isPending return { submit, isLoading, redirect: nextRouter.push, purge } } ```
  • Open Graph Meta Tags

    3
    • og:title
      Possible to build this custom hook? - tRPC
    • og:description
      Hello! I'm facing a challenge and hope to get some guidance here. I'm building an admin dashboard for creating and editing data - let's just say it's product data. I built a product management form that I want to use for both creating and editing a product, because the fields are the same. I'd like to build a custom hook which returns multiple `useQuery`s or `useMutation`s for a tRPC route. The hook might look like this: ```ts // single query/mutation const mutation = useApi((router) => router.admin.product.edit) const query = useApi((router) => router.public.product.details) // or multiple hooks for a router const { editMutation, createMutation } = useApi((router) => router.admin.product) // ^ // | with type error, if tRPC route (e.g. `admin.product.edit`) does not exist ``` I've tried creating such a hook but it's not very great and absolutely not type-safe. Any idea how this could be properly implemented? Would appreciate any help :) ```ts export const useManager = <FormData extends FieldValues>( router: keyof AppRouter["admin"], dataMode: "create" | "edit" ) => { const nextRouter = useRouter() function getMutation(): UseTRPCMutationResult<FormData, any, any, any> { if (dataMode === "create") { // @ts-ignore return trpc.admin[router].create.useMutation() } else { // @ts-ignore return trpc.admin[router].edit.useMutation() } } const mutation = getMutation() async function submit(data: FormData) { return await mutation.mutateAsync(data) as FormData } // @ts-ignore const deleteMutation: UseTRPCMutationResult<FormData, any, any, any> = trpc.admin[router].delete.useMutation() async function purge(slug: string) { return await deleteMutation.mutateAsync({ slug }) } const isLoading = mutation.isPending || deleteMutation.isPending return { submit, isLoading, redirect: nextRouter.push, purge } } ```
    • og:image
      https://discord-questions.trpc.io/og/post?id=1311807422939140147
  • Twitter Meta Tags

    4
    • twitter:card
      summary_large_image
    • twitter:title
      Possible to build this custom hook? - tRPC
    • twitter:description
      Hello! I'm facing a challenge and hope to get some guidance here. I'm building an admin dashboard for creating and editing data - let's just say it's product data. I built a product management form that I want to use for both creating and editing a product, because the fields are the same. I'd like to build a custom hook which returns multiple `useQuery`s or `useMutation`s for a tRPC route. The hook might look like this: ```ts // single query/mutation const mutation = useApi((router) => router.admin.product.edit) const query = useApi((router) => router.public.product.details) // or multiple hooks for a router const { editMutation, createMutation } = useApi((router) => router.admin.product) // ^ // | with type error, if tRPC route (e.g. `admin.product.edit`) does not exist ``` I've tried creating such a hook but it's not very great and absolutely not type-safe. Any idea how this could be properly implemented? Would appreciate any help :) ```ts export const useManager = <FormData extends FieldValues>( router: keyof AppRouter["admin"], dataMode: "create" | "edit" ) => { const nextRouter = useRouter() function getMutation(): UseTRPCMutationResult<FormData, any, any, any> { if (dataMode === "create") { // @ts-ignore return trpc.admin[router].create.useMutation() } else { // @ts-ignore return trpc.admin[router].edit.useMutation() } } const mutation = getMutation() async function submit(data: FormData) { return await mutation.mutateAsync(data) as FormData } // @ts-ignore const deleteMutation: UseTRPCMutationResult<FormData, any, any, any> = trpc.admin[router].delete.useMutation() async function purge(slug: string) { return await deleteMutation.mutateAsync({ slug }) } const isLoading = mutation.isPending || deleteMutation.isPending return { submit, isLoading, redirect: nextRouter.push, purge } } ```
    • twitter:image
      https://discord-questions.trpc.io/og/post?id=1311807422939140147
  • Link Tags

    8
    • canonical
      https://discord-questions.trpc.io/m/1311807422939140147
    • icon
      https://cdn.answeroverflow.com/867764511159091230/6af104c7f0f39a12fcd55bd7bd28928f/icon.png
    • preload
      https://www.answeroverflow.com/_next/static/media/4f05ba3a6752a328-s.p.woff2
    • preload
      https://www.answeroverflow.com/_next/static/media/9cf9c6e84ed13b5e-s.p.woff2
    • preload
      /discord/2.png

Links

7