Chatwoot/app/javascript/widget/store/modules/conversationv2/actions.js
Nithin David Thomas 211f5cdedd
feat: Refactors API on widget side to support multiple conversations (#3032)
* State structure

* How to refactr action modules

* feat: Add state and mutations for multiple conversation on widget

* Adds actions to support multiple conversation

* feat: Adds public API endpoints for widget

* fixes lint errors

* Refactors store

* Update mutations to accommodate new changes in store

* Refactors actions in messages

* fixes broken tests
2021-09-23 11:56:24 +05:30

87 lines
2.5 KiB
JavaScript

import conversationPublicAPI from 'widget/api/conversationPublic';
import MessagePublicAPI from 'widget/api/messagePublic';
export const actions = {
fetchAllConversations: async (
{ commit },
{ inboxIdentifier, contactIdentifier }
) => {
try {
commit('setUIFlag', { isFetching: true });
const { data } = await conversationPublicAPI.get(
inboxIdentifier,
contactIdentifier
);
data.forEach(conversation => {
const { id: conversationId, messages } = conversation;
commit('addConversationEntry', conversation);
commit('addConversationId', conversation.id);
commit(
'messagev2/addMessagesEntry',
{ conversationId, messages },
{ root: true }
);
commit('addMessageIdsToConversation', {
conversationId,
messages,
});
});
} catch (error) {
throw new Error(error);
} finally {
commit('setUIFlag', { isFetching: false });
}
},
fetchConversationById: async ({ commit }, params) => {
const { conversationId, inboxIdentifier, contactIdentifier } = params;
try {
commit('setConversationUIFlag', { isFetching: true });
const { data } = await MessagePublicAPI.get(
inboxIdentifier,
contactIdentifier,
conversationId
);
const { messages } = data;
commit('updateConversationEntry', data);
commit('addMessagesEntry', { conversationId, messages });
commit('addMessageIds', { conversationId, messages });
} catch (error) {
throw new Error(error);
} finally {
commit('setConversationUIFlag', {
conversationId,
uiFlags: { isFetching: false },
});
}
},
createConversation: async (
{ commit },
{ inboxIdentifier, contactIdentifier }
) => {
commit('setUIFlag', { isCreating: true });
try {
const params = { inboxIdentifier, contactIdentifier };
const { data } = await conversationPublicAPI.create(params);
const { id: conversationId, messages } = data;
commit('addConversationEntry', data);
commit('addConversationId', conversationId);
commit(
'messagev2/addMessagesEntry',
{ conversationId, messages },
{ root: true }
);
commit('addMessageIdsToConversation', {
conversationId,
messages,
});
} catch (error) {
throw new Error(error);
} finally {
commit('setUIFlag', { isCreating: false });
}
},
};