Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
jhlagado committed Feb 8, 2024
1 parent 570740f commit e6e1390
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 677 deletions.
430 changes: 12 additions & 418 deletions data/db.json

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions src/components/xtodo/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ import { FaTrash, FaUpload } from "react-icons/fa";
import { Todo, SERVER_URL as cacheKey, getTodos } from "./todosApi";
import {
addTodoMutation,
addTodoMutationOptions,
addTodoUtility,
deleteTodoMutation,
deleteTodoMutationOptions,
deleteTodoUtility,
updateTodoMutation,
updateTodoMutationOptions,
updateTodoUtility,
} from "./mutations";

const TodoList = () => {
const [newTodo, setNewTodo] = useState("");

const { isLoading, error, data: todos, mutate } = useSWR(cacheKey, getTodos);

const handleAdd = async (newTodo: Todo) => {
const handleAdd = async (addedTodo: Todo) => {
if (!todos) return;
try {
await mutate(
addTodoMutation(newTodo, todos),
addTodoMutationOptions(newTodo, todos)
addTodoMutation(addedTodo, todos),
{
optimisticData: addTodoUtility(addedTodo, todos),
revalidate: false,
}
);
toast.success("Success! Added a new item", {
duration: 2000,
Expand All @@ -38,7 +41,10 @@ const TodoList = () => {
try {
await mutate(
updateTodoMutation(updatedTodo, todos),
updateTodoMutationOptions(updatedTodo, todos)
{
optimisticData: updateTodoUtility(updatedTodo, todos),
revalidate: false,
}
);
toast.success("Success! Updated item", {
duration: 2000,
Expand All @@ -54,7 +60,10 @@ const TodoList = () => {
try {
await mutate(
deleteTodoMutation(id, todos),
deleteTodoMutationOptions(id, todos)
{
optimisticData: deleteTodoUtility(id, todos),
revalidate: false,
}
);
toast.success("Success! Deleted item", {
duration: 2000,
Expand Down
33 changes: 0 additions & 33 deletions src/components/xtodo/addTodo.test.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/components/xtodo/deleteTodo.test.ts

This file was deleted.

56 changes: 56 additions & 0 deletions src/components/xtodo/mutations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { HttpResponse, http } from "msw";
import { server } from "../../test/mocks/server";
import { SERVER_URL, Todo } from "./todosApi";
import { mockTodo, mockTodoList } from "../../test/mocks/constants";
import {
addTodoMutation,
deleteTodoMutation,
updateTodoMutation,
} from "./mutations";

describe("mutations", () => {
it("should return the added todo item", async () => {
server.use(
http.post(`${SERVER_URL}`, () => {
return HttpResponse.json(mockTodo, { status: 200 });
})
);
const todos = mockTodoList;
const todos1 = await addTodoMutation(mockTodo, todos);
expect(todos1).toEqual([mockTodo, ...todos]);
});

it("should return the updated todo item", async () => {
server.use(
http.put(`${SERVER_URL}/:id`, async ({ request }) => {
const { id, userId, title, completed } = (await request.json()) as Todo;
return HttpResponse.json(
{
userId,
title,
completed,
id,
},
{ status: 200 }
);
})
);
const todos = [mockTodo, ...mockTodoList];
const todos1 = await updateTodoMutation(
{ ...mockTodo, title: "123" },
todos
);
expect(todos1).toEqual([{ ...mockTodo, title: "123" }, ...mockTodoList]);
});

it("should return the deleted todo id", async () => {
server.use(
http.delete(`${SERVER_URL}/:id`, () => {
return HttpResponse.json(mockTodo, { status: 200 });
})
);
const todos = [mockTodo, ...mockTodoList];
const todos1 = await deleteTodoMutation(mockTodo.id, todos);
expect(todos1).toEqual(mockTodoList);
});
});
46 changes: 17 additions & 29 deletions src/components/xtodo/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
import { Todo, addTodo, deleteTodo, updateTodo } from "./todosApi";

export async function addTodoMutation(newTodo: Todo, todos: Todo[]) {
const added = await addTodo(newTodo);
export const addTodoUtility = (added: Todo, todos: Todo[]) => {
return [added, ...todos];
}
};

export function addTodoMutationOptions(newTodo: Todo, todos: Todo[]) {
return {
optimisticData: [newTodo, ...todos],
revalidate: false,
};
export async function addTodoMutation(addedTodo: Todo, todos: Todo[]) {
const added = await addTodo(addedTodo);
return addTodoUtility(added, todos);
}

export async function updateTodoMutation(updatedTodo: Todo, todos: Todo[]) {
const updated = await updateTodo(updatedTodo);
export const updateTodoUtility = (updated: Todo, todos: Todo[]) => {
return todos.map((todo) => {
return updatedTodo.id === todo.id ? updated : todo;
return updated.id === todo.id ? updated : todo;
});
}
};

export function updateTodoMutationOptions(updatedTodo: Todo, todos: Todo[]) {
return {
optimisticData: todos.map((todo) => {
return updatedTodo.id === todo.id ? updatedTodo : todo;
}),
revalidate: false,
};
export async function updateTodoMutation(updatedTodo: Todo, todos: Todo[]) {
const response = await updateTodo(updatedTodo);
return updateTodoUtility(response, todos);
}

export async function deleteTodoMutation(id: number, todos: Todo[]) {
await deleteTodo(id);
export const deleteTodoUtility = (id: number, todos: Todo[]) => {
return todos.filter((todo) => {
return todo.id !== id;
});
}
};

export function deleteTodoMutationOptions(id: number, todos: Todo[]) {
return {
optimisticData: todos.filter((todo) => {
return todo.id !== id;
}),
revalidate: false,
};
export async function deleteTodoMutation(id: number, todos: Todo[]) {
await deleteTodo(id);
return deleteTodoUtility(id, todos);
}

File renamed without changes.
47 changes: 0 additions & 47 deletions src/components/xtodo/todosMutations.tsx

This file was deleted.

41 changes: 0 additions & 41 deletions src/components/xtodo/updateTodo.test.ts

This file was deleted.

17 changes: 16 additions & 1 deletion src/test/mocks/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
export const mockTodo = {
userId: 1,
title: "Wave hello! 👋",
title: "A",
completed: false,
id: 1,
};

export const mockTodoList = [
{
userId: 2,
title: "B",
completed: false,
id: 2,
},
{
userId: 3,
title: "C",
completed: false,
id: 3,
},
];
Loading

0 comments on commit e6e1390

Please sign in to comment.