diff --git a/src/orpc/client.ts b/src/orpc/client.ts index 8de2d89..b5d0e8f 100644 --- a/src/orpc/client.ts +++ b/src/orpc/client.ts @@ -24,4 +24,30 @@ const getORPCClient = createIsomorphicFn() const client: RouterClient = getORPCClient() -export const orpc = createTanstackQueryUtils(client) +export const orpc = createTanstackQueryUtils(client, { + experimental_defaults: { + todo: { + create: { + mutationOptions: { + onSuccess: (_, __, ___, ctx) => { + ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) + }, + }, + }, + update: { + mutationOptions: { + onSuccess: (_, __, ___, ctx) => { + ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) + }, + }, + }, + remove: { + mutationOptions: { + onSuccess: (_, __, ___, ctx) => { + ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) + }, + }, + }, + }, + }, +}) diff --git a/src/routes/todos.tsx b/src/routes/todos.tsx index 9b88e46..3112308 100644 --- a/src/routes/todos.tsx +++ b/src/routes/todos.tsx @@ -15,6 +15,7 @@ function Todos() { const [newTodoTitle, setNewTodoTitle] = useState('') const listQuery = useSuspenseQuery(orpc.todo.list.queryOptions()) + const createMutation = useMutation(orpc.todo.create.mutationOptions()) const updateMutation = useMutation(orpc.todo.update.mutationOptions()) const deleteMutation = useMutation(orpc.todo.remove.mutationOptions()) @@ -22,15 +23,8 @@ function Todos() { const handleCreateTodo = (e: FormEvent) => { e.preventDefault() if (newTodoTitle.trim()) { - createMutation.mutate( - { title: newTodoTitle.trim() }, - { - onSuccess: () => { - setNewTodoTitle('') - listQuery.refetch() - }, - }, - ) + createMutation.mutate({ title: newTodoTitle.trim() }) + setNewTodoTitle('') } } @@ -39,28 +33,14 @@ function Todos() { } const handleToggleTodo = (id: string, currentCompleted: boolean) => { - updateMutation.mutate( - { - id, - data: { completed: !currentCompleted }, - }, - { - onSuccess: () => { - listQuery.refetch() - }, - }, - ) + updateMutation.mutate({ + id, + data: { completed: !currentCompleted }, + }) } const handleDeleteTodo = (id: string) => { - deleteMutation.mutate( - { id }, - { - onSuccess: () => { - listQuery.refetch() - }, - }, - ) + deleteMutation.mutate({ id }) } const todos = listQuery.data