Skip to content

Commit 8ec5ffa

Browse files
committed
53
1 parent 212cda6 commit 8ec5ffa

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

src/components/Toast.vue

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<div
3+
class="alert toast-box"
4+
:class="`alert-${type}`"
5+
role="alert"
6+
>
7+
{{ message }}
8+
</div>
9+
</template>
10+
11+
<script>
12+
export default {
13+
props: {
14+
message: {
15+
type: String,
16+
required: true
17+
},
18+
type: {
19+
type: String,
20+
default: 'success'
21+
}
22+
}
23+
}
24+
</script>
25+
26+
<style>
27+
.toast-box {
28+
position: fixed;
29+
top: 10px;
30+
right: 10px;
31+
}
32+
</style>

src/pages/todos/_id.vue

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,49 @@
4949
Cancel
5050
</button>
5151
</form>
52+
<Toast
53+
v-if="showToast"
54+
:message="toastMessage"
55+
:type="toastAlertType"
56+
/>
5257
</template>
5358

5459
<script>
5560
import { useRoute, useRouter } from 'vue-router';
5661
import axios from 'axios';
5762
import { ref, computed } from 'vue';
5863
import _ from 'lodash';
64+
import Toast from '@/components/Toast.vue';
5965
6066
export default {
67+
components: {
68+
Toast
69+
},
6170
setup() {
6271
const route = useRoute();
6372
const router = useRouter();
6473
const todo = ref(null);
6574
const originalTodo = ref(null);
6675
const loading = ref(true);
76+
const showToast = ref(false);
77+
const toastMessage = ref('');
78+
const toastAlertType = ref('');
6779
const todoId = route.params.id
6880
6981
const getTodo = async () => {
70-
const res = await axios.get(`
71-
http://localhost:3000/todos/${todoId}
72-
`);
82+
try {
83+
const res = await axios.get(`
84+
http://localhost:3000/todos/${todoId}
85+
`);
7386
74-
todo.value = { ...res.data };
75-
originalTodo.value = { ...res.data };
87+
todo.value = { ...res.data };
88+
originalTodo.value = { ...res.data };
7689
77-
loading.value = false;
90+
loading.value = false;
91+
} catch (error) {
92+
console.log(error);
93+
tiggerToast('Something went wrong', 'danger');
94+
}
7895
};
7996
8097
const todoUpdated = computed(() => {
@@ -93,15 +110,32 @@ export default {
93110
94111
getTodo();
95112
113+
const tiggerToast = (message, type = 'success') => {
114+
toastMessage.value = message;
115+
toastAlertType.value = type;
116+
showToast.value = true;
117+
setTimeout(() => {
118+
toastMessage.value = '';
119+
toastAlertType.value = '';
120+
showToast.value = false;
121+
}, 3000)
122+
}
123+
96124
const onSave = async () => {
97-
const res = await axios.put(`
98-
http://localhost:3000/todos/${todoId}
99-
`, {
100-
subject: todo.value.subject,
101-
completed: todo.value.completed
102-
});
125+
try {
126+
const res = await axios.put(`
127+
http://localhost:3000/todos/${todoId}
128+
`, {
129+
subject: todo.value.subject,
130+
completed: todo.value.completed
131+
});
103132
104-
originalTodo.value = {...res.data};
133+
originalTodo.value = {...res.data};
134+
tiggerToast('Successfully saved!');
135+
} catch (error) {
136+
console.log(error);
137+
tiggerToast('Something went wrong', 'danger')
138+
}
105139
};
106140
107141
return {
@@ -111,6 +145,9 @@ export default {
111145
moveToTodoListPage,
112146
onSave,
113147
todoUpdated,
148+
showToast,
149+
toastMessage,
150+
toastAlertType,
114151
};
115152
}
116153
}

0 commit comments

Comments
 (0)