Skip to content

Commit

Permalink
feat: useUpdateEntry hook
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 25, 2024
1 parent e6b12d3 commit ef5385b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
52 changes: 11 additions & 41 deletions src/renderer/src/hooks/useEntryActions.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useToast } from "@renderer/components/ui/use-toast"
import { useUpdateEntry } from "@renderer/hooks/useUpdateEntry"
import { client } from "@renderer/lib/client"
import type { EntriesResponse, ListResponse } from "@renderer/lib/types"
import type { EntriesResponse } from "@renderer/lib/types"
import { apiFetch } from "@renderer/queries/api-fetch"
import type { InfiniteData, QueryKey } from "@tanstack/react-query"
import {
useMutation,
useQuery,
useQueryClient,
} from "@tanstack/react-query"
import type { FetchError } from "ofetch"
import { ofetch } from "ofetch"
Expand All @@ -31,42 +30,9 @@ export const useEntryActions = ({
},
})

const queryClient = useQueryClient()

const updateCollection = (
target: boolean,
) => {
const key = ["entry", entry?.id]
const data = queryClient.getQueryData(key)
if (data) {
queryClient.setQueryData(
key,
Object.assign({}, data, {
collected: target,
}),
)
}

const entriesData = queryClient.getQueriesData({
queryKey: ["entries"],
})
entriesData.forEach(
([key, data]: [
QueryKey,
unknown,
]) => {
const list = (data as InfiniteData<ListResponse<EntriesResponse>>)?.pages?.[0]?.data
if (list) {
for (const item of list) {
if (item.id === entry?.id) {
item.collected = target
queryClient.setQueryData(key, data)
}
}
}
},
)
}
const updateEntry = useUpdateEntry({
entryId: entry?.id,
})

const collect = useMutation({
mutationFn: async () =>
Expand All @@ -77,7 +43,9 @@ export const useEntryActions = ({
},
}),
onSuccess: () => {
updateCollection(true)
updateEntry({
collected: true,
})

toast({
duration: 1000,
Expand All @@ -94,7 +62,9 @@ export const useEntryActions = ({
},
}),
onSuccess: () => {
updateCollection(false)
updateEntry({
collected: false,
})

toast({
duration: 1000,
Expand Down
50 changes: 50 additions & 0 deletions src/renderer/src/hooks/useUpdateEntry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { EntriesResponse, ListResponse } from "@renderer/lib/types"
import type { InfiniteData, QueryKey } from "@tanstack/react-query"
import {
useQueryClient,
} from "@tanstack/react-query"

export const useUpdateEntry = ({
entryId,
}: {
entryId?: string
}) => {
const queryClient = useQueryClient()

const updateEntry = (
changed: Partial<EntriesResponse[number]>,
) => {
const key = ["entry", entryId]
const data = queryClient.getQueryData(key)
if (data) {
queryClient.setQueryData(
key,
Object.assign({}, data, changed),
)
}

const entriesData = queryClient.getQueriesData({
queryKey: ["entries"],
})
entriesData.forEach(
([key, data]: [
QueryKey,
unknown,
]) => {
const list = (data as InfiniteData<ListResponse<EntriesResponse>>)?.pages?.[0]?.data
if (list) {
for (const item of list) {
if (item.id === entryId) {
for (const [key, value] of Object.entries(changed)) {
item[key] = value
}
queryClient.setQueryData(key, data)
}
}
}
},
)
}

return updateEntry
}

0 comments on commit ef5385b

Please sign in to comment.