Purge state before loading new conversations

Signed-off-by: Marco Ambrosini <marcoambrosini@pm.me>
This commit is contained in:
Marco Ambrosini 2019-10-22 11:11:02 +02:00
Родитель 5e0d24766e
Коммит 04a79c8e52
2 изменённых файлов: 22 добавлений и 16 удалений

Просмотреть файл

@ -65,6 +65,7 @@ export default {
* to the store.
*/
const conversations = await fetchConversations()
this.$store.dispatch('purgeConversationsStore')
conversations.data.ocs.data.forEach(conversation => {
this.$store.dispatch('addConversation', conversation)
})

Просмотреть файл

@ -21,10 +21,15 @@
*/
import Vue from 'vue'
const getDefaultState = () => {
return {
conversations: {
}
}
}
const state = {
conversations: {
},
conversationsNames: {
}
}
@ -43,18 +48,6 @@ const mutations = {
addConversation(state, conversation) {
Vue.set(state.conversations, conversation.token, conversation)
},
/**
* Creates a key-value pair with conversation id and name
* respectively.
*
* @param {object} state current state object;
* @param {object} object destructuring object;
* @param {string} object.token conversation token;
* @param {string} object.displayName conversation name;
*/
indexConversationName(state, { token, displayName }) {
Vue.set(state.conversationsNames, token, displayName)
},
/**
* Deletes a conversation from the store.
* @param {object} state current store state;
@ -62,7 +55,13 @@ const mutations = {
*/
deleteConversation(state, conversation) {
Vue.delete(state.conversations, conversation.token)
Vue.delete(state.conversationsNames, conversation.token)
},
/**
* Resets the store to it's original state
* @param {object} state current store state;
*/
purgeConversationsStore(state) {
Object.assign(state, getDefaultState())
}
}
@ -75,7 +74,6 @@ const actions = {
*/
addConversation(context, conversation) {
context.commit('addConversation', conversation)
context.commit('indexConversationName', conversation)
},
/**
@ -86,6 +84,13 @@ const actions = {
*/
deleteConversation(context, conversation) {
context.commit('deleteConversation', conversation)
},
/**
* Resets the store to it's original state.
* @param {object} context default store context;
*/
purgeConversationsStore(context) {
context.commit('purgeConversationsStore')
}
}