Skip to content

Commit

Permalink
Add task refetch after creating a new task
Browse files Browse the repository at this point in the history
The invalidateQueries causes the query cache to be invalid and a new
request is made to the api to make sure that we have the most updated
data.
  • Loading branch information
negreirosleo committed Nov 14, 2023
1 parent b1ca39c commit bcb7879
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions frontend/src/app/tasks/hooks/useTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { createTask } from '@/infra/task/createTask'
import { getTasks } from '@/infra/task/getTasks'
import { useClientFetch } from '@/infra/lib/useClientFetch'
import { format } from 'date-fns'
import { Task } from '@/domain/Task'

export const useGetTasks = (userId: number) => {
const apiClient = useClientFetch()
Expand All @@ -26,13 +25,8 @@ export const useAddTask = (userId: number) => {
const { showError, showSuccess } = useAlert()

const { mutate } = useMutation((task: TaskIntent) => createTask(task, apiClient), {
onSuccess: (data) => {
queryClient.setQueryData<Array<Task>>(['tasks', userId], (oldData) => {
if (oldData) {
return [...oldData, data]
}
return [data]
})
onSuccess: () => {
queryClient.invalidateQueries(['tasks', userId])
showSuccess('Task added succesfully')
},
onError: () => {
Expand Down

0 comments on commit bcb7879

Please sign in to comment.