jotaijs / jotai-trpc

MIT License
122 stars 7 forks source link

Mutation #15

Closed drMonro closed 1 year ago

drMonro commented 1 year ago

How to make custom mutation? I just want to call mutation with signing only 'offerId' and setting 'cartId' in Atoms layer? Now i got TS error

How to fix? Or i missing something? Thx

dai-shi commented 1 year ago

Can you reproduce it in https://www.typescriptlang.org/play ? You can import libraries there.

drMonro commented 1 year ago

Line 134

dai-shi commented 1 year ago

I think you misunderstand the usage. https://github.com/jotaijs/jotai-trpc/blob/9751009852034759df5cd22aa915f2be039964dc/src/createTRPCJotai.ts#L142-L148 The first argument is optional getClient. So, you don't need to specify anything.

👉 https://tsplay.dev/Wzx4LN

image

and setting 'cartId' in Atoms layer?

Does that mean you want to set the atom value based on the mutation output?? You would be able to create a new atom then.

drMonro commented 1 year ago

I just tried to make construction like in 'atomWithQuery', where I set parameters which is required for GET response right in Atom layer (not inside component)

How can I do the same but with 'atomWithMutation'?

Now i had to set 'cartId' when i call mutation in component, though i got 'cartAtom' data in Jotaii layer

dai-shi commented 1 year ago

Ah, I see. It wasn't a TS issue, but it's more like Jotai usage question.

I think you can create another action atom.

export const cartAtom = atom({ id: '', items: [] })

const deleteFromCartAtom = api.cart.del.atomWithMutation()

export const deleteCurrentFromCurtAtom = atom(null, async (get, set) => {
  const cart = get(cartAtom)
  const output = await set(deleteFromCartAtom, {
    cartId: cart.id,
  })
  console.log(output)
})
drMonro commented 1 year ago

Oh! Ok! Thank you I'll try this approach

drMonro commented 1 year ago

So, with that approach i can't set part of arguments on Component layer and another part on Jotai layer - and i need to create 'offerIdAtom', right?

dai-shi commented 1 year ago

I'm not 100% following, but if it's about how to use jotai apart from jotai-trpc, it sounds about right.