Skip to content

Commit

Permalink
fix: reset auto save data when saved
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jan 10, 2024
1 parent 6daaf06 commit c5bae13
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { createContext, useContext, useEffect, useMemo, useState } from 'react'
import { useForceUpdate } from 'framer-motion'
import { produce } from 'immer'
Expand Down
17 changes: 12 additions & 5 deletions src/queries/definition/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { revalidateTag } from 'next/cache'
import type { NoteModel, NoteWrappedPayload } from '@mx-space/api-client'
import type { NoteDto } from '~/models/writing'

import { useResetAutoSaverData } from '~/components/modules/dashboard/writing/BaseWritingProvider'
import { cloneDeep } from '~/lib/_'
import { apiClient } from '~/lib/request'
import { routeBuilder, Routes } from '~/lib/route-builder'
Expand Down Expand Up @@ -73,8 +74,9 @@ export const noteAdmin = {
}),
}

export const useCreateNote = () =>
useMutation({
export const useCreateNote = () => {
const resetAutoSaver = useResetAutoSaverData()
return useMutation({
mutationFn: (data: NoteDto) => {
const readonlyKeys = [
'id',
Expand All @@ -95,11 +97,14 @@ export const useCreateNote = () =>
onSuccess: () => {
revalidateTag('note')
toast.success('创建成功')
resetAutoSaver('note')
},
})
}

export const useUpdateNote = () =>
useMutation({
export const useUpdateNote = () => {
const resetAutoSaver = useResetAutoSaverData()
return useMutation({
mutationFn: (data: NoteDto) => {
const { id } = data
const readonlyKeys = [
Expand All @@ -118,7 +123,9 @@ export const useUpdateNote = () =>
data: nextData,
})
},
onSuccess: () => {
onSuccess: ({ id }) => {
toast.success('更新成功')
resetAutoSaver('note', id)
},
})
}
17 changes: 12 additions & 5 deletions src/queries/definition/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMutation } from '@tanstack/react-query'
import type { PaginateResult, PostModel, TagModel } from '@mx-space/api-client'
import type { PostDto } from '~/models/writing'

import { useResetAutoSaverData } from '~/components/modules/dashboard/writing/BaseWritingProvider'
import { cloneDeep } from '~/lib/_'
import { apiClient } from '~/lib/request'
import { routeBuilder, Routes } from '~/lib/route-builder'
Expand Down Expand Up @@ -102,8 +103,9 @@ export const postAdmin = {
}),
}

export const useCreatePost = () =>
useMutation({
export const useCreatePost = () => {
const resetAutoSaver = useResetAutoSaverData()
return useMutation({
mutationFn: (data: PostDto) => {
const readonlyKeys = [
'id',
Expand All @@ -123,11 +125,14 @@ export const useCreatePost = () =>
},
onSuccess: () => {
toast.success('创建成功')
resetAutoSaver('post')
},
})
}

export const useUpdatePost = () =>
useMutation({
export const useUpdatePost = () => {
const resetAutoSaver = useResetAutoSaverData()
return useMutation({
mutationFn: (data: PostDto) => {
const { id } = data
const readonlyKeys = [
Expand All @@ -146,7 +151,9 @@ export const useUpdatePost = () =>
data: nextData,
})
},
onSuccess: () => {
onSuccess: ({ id }) => {
toast.success('更新成功')
resetAutoSaver('post', id)
},
})
}

1 comment on commit c5bae13

@vercel
Copy link

@vercel vercel bot commented on c5bae13 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

shiro-innei.vercel.app
shiro-git-main-innei.vercel.app
springtide.vercel.app
innei.in

Please sign in to comment.