Chatwoot/app/javascript/widget/store/modules/conversationv2/actions.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-09-16 06:52:05 +00:00
import conversationPublicAPI from 'widget/api/conversationPublic';
2021-09-17 06:20:30 +00:00
import MessagePublicAPI from 'widget/api/messagePublic';
2021-09-16 06:52:05 +00:00
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);
2021-09-16 06:52:05 +00:00
commit('addConversationId', conversation.id);
2021-09-17 06:20:30 +00:00
commit(
'messagev2/addMessagesEntry',
{ conversationId, messages },
{ root: true }
);
commit('addMessageIdsToConversation', {
conversationId,
messages,
});
2021-09-16 06:52:05 +00:00
});
} catch (error) {
throw new Error(error);
} finally {
commit('setUIFlag', { isFetching: false });
}
},
2021-09-17 06:20:30 +00:00
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 },
});
}
},
2021-09-16 06:52:05 +00:00
createConversation: async (
{ commit },
{ inboxIdentifier, contactIdentifier }
) => {
2021-09-17 06:20:30 +00:00
commit('setUIFlag', { isCreating: true });
2021-09-16 06:52:05 +00:00
try {
const params = { inboxIdentifier, contactIdentifier };
const { data } = await conversationPublicAPI.create(params);
const { id: conversationId, messages } = data;
commit('addConversationEntry', data);
commit('addConversationId', conversationId);
2021-09-17 06:20:30 +00:00
commit(
'messagev2/addMessagesEntry',
{ conversationId, messages },
{ root: true }
);
commit('addMessageIdsToConversation', {
conversationId,
messages,
});
2021-09-16 06:52:05 +00:00
} catch (error) {
throw new Error(error);
} finally {
2021-09-17 06:20:30 +00:00
commit('setUIFlag', { isCreating: false });
2021-09-16 06:52:05 +00:00
}
},
};