Skip to content

Commit 5d30053

Browse files
committed
fix: todo-list api
1 parent 7a90763 commit 5d30053

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

src/containers/TodoList/TodoList.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const TodoList = () => {
2121
const { open } = usePopup();
2222
const { t } = useTranslation();
2323

24-
// const { mutate } = useDeleteCategory();
2524
const { mutate } = useDeleteTodoList();
2625

2726
if (isLoading) return 'loading ...';
@@ -30,7 +29,7 @@ const TodoList = () => {
3029

3130
const deletePopupHandler = () => {
3231
open({
33-
message: t('delete_category', { title: data.title }),
32+
message: t('delete_todo_list', { title: data.title }),
3433
title: t('delete'),
3534
buttons: [
3635
{

src/containers/TodoListForm/TodoListForm.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const TodoListForm = ({ type }: IProps) => {
3030
resolver: yupResolver(todoListSchema),
3131
values: {
3232
title: todoList?.title ?? '',
33+
description: todoList?.description ?? '',
3334
categoryId: categoryId as string,
3435
},
3536
});
@@ -45,8 +46,8 @@ const TodoListForm = ({ type }: IProps) => {
4546
} = methods;
4647

4748
const onSubmit = handleSubmit(data => {
48-
if (type === 'create') {
49-
create(data);
49+
if (type === 'create' && data.categoryId) {
50+
create({ ...data, categoryId: data.categoryId });
5051
} else {
5152
if (!todoList?.id) return;
5253
edit({ ...data, id: todoList.id });

src/hooks/api/todo-list.ts

+23-32
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,46 @@ export const useEditTodoList = () => {
5151
return useMutation({
5252
mutationFn: api.editTodoList,
5353
onMutate: async variables => {
54-
const snapshot = queryClient.getQueryData<ICategory[]>(['categories']);
54+
const snapshot = queryClient.getQueryData<ITodoList>([
55+
'todo-list',
56+
variables.id,
57+
]);
5558

5659
// Optimistically update to the new value
57-
queryClient.setQueryData<ICategory[]>(
58-
['categories'],
59-
produce(draft => {
60-
const category = draft?.find(
61-
category => category.id === variables.categoryId
62-
);
63-
let todoList = category?.todoLists.find(
64-
todo => todo.id === variables.id
65-
);
66-
67-
if (todoList) {
68-
todoList = { ...todoList, ...variables };
69-
}
70-
})
71-
);
60+
queryClient.setQueryData<ITodoList>(['todo-list', variables.id], prev => {
61+
if (!prev) return;
62+
return {
63+
...prev,
64+
...variables,
65+
};
66+
});
7267

7368
router.replace('/todo-list/' + variables.id);
7469

7570
// Return a context object with the snapshot value
7671
return { snapshot };
7772
},
73+
onSuccess: () => {
74+
queryClient.refetchQueries({
75+
exact: true,
76+
queryKey: ['categories'],
77+
});
78+
},
7879
});
7980
};
8081

81-
//Should converted to Todo List with immer
8282
export const useDeleteTodoList = () => {
8383
const queryClient = useQueryClient();
8484
const router = useRouter();
8585

8686
return useMutation({
8787
mutationFn: api.deleteTodoList,
88-
onMutate: async ({ id }) => {
89-
// Snapshot the previous value
90-
const snapshot = queryClient.getQueryData<ICategory[]>(['categories']);
91-
92-
// Optimistically update to the new value
93-
queryClient.setQueryData<ICategory[]>(['categories'], old => old);
94-
95-
// Redirect to landing page
96-
router.replace('/');
97-
98-
// Return a context object with the snapshot value
99-
return { snapshot };
100-
},
101-
onError: (_error, _variables, context) => {
102-
queryClient.setQueryData(['categories'], context?.snapshot);
88+
onSuccess: () => {
89+
router.push('/');
90+
queryClient.refetchQueries({
91+
exact: true,
92+
queryKey: ['categories'],
93+
});
10394
},
10495
});
10596
};

src/schema/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const categorySchema = yup.object().shape({
77

88
export const todoListSchema = yup.object().shape({
99
title: yup.string().required(),
10-
categoryId: yup.string().required(),
10+
categoryId: yup.string(),
1111
description: yup.string(),
1212
left: yup.number(),
1313
completed: yup.number(),

src/types/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface ITodoList {
3636
}
3737
export type ITodoListGet = Pick<ITodoList, 'id'>;
3838
export type ITodoListPost = Pick<ITodoList, 'title' | 'categoryId'>;
39-
export type ITodoListEdit = Pick<ITodoList, 'title' | 'categoryId' | 'id'>;
39+
export type ITodoListEdit = Pick<ITodoList, 'title' | 'id'>;
4040
export type ITodoListDelete = Pick<ITodoList, 'id'>;
4141

4242
export interface ICategory {

0 commit comments

Comments
 (0)