Skip to content

Commit 274b1c6

Browse files
committed
59
1 parent 42b775d commit 274b1c6

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

db.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@
6464
"subject": "new todo222",
6565
"completed": true,
6666
"id": 21
67+
},
68+
{
69+
"subject": "buy milk",
70+
"completed": false,
71+
"body": "go to supermarket and buy milk",
72+
"id": 22
73+
},
74+
{
75+
"subject": "buy milk",
76+
"completed": false,
77+
"body": "buy milk at supermarket",
78+
"id": 23
79+
},
80+
{
81+
"subject": "asdfasdf",
82+
"completed": false,
83+
"body": "asdfasdf",
84+
"id": 24
85+
},
86+
{
87+
"subject": "asdf",
88+
"completed": false,
89+
"body": "asdf",
90+
"id": 25
91+
},
92+
{
93+
"subject": "dgdsfg",
94+
"completed": false,
95+
"body": "dfghdfgh",
96+
"id": 26
6797
}
6898
]
6999
}

src/components/TodoForm.vue

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
class="btn btn-primary"
4646
:disabled="!todoUpdated"
4747
>
48-
Save
48+
{{ editing ? 'Update' : 'Create'}}
4949
</button>
5050
<button
5151
class="btn btn-outline-dark ml-2"
@@ -136,15 +136,27 @@ export default {
136136
137137
const onSave = async () => {
138138
try {
139-
const res = await axios.put(`
140-
http://localhost:3000/todos/${todoId}
141-
`, {
139+
let res;
140+
const data = {
142141
subject: todo.value.subject,
143-
completed: todo.value.completed
144-
});
145-
146-
originalTodo.value = {...res.data};
147-
triggerToast('Successfully saved!');
142+
completed: todo.value.completed,
143+
body: todo.value.body,
144+
};
145+
if (props.editing) {
146+
res = await axios.put(`
147+
http://localhost:3000/todos/${todoId}
148+
`, data);
149+
originalTodo.value = {...res.data};
150+
} else {
151+
res = await axios.post(`
152+
http://localhost:3000/todos
153+
`, data);
154+
todo.value.subject = '';
155+
todo.value.body = '';
156+
}
157+
158+
const message = 'Successfully ' + (props.editing ? 'Updated!' : 'Created!');
159+
triggerToast(message);
148160
} catch (error) {
149161
console.log(error);
150162
triggerToast('Something went wrong', 'danger')

0 commit comments

Comments
 (0)