Created
November 2, 2024 01:13
-
-
Save arackaf/cb4eee7bd7e5ef773c1d273d7d119e4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// very loose idea of what we'd need | |
// query options would need to be defined separately | |
export const getTasksQueryOptions = (search: string) => { | |
return { | |
queryKey: ["tasks", search], | |
queryFn: async () => { | |
const tasks = await isoMorphicFetch(`/api/tasks?search=${search}`); | |
return tasks; | |
} | |
}; | |
}; | |
export const updateTaskMiddleware_Server = (context: Context, id: number) => { | |
// we know task with id === id was updated. We'll be redirecting to the root tasks page, so fetch that data | |
const { queryKey, queryFn } = getTasksQueryOptions(""); | |
const tasks = queryFn(); | |
return { | |
tasksDataPacket: { | |
queryKey, | |
data: tasks | |
}, | |
invalidateAll: ["tasks"] | |
}; | |
}; | |
export const updateTaskMiddleware_Client = async (context: Context, id: number) => { | |
// The update task middleware has run, and it pushed down a promise with the new tasks data. | |
const { | |
tasksDataPacket, // from server middleware | |
invalidateAll, // from server middleware | |
queryClient // set up previously | |
} = context; | |
const tasks = await tasksDataPacket.data; | |
queryClient.invalidateQueries(invalidateAll); | |
queryClient.setQueryData(tasksDataPacket.queryKey, tasks); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment