Skip to content

Commit a1c6afa

Browse files
committed
fix: change todoIdBeingEdited to TodoList.vue
1 parent f818b8b commit a1c6afa

File tree

3 files changed

+51
-54
lines changed

3 files changed

+51
-54
lines changed

src/components/EditTodoForm.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<form @submit.prevent="handleSubmitEditedTodo(todoBeingEdited)">
2+
<form @submit.prevent="handleSubmitEditedTodoAndReset(todoBeingEdited)">
33
<input
44
ref="input"
55
type="text"
@@ -16,16 +16,16 @@ import { defineComponent, PropType, Ref, ref, onMounted } from "vue";
1616
import type Todo from "@/models/todo";
1717
1818
export type HandleCancelEdit = () => void;
19-
export type HandleSubmitEditedTodo = (editedTodo: Todo) => void;
19+
export type HandleSubmitEditedTodoAndReset = (editedTodo: Todo) => void;
2020
2121
export default defineComponent({
2222
props: {
2323
todo: {
2424
type: Object as PropType<Todo>,
2525
required: true,
2626
},
27-
handleSubmitEditedTodo: {
28-
type: Function as PropType<HandleSubmitEditedTodo>,
27+
handleSubmitEditedTodoAndReset: {
28+
type: Function as PropType<HandleSubmitEditedTodoAndReset>,
2929
required: true,
3030
},
3131
handleCancelEdit: {

src/components/TodoList.vue

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
<EditTodoForm
1212
v-else
1313
:todo="todo"
14-
:handle-submit-edited-todo="handleSubmitEditedTodo"
14+
:handle-submit-edited-todo-and-reset="handleSubmitEditedTodoAndReset"
1515
:handle-cancel-edit="handleCancelEdit"
1616
/>
1717
</div>
1818
</ul>
1919
</template>
2020

2121
<script lang="ts">
22-
import { defineComponent, PropType } from "vue";
22+
import { defineComponent, PropType, reactive } from "vue";
2323
import Todo from "@/models/todo";
2424
import TodoItem from "@/components/TodoItem.vue";
2525
import EditTodoForm from "@/components/EditTodoForm.vue";
@@ -30,48 +30,64 @@ import type {
3030
HandleClickRemove,
3131
} from "@/components/TodoItem.vue";
3232
import type {
33+
HandleSubmitEditedTodoAndReset,
3334
HandleCancelEdit,
34-
HandleSubmitEditedTodo,
3535
} from "@/components/EditTodoForm.vue";
3636
37+
export type HandleSubmitEditedTodo = (editedTodo: Todo) => void;
38+
39+
type State = {
40+
todoIdBeingEdited: string | null;
41+
};
42+
3743
export default defineComponent({
3844
components: { TodoItem, EditTodoForm },
3945
4046
props: {
4147
todos: {
4248
type: Array as PropType<ReadonlyArray<Todo>>,
43-
required: true
44-
},
45-
todoIdBeingEdited: {
46-
type: String as PropType<string | null>,
47-
default: undefined
49+
required: true,
4850
},
4951
handleToggleCompleted: {
5052
type: Function as PropType<HandleToggleCompleted>,
51-
required: true
52-
},
53-
handleClickEdit: {
54-
type: Function as PropType<HandleClickEdit>,
55-
required: true
53+
required: true,
5654
},
5755
handleClickRemove: {
5856
type: Function as PropType<HandleClickRemove>,
59-
required: true
57+
required: true,
6058
},
6159
handleSubmitEditedTodo: {
6260
type: Function as PropType<HandleSubmitEditedTodo>,
63-
required: true
61+
required: true,
6462
},
65-
handleCancelEdit: {
66-
type: Function as PropType<HandleCancelEdit>,
67-
required: true
68-
}
6963
},
7064
7165
setup(props) {
66+
const state = reactive<State>({
67+
todoIdBeingEdited: null,
68+
});
7269
const isTodoBeingEdited = (todo: Todo) =>
73-
todo.id === props.todoIdBeingEdited;
74-
return { isTodoBeingEdited };
75-
}
70+
todo.id === state.todoIdBeingEdited;
71+
72+
const handleSubmitEditedTodoAndReset: HandleSubmitEditedTodoAndReset = (
73+
editedTodo
74+
) => {
75+
props.handleSubmitEditedTodo(editedTodo);
76+
state.todoIdBeingEdited = null;
77+
};
78+
const handleClickEdit: HandleClickEdit = (todo) => {
79+
state.todoIdBeingEdited = todo.id;
80+
};
81+
const handleCancelEdit: HandleCancelEdit = () => {
82+
state.todoIdBeingEdited = null;
83+
};
84+
85+
return {
86+
isTodoBeingEdited,
87+
handleSubmitEditedTodoAndReset,
88+
handleClickEdit,
89+
handleCancelEdit,
90+
};
91+
},
7692
});
7793
</script>

src/pages/Index.vue

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,64 @@
77
/>
88
<TodoList
99
:todos="todos"
10-
:todo-id-being-edited="todoIdBeingEdited"
1110
:handle-toggle-completed="handleToggleCompleted"
12-
:handle-click-edit="handleClickEdit"
1311
:handle-click-remove="handleClickRemove"
1412
:handle-submit-edited-todo="handleSubmitEditedTodo"
15-
:handle-cancel-edit="handleCancelEdit"
1613
/>
1714
</div>
1815
</template>
1916

2017
<script lang="ts">
21-
import { defineComponent, reactive, Ref, ref } from "vue";
18+
import { defineComponent, reactive } from "vue";
2219
import CreateTodoForm from "@/components/CreateTodoForm.vue";
2320
import TodoList from "@/components/TodoList.vue";
2421
import useTodos from "@/composables/use-todos";
2522
2623
import type { HandleSubmitNewTodo } from "@/components/CreateTodoForm.vue";
24+
import type { HandleSubmitEditedTodo } from "@/components/TodoList.vue";
2725
import type {
2826
HandleToggleCompleted,
29-
HandleClickEdit,
3027
HandleClickRemove,
3128
} from "@/components/TodoItem.vue";
32-
import type {
33-
HandleCancelEdit,
34-
HandleSubmitEditedTodo,
35-
} from "@/components/EditTodoForm.vue";
3629
3730
type State = {
38-
newTodoTitle: string,
39-
}
31+
newTodoTitle: string;
32+
};
4033
4134
export default defineComponent({
4235
components: { CreateTodoForm, TodoList },
4336
4437
setup() {
45-
const { todos, createTodo, updateTodo, removeTodo } = useTodos();
4638
const state = reactive<State>({
47-
newTodoTitle: ''
48-
})
39+
newTodoTitle: "",
40+
});
41+
42+
const { todos, createTodo, updateTodo, removeTodo } = useTodos();
4943
5044
const handleSubmitNewTodo: HandleSubmitNewTodo = () => {
5145
createTodo({ title: state.newTodoTitle, completed: false });
5246
state.newTodoTitle = "";
5347
};
54-
55-
const todoIdBeingEdited: Ref<string | null> = ref(null);
5648
const handleToggleCompleted: HandleToggleCompleted = (todo) => {
5749
const { id, title, completed } = todo;
5850
updateTodo({ id, title, completed: !completed });
5951
};
60-
const handleClickEdit: HandleClickEdit = (todo) => {
61-
todoIdBeingEdited.value = todo.id;
62-
};
6352
const handleClickRemove: HandleClickRemove = (todo) => {
6453
const { id } = todo;
6554
removeTodo({ id });
6655
};
6756
const handleSubmitEditedTodo: HandleSubmitEditedTodo = (editedTodo) => {
6857
const { id, title, completed } = editedTodo;
6958
updateTodo({ id, title, completed });
70-
todoIdBeingEdited.value = null;
71-
};
72-
const handleCancelEdit: HandleCancelEdit = () => {
73-
todoIdBeingEdited.value = null;
7459
};
7560
7661
return {
7762
state,
78-
handleSubmitNewTodo,
79-
8063
todos,
81-
todoIdBeingEdited,
64+
handleSubmitNewTodo,
8265
handleToggleCompleted,
83-
handleClickEdit,
8466
handleClickRemove,
8567
handleSubmitEditedTodo,
86-
handleCancelEdit,
8768
};
8869
},
8970
});

0 commit comments

Comments
 (0)