Skip to content

Commit 212cda6

Browse files
committed
52
1 parent 220de47 commit 212cda6

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

db.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"id": 18
6262
},
6363
{
64-
"subject": "new todo2",
64+
"subject": "new todo222",
6565
"completed": true,
6666
"id": 21
6767
}

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dependencies": {
1111
"axios": "^0.21.1",
1212
"core-js": "^3.6.5",
13+
"lodash": "^4.17.21",
1314
"vue": "^3.0.0",
1415
"vue-router": "^4.0.4"
1516
},

src/pages/todos/_id.vue

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
</div>
3636
</div>
3737

38-
<button type="submit" class="btn btn-primary">
38+
<button
39+
type="submit"
40+
class="btn btn-primary"
41+
:disabled="!todoUpdated"
42+
>
3943
Save
4044
</button>
4145
<button
@@ -50,23 +54,33 @@
5054
<script>
5155
import { useRoute, useRouter } from 'vue-router';
5256
import axios from 'axios';
53-
import { ref } from '@vue/reactivity';
57+
import { ref, computed } from 'vue';
58+
import _ from 'lodash';
5459
5560
export default {
5661
setup() {
5762
const route = useRoute();
5863
const router = useRouter();
5964
const todo = ref(null);
65+
const originalTodo = ref(null);
6066
const loading = ref(true);
6167
const todoId = route.params.id
6268
6369
const getTodo = async () => {
64-
const res = await axios.get(`http://localhost:3000/todos/${todoId}`);
70+
const res = await axios.get(`
71+
http://localhost:3000/todos/${todoId}
72+
`);
73+
74+
todo.value = { ...res.data };
75+
originalTodo.value = { ...res.data };
6576
66-
todo.value = res.data;
6777
loading.value = false;
6878
};
6979
80+
const todoUpdated = computed(() => {
81+
return !_.isEqual(todo.value, originalTodo.value)
82+
});
83+
7084
const toggleTodoStatus = () => {
7185
todo.value.completed = !todo.value.completed;
7286
};
@@ -80,12 +94,14 @@ export default {
8094
getTodo();
8195
8296
const onSave = async () => {
83-
const res = await axios.put(`http://localhost:3000/todos/${todoId}`, {
97+
const res = await axios.put(`
98+
http://localhost:3000/todos/${todoId}
99+
`, {
84100
subject: todo.value.subject,
85101
completed: todo.value.completed
86102
});
87103
88-
console.log(res);
104+
originalTodo.value = {...res.data};
89105
};
90106
91107
return {
@@ -94,6 +110,7 @@ export default {
94110
toggleTodoStatus,
95111
moveToTodoListPage,
96112
onSave,
113+
todoUpdated,
97114
};
98115
}
99116
}

0 commit comments

Comments
 (0)