Skip to content
Snippets Groups Projects
Commit db9b7e5d authored by Endrizzi, Marco (UG - Comp Sci & Elec Eng)'s avatar Endrizzi, Marco (UG - Comp Sci & Elec Eng)
Browse files

The messages are now read from the database

parent 1455ef71
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
q-toolbar.bg-primary.text-white.shadow-2 q-toolbar.bg-primary.text-white.shadow-2
q-toolbar-title Contacts q-toolbar-title Contacts
q-list(bordered) q-list(bordered)
q-item.q-my-sm(v-for='(user, key) in users', :key='key', to='/chat', clickable, v-ripple) q-item.q-my-sm(v-for='(user, key) in users', :key='key', :to='"/chat/" + key', clickable, v-ripple)
q-item-section(avatar) q-item-section(avatar)
q-avatar(color='primary', text-color='white') q-avatar(color='primary', text-color='white')
| {{ user.name.charAt(0) }} | {{ user.name.charAt(0) }}
......
...@@ -17,24 +17,12 @@ q-footer(elevated) ...@@ -17,24 +17,12 @@ q-footer(elevated)
</template> </template>
<script> <script>
import { mapState, mapActions } from 'vuex'
export default { export default {
data() { data() {
return { return {
newMessage: '', newMessage: ''
messages: [
{
text: 'Hello',
from: 'me'
},
{
text: 'Hi',
from: 'them'
},
{
text: 'How are you?',
from: 'me'
}
]
} }
}, },
...@@ -45,7 +33,21 @@ export default { ...@@ -45,7 +33,21 @@ export default {
from: 'me' from: 'me'
}) })
this.newMessage = '' this.newMessage = ''
} },
...mapActions('firebase', ['firebaseGetMessages', 'firebaseStopGettingMessages'])
},
computed: {
...mapState('firebase', ['messages'])
},
mounted() {
this.firebaseGetMessages(this.$route.params.otherUserId)
},
// Fire when the user leaves the page
unmounted() {
this.firebaseStopGettingMessages()
} }
} }
</script> </script>
......
...@@ -8,7 +8,7 @@ const routes = [ ...@@ -8,7 +8,7 @@ const routes = [
] ]
}, },
{ {
path: '/chat', path: '/chat/:otherUserId',
component: () => import('layouts/ChatLayout.vue'), component: () => import('layouts/ChatLayout.vue'),
children: [ children: [
{ path: '', component: () => import('pages/Chat.vue')} { path: '', component: () => import('pages/Chat.vue')}
......
import { firebaseAuth, firebaseDb } from 'boot/firebase' import { firebaseAuth, firebaseDb } from 'boot/firebase'
let messagesRef
const state = { const state = {
userDetails: {}, userDetails: {},
users: {} users: {},
messages: {}
} }
const mutations = { const mutations = {
...@@ -16,6 +19,14 @@ const mutations = { ...@@ -16,6 +19,14 @@ const mutations = {
updateUser(state, payload) { updateUser(state, payload) {
Object.assign(state.users[payload.userId], payload.userDetails) Object.assign(state.users[payload.userId], payload.userDetails)
},
addMessage(state, payload) {
state.messages[payload.messageId] = payload.messageDetails
},
clearMessages(state) {
state.messages = {}
} }
} }
...@@ -108,6 +119,28 @@ const actions = { ...@@ -108,6 +119,28 @@ const actions = {
userDetails userDetails
}) })
}) })
},
firebaseGetMessages({ state, commit }, otherUserId) {
let userId = state.userDetails.userId
messagesRef = firebaseDb.ref('chats/' + userId + '/' + otherUserId)
messagesRef.on(
'child_added', snapshot => {
let messageDetails = snapshot.val()
let messageId = snapshot.key
commit('addMessage', {
messageId,
messageDetails
})
}
)
},
firebaseStopGettingMessages({ commit }) {
if (messagesRef) {
messagesRef.off('child_added')
commit('clearMessages')
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment