| 1234567891011121314151617181920212223242526272829303132 |
- import { useOptimisticUpdate } from "pinia-colada-plugin-normalizer"
- export const useUpdateCharacter = defineMutation(() => {
- const { transaction } = useOptimisticUpdate()
- const { mutate, ...mutation } = useMutation({
- onMutate: ({ id, updates }) => {
- const optionsCache = useOptionsCache()
- const tx = transaction()
- tx.set("character", id, updates)
- optionsCache.update()
- return { ...tx, rollbackOptions: optionsCache.rollback }
- },
- mutation: ({ id, updates }) => useEmit("character:update", id, updates),
- onError: (error, _vars, { rollback, rollbackOptions }) => {
- rollback?.()
- rollbackOptions()
- console.error("[useUpdateCharacter] [onError]", error)
- },
- onSuccess: (_data, _vars, { commit }) => commit?.()
- })
- return {
- ...mutation,
- updateCharacter: (id, updates) => mutate({ id, updates })
- }
- })
|