useUpdateCharacter.js 890 B

1234567891011121314151617181920212223242526272829303132
  1. import { useOptimisticUpdate } from "pinia-colada-plugin-normalizer"
  2. export const useUpdateCharacter = defineMutation(() => {
  3. const { transaction } = useOptimisticUpdate()
  4. const { mutate, ...mutation } = useMutation({
  5. onMutate: ({ id, updates }) => {
  6. const optionsCache = useOptionsCache()
  7. const tx = transaction()
  8. tx.set("character", id, updates)
  9. optionsCache.update()
  10. return { ...tx, rollbackOptions: optionsCache.rollback }
  11. },
  12. mutation: ({ id, updates }) => useEmit("character:update", id, updates),
  13. onError: (error, _vars, { rollback, rollbackOptions }) => {
  14. rollback?.()
  15. rollbackOptions()
  16. console.error("[useUpdateCharacter] [onError]", error)
  17. },
  18. onSuccess: (_data, _vars, { commit }) => commit?.()
  19. })
  20. return {
  21. ...mutation,
  22. updateCharacter: (id, updates) => mutate({ id, updates })
  23. }
  24. })