diff --git a/src/components/DestinationInfoCard.vue b/src/components/DestinationInfoCard.vue index 6346791..ddd0003 100644 --- a/src/components/DestinationInfoCard.vue +++ b/src/components/DestinationInfoCard.vue @@ -259,35 +259,44 @@ export default { this.showRoute = false; }, addToFavorites() { - console.log('on click'); - fetch("https://little-lines-backend.techtransthai.org/api/favorites/create",{ - method: "POST", - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - user_id: '123456789', - place_name: this.nearestStructureData.display_name, - location: { - type: "Point", - coordinates: [parseFloat(this.nearestStructureData.lon), parseFloat(this.nearestStructureData.lat)] - }, - wheelchair_access: 'Accessible', - highway_type: 'Highway' - }) + const currentUser = JSON.parse(sessionStorage.getItem('current_user')); + + if (currentUser) { + console.log('Logged in. Proceed to add to favorites.'); + + fetch("https://little-lines-backend.techtransthai.org/api/favorites/create", { + method: "POST", + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + user_id: currentUser.id, // ใช้ userId ของผู้ใช้ที่ล็อกอินอยู่ + place_name: this.nearestStructureData.display_name, + location: { + type: "Point", + coordinates: [parseFloat(this.nearestStructureData.lon), parseFloat(this.nearestStructureData.lat)] + }, + wheelchair_access: 'Accessible', + highway_type: 'Highway' }) + }) .then((res) => { - if (res.ok) { - console.log('Add to favorites success'); - return res.json(); - } else { - throw Error(`Add to favorited faild (${res.status})`); - } - }) - .catch((err) => { - console.log(err); - }); - } + if (res.ok) { + console.log('Add to favorites success'); + return res.json(); + } else { + throw Error(`Add to favorites failed (${res.status})`); + } + }) + .catch((err) => { + console.log(err); + }); + } else { + console.log('User not logged in. Unable to add to favorites.'); + this.$router.push({ name: 'login' }); + } +} + }, }; diff --git a/src/views/Login.vue b/src/views/Login.vue index d847f21..e7eab55 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -116,10 +116,10 @@ export default { } }) .then((data) => { - // localStorage.setItem('isLoggedIn', true); - // console.log(isLoggedIn); + console.log(data.success); sessionStorage.setItem('current_user', JSON.stringify({id:data.user._id,username:data.user.username,email:data.user.email})); + this.$router.push({ name: 'home' }); }) .catch((err) => { diff --git a/src/views/Setting.vue b/src/views/Setting.vue index 2ccadff..93fb7a4 100644 --- a/src/views/Setting.vue +++ b/src/views/Setting.vue @@ -1,7 +1,7 @@