diff --git a/app/javascript/dashboard/api/inbox/message.js b/app/javascript/dashboard/api/inbox/message.js index 0020a4138..84e77fb8b 100644 --- a/app/javascript/dashboard/api/inbox/message.js +++ b/app/javascript/dashboard/api/inbox/message.js @@ -30,6 +30,10 @@ class MessageApi extends ApiClient { }); } + delete(conversationID, messageId) { + return axios.delete(`${this.url}/${conversationID}/messages/${messageId}`); + } + getPreviousMessages({ conversationId, before }) { return axios.get(`${this.url}/${conversationId}/messages`, { params: { before }, diff --git a/app/javascript/dashboard/store/modules/conversations/actions.js b/app/javascript/dashboard/store/modules/conversations/actions.js index e983ca2f2..042c3e2a1 100644 --- a/app/javascript/dashboard/store/modules/conversations/actions.js +++ b/app/javascript/dashboard/store/modules/conversations/actions.js @@ -178,6 +178,20 @@ const actions = { commit(types.default.ADD_MESSAGE, message); }, + deleteMessage: async function deleteLabels( + { commit }, + { conversationId, messageId } + ) { + try { + const response = await MessageApi.delete(conversationId, messageId); + const { data } = response; + // The delete message is actually deleting the content. + commit(types.default.ADD_MESSAGE, data); + } catch (error) { + throw new Error(error); + } + }, + addConversation({ commit, state, dispatch }, conversation) { const { currentInbox } = state; const { diff --git a/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js index a4e59f5c3..ad794611c 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js @@ -238,3 +238,22 @@ describe('#actions', () => { }); }); }); + +describe('#deleteMessage', () => { + it('sends correct actions if API is success', async () => { + const [conversationId, messageId] = [1, 1]; + axios.delete.mockResolvedValue({ data: { id: 1, content: 'deleted' } }); + await actions.deleteMessage({ commit }, { conversationId, messageId }); + expect(commit.mock.calls).toEqual([ + [types.default.ADD_MESSAGE, { id: 1, content: 'deleted' }], + ]); + }); + it('sends no actions if API is error', async () => { + const [conversationId, messageId] = [1, 1]; + axios.delete.mockRejectedValue({ message: 'Incorrect header' }); + await expect( + actions.deleteMessage({ commit }, { conversationId, messageId }) + ).rejects.toThrow(Error); + expect(commit.mock.calls).toEqual([]); + }); +}); diff --git a/app/javascript/dashboard/store/mutation-types.js b/app/javascript/dashboard/store/mutation-types.js index 657c6ec57..a61b14362 100755 --- a/app/javascript/dashboard/store/mutation-types.js +++ b/app/javascript/dashboard/store/mutation-types.js @@ -31,6 +31,7 @@ export default { ASSIGN_TEAM: 'ASSIGN_TEAM', SET_CHAT_META: 'SET_CHAT_META', ADD_MESSAGE: 'ADD_MESSAGE', + DELETE_MESSAGE: 'DELETE_MESSAGE', ADD_PENDING_MESSAGE: 'ADD_PENDING_MESSAGE', MARK_MESSAGE_READ: 'MARK_MESSAGE_READ', SET_PREVIOUS_CONVERSATIONS: 'SET_PREVIOUS_CONVERSATIONS',