feat: Add actions and API for deleting message via API (#2519)

This commit is contained in:
Nithin David Thomas 2021-06-29 11:19:08 +05:30 committed by GitHub
parent b705eb39e2
commit 044b6872a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 0 deletions

View file

@ -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 },

View file

@ -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 {

View file

@ -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([]);
});
});

View file

@ -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',