useDeleteCharacter.js 845 B

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