Skip to content

Commit

Permalink
Update mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
nawrotw committed Jul 22, 2024
1 parent 9b1e4f2 commit a6d5557
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function App() {

const handleDelete = useCallback((todoId: number) => {
deleteTodo(todoId);
resetEdit();
}, [deleteTodo]);

const handleSave = (id: number, text: string) => {
Expand Down
21 changes: 12 additions & 9 deletions src/api/todos/useTodosMutation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { updateTodoCheck, clearCompleted as clearCompletedFn, addTodo, updateTodoText, deleteTodo } from "./fetchTodos.ts";
import { Todo } from "./Todo.ts";
import { getTodoFromCache, setTodoCache } from "../../utils/reactQueryUtils.ts";
import { getTodoFromCache, updateCacheTodo } from "../../utils/reactQueryUtils.ts";

export const useTodosMutation = () => {
const queryClient = useQueryClient();

const getTodo = (id: number) => getTodoFromCache(queryClient, id);
const setTodo = (todo: Todo) => setTodoCache(queryClient, todo);
const updateTodo = (todo: Todo) => updateCacheTodo(queryClient, todo);

const addTodoMutation = useMutation({
mutationFn: addTodo,
Expand All @@ -23,7 +23,7 @@ export const useTodosMutation = () => {
// If the mutation fails,
// use the context returned from onMutate to roll back
onError: (_err, _newTodo, context) => {
context && queryClient.setQueryData(['todos'], context.previousTodos)
context && queryClient.setQueryData(['todos'], context.previousTodos);
},
// Always re-fetch after error or success:
onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
Expand All @@ -42,7 +42,7 @@ export const useTodosMutation = () => {
mutationFn: updateTodoText,
onMutate: async ({ id, text }) => {
await queryClient.cancelQueries({ queryKey: ['todos', id] });
setTodo({ ...getTodo(id), text: text });
updateTodo({ ...getTodo(id), text: text });
},
onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
});
Expand All @@ -51,9 +51,15 @@ export const useTodosMutation = () => {
mutationFn: updateTodoCheck,
onMutate: async ({ id, checked }) => {
await queryClient.cancelQueries({ queryKey: ['todos', id] });
setTodo({ ...getTodo(id), checked: checked });
const prevTodo = getTodo(id);
updateTodo({ ...prevTodo, checked: checked });
return { prevTodo }
},
onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
onError: (_err, _newTodo, context) => {
context && updateTodo(context.prevTodo);
},
// do not re-fetch to preserve items order
// onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
});

const clearCompletedMutate = useMutation({
Expand All @@ -68,9 +74,6 @@ export const useTodosMutation = () => {
onSettled: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
});

addTodoMutation.error
addTodoMutation.reset

return {
addTodo: addTodoMutation.mutate,
deleteTodo: deleteTodoMutation.mutate,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/reactQueryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient } from "@tanstack/react-query";
import { Todo } from "../api/todos/Todo.ts";

export const setTodoCache = (queryClient: QueryClient, todo: Todo) => {
export const updateCacheTodo = (queryClient: QueryClient, todo: Todo) => {
queryClient.setQueryData<Todo[]>(['todos'], (old) => {
if (!old) throw new Error(`Todos list is undefined. Should not happen`);
const index = old?.findIndex(({id}) => id === todo.id);
Expand Down

0 comments on commit a6d5557

Please sign in to comment.