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

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-09-23 06:50:53 +00:00
import MessagePublicAPI from 'widget/api/messagePublic';
2021-09-17 16:20:16 +00:00
import { refreshActionCableConnector } from 'widget/helpers/actionCable';
2021-09-16 06:52:05 +00:00
import {
createTemporaryMessage,
createTemporaryAttachmentMessage,
} from './helpers';
export const actions = {
sendMessage: async ({ commit }, params) => {
2021-09-17 06:20:30 +00:00
try {
commit(
'conversationV2/setConversationUIFlag',
{ isCreating: true },
{ root: true }
);
const { content, conversationId } = params;
const message = createTemporaryMessage({ content });
const { id: echoId } = message;
const messages = [message];
commit('addMessagesEntry', { conversationId, messages });
commit('addMessageIds', { conversationId, messages });
await MessagePublicAPI.create(
...params,
content,
echoId
);
} catch (error) {
throw new Error(error);
} finally {
commit(
'conversationV2/setConversationUIFlag',
{ isCreating: false },
{ root: true }
);
}
2021-09-16 06:52:05 +00:00
},
sendAttachment: async ({ commit }, params) => {
try {
2021-09-17 06:20:30 +00:00
commit(
'conversationV2/setConversationUIFlag',
{ isCreating: true },
{ root: true }
);
const {
attachment: { thumbUrl, fileType },
conversationId,
} = params;
const message = createTemporaryAttachmentMessage({
thumbUrl,
fileType,
});
const messages = [message];
const { id: echoId, ...rest } = message;
commit('addMessagesEntry', { conversationId, messages });
commit('addMessageIds', { conversationId, messages });
const { data } = await MessagePublicAPI.create({
echo_id: echoId,
...rest,
});
2021-09-16 06:52:05 +00:00
commit('updateAttachmentMessageStatus', {
message: data,
tempId: message.id,
});
} catch (error) {
2021-09-17 06:20:30 +00:00
throw new Error(error);
} finally {
commit(
'conversationV2/setConversationUIFlag',
{ isCreating: false },
{ root: true }
);
2021-09-16 06:52:05 +00:00
}
},
2021-09-17 06:20:30 +00:00
updateMessage: async (
2021-09-16 06:52:05 +00:00
{ commit, dispatch },
{ email, messageId, submittedValues }
) => {
try {
2021-09-17 06:20:30 +00:00
commit('setMessageUIFlag', {
messageId,
uiFlags: { isUpdating: true },
});
2021-09-16 06:52:05 +00:00
const {
data: { contact: { pubsub_token: pubsubToken } = {} },
2021-09-17 06:20:30 +00:00
} = await MessagePublicAPI.update({
2021-09-16 06:52:05 +00:00
email,
messageId,
values: submittedValues,
});
2021-09-17 06:20:30 +00:00
commit('updateMessageEntry', {
id: messageId,
content_attributes: {
submitted_email: email,
submitted_values: email ? null : submittedValues,
2021-09-16 06:52:05 +00:00
},
2021-09-17 06:20:30 +00:00
});
2021-09-16 06:52:05 +00:00
dispatch('contacts/get', {}, { root: true });
refreshActionCableConnector(pubsubToken);
} catch (error) {
2021-09-17 06:20:30 +00:00
throw new Error(error);
} finally {
commit('setMessageUIFlag', { messageId, uiFlags: { isUpdating: false } });
2021-09-16 06:52:05 +00:00
}
},
};