Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 535a8af

Browse files
committed
Do not refetch all the updates when an update is deleted
1 parent c3dd2da commit 535a8af

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

src/components/HomePageFeed.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,38 @@ export function HomePageFeedUpdate({
7777
})
7878
},
7979
{
80-
onSuccess: () => {
80+
onSuccess: (data, variables, context) => {
8181
toast.success('Deleted your update!!', {
8282
id: toastId.current,
8383
icon: <Trash className="text-danger-400" />,
8484
})
85-
queryClient.refetchQueries('/api/fauna/all-updates')
86-
queryClient.refetchQueries(['/api/fauna/recent-updates', goal.id])
85+
86+
if (queryClient.getQueryState('/api/fauna/all-updates')) {
87+
queryClient.setQueryData<{ updates: HomePageFeedUpdateType[] }>(
88+
'/api/fauna/all-updates',
89+
(oldData) => ({
90+
updates: oldData.updates.filter(
91+
(_update) => _update.id !== update.id
92+
),
93+
})
94+
)
95+
}
96+
97+
if (queryClient.getQueryState(['/api/fauna/recent-updates', goal.id])) {
98+
queryClient.setQueryData<{ response: GoalResponse }>(
99+
['/api/fauna/recent-updates', goal.id],
100+
(oldData) => ({
101+
response: {
102+
...oldData.response,
103+
updates: {
104+
data: oldData.response.updates.data.filter(
105+
(_update) => _update.id !== update.id
106+
),
107+
},
108+
},
109+
})
110+
)
111+
}
87112
},
88113
onError: () => {
89114
toast.error('Something went wrong!!!', {

src/components/goals/NewUpdate.tsx

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,21 @@ export default function NewUpdate({
7474
)
7575

7676
if (updateFromHomePage) {
77-
queryClient.setQueryData<{ updates: HomePageFeedUpdateType[] }>(
78-
'/api/fauna/all-updates',
79-
(oldData) => {
80-
if (!oldData) {
81-
return oldData
82-
}
83-
return {
77+
if (queryClient.getQueryState('/api/fauna/all-updates')) {
78+
queryClient.setQueryData<{ updates: HomePageFeedUpdateType[] }>(
79+
'/api/fauna/all-updates',
80+
(oldData) => ({
8481
updates: [data.response, ...oldData.updates],
85-
}
86-
}
87-
)
82+
})
83+
)
84+
}
8885

89-
queryClient.setQueryData<{ response: GoalResponse }>(
90-
['/api/fauna/recent-updates', goal.id],
91-
(oldData) => {
92-
// If the recent-updates panel is not opened yet
93-
// then this oldData will be undefined
94-
// since the corresponsing query is not execcuted even once
95-
if (!oldData) {
96-
return oldData
97-
}
98-
return {
86+
if (
87+
queryClient.getQueryState(['/api/fauna/recent-updates', goal.id])
88+
) {
89+
queryClient.setQueryData<{ response: GoalResponse }>(
90+
['/api/fauna/recent-updates', goal.id],
91+
(oldData) => ({
9992
response: {
10093
...oldData.response,
10194
updates: {
@@ -110,9 +103,9 @@ export default function NewUpdate({
110103
],
111104
},
112105
},
113-
}
114-
}
115-
)
106+
})
107+
)
108+
}
116109
}
117110

118111
reset()

0 commit comments

Comments
 (0)