/
conversations.js
64 lines (52 loc) · 1.63 KB
/
conversations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Vue from 'vue'
import uuidv4 from 'uuid/v4'
const state = {
all: {},
allIds: [],
allMsgIds: []
}
const mutations = {
SET_CONVERSATION (state, { conversation }) {
const data = conversation.data()
state.all = {...state.all, [conversation.id]: { users: data.users, created: data.created, messages: [] }}
state.allIds.push(conversation.id)
},
ADD_MESSAGE (state, { conversationId, message }) {
if (!state.allMsgIds.includes(message.id)) {
state.all[conversationId].messages.push(message)
state.allMsgIds.push(message.id)
}
},
}
const actions = {
sendMessage ({ commit, rootState }, { text, created, sender, conversationId }) {
const convoRef = rootState.db.collection('conversations').doc(conversationId)
convoRef.update({
messages: [...state.all[conversationId].messages, { id: uuidv4(), created, sender, text }]
})
.then(res => console.log('Message sent.'))
.catch(err => console.log('Error', err))
},
seed ({ rootState }) {
let convoRef = rootState.db.collection('conversations')
convoRef.add({
created: Date.now(),
users: ['mr_a', 'mr_b'],
messages: [
{ id: uuidv4(), text: 'Hi there', sender: 'mr_a', created: Date.now() },
{ id: uuidv4(), text: 'Hi to you too!', sender: 'mr_b', created: Date.now() }
]
})
convoRef.add({
created: Date.now(),
users: ['mr_a', 'mr_c'],
messages: []
})
},
async get ({ commit, rootState }) {
let convoRef = rootState.db.collection('conversations')
let convos = await convoRef.get()
convos.forEach(conversation => commit('SET_CONVERSATION', { conversation }))
}
}
export default { namespaced: true, state, mutations, actions }