feat: Add Bulk actions to conversations (#4647)
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
44
app/javascript/dashboard/store/modules/bulkActions.js
Normal file
44
app/javascript/dashboard/store/modules/bulkActions.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import types from '../mutation-types';
|
||||
import BulkActionsAPI from '../../api/bulkActions';
|
||||
|
||||
export const state = {
|
||||
uiFlags: {
|
||||
isUpdating: false,
|
||||
},
|
||||
};
|
||||
|
||||
export const getters = {
|
||||
getUIFlags(_state) {
|
||||
return _state.uiFlags;
|
||||
},
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
process: async function processAction({ commit }, payload) {
|
||||
commit(types.SET_BULK_ACTIONS_FLAG, { isUpdating: true });
|
||||
try {
|
||||
await BulkActionsAPI.create(payload);
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
} finally {
|
||||
commit(types.SET_BULK_ACTIONS_FLAG, { isUpdating: false });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export const mutations = {
|
||||
[types.SET_BULK_ACTIONS_FLAG](_state, data) {
|
||||
_state.uiFlags = {
|
||||
..._state.uiFlags,
|
||||
...data,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
actions,
|
||||
state,
|
||||
getters,
|
||||
mutations,
|
||||
};
|
||||
@@ -26,13 +26,16 @@ export const getters = {
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
async fetch({ commit }, { inboxId }) {
|
||||
async fetch({ commit }, inboxIds) {
|
||||
commit(types.SET_INBOX_ASSIGNABLE_AGENTS_UI_FLAG, { isFetching: true });
|
||||
try {
|
||||
const {
|
||||
data: { payload },
|
||||
} = await AssignableAgentsAPI.get([inboxId]);
|
||||
commit(types.SET_INBOX_ASSIGNABLE_AGENTS, { inboxId, members: payload });
|
||||
} = await AssignableAgentsAPI.get(inboxIds);
|
||||
commit(types.SET_INBOX_ASSIGNABLE_AGENTS, {
|
||||
inboxId: inboxIds.join(','),
|
||||
members: payload,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
} finally {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import axios from 'axios';
|
||||
import { actions } from '../../bulkActions';
|
||||
import * as types from '../../../mutation-types';
|
||||
import payload from './fixtures';
|
||||
const commit = jest.fn();
|
||||
global.axios = axios;
|
||||
jest.mock('axios');
|
||||
|
||||
describe('#actions', () => {
|
||||
describe('#create', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
axios.post.mockResolvedValue({ data: payload });
|
||||
await actions.process({ commit }, payload);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.default.SET_BULK_ACTIONS_FLAG, { isUpdating: true }],
|
||||
[types.default.SET_BULK_ACTIONS_FLAG, { isUpdating: false }],
|
||||
]);
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
||||
await expect(actions.process({ commit })).rejects.toThrow(Error);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.default.SET_BULK_ACTIONS_FLAG, { isUpdating: true }],
|
||||
[types.default.SET_BULK_ACTIONS_FLAG, { isUpdating: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
export default {
|
||||
type: 'Conversation',
|
||||
ids: [64, 39],
|
||||
fields: { assignee_id: 6 },
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { getters } from '../../bulkActions';
|
||||
|
||||
describe('#getters', () => {
|
||||
it('getUIFlags', () => {
|
||||
const state = {
|
||||
uiFlags: {
|
||||
isUpdating: false,
|
||||
},
|
||||
};
|
||||
expect(getters.getUIFlags(state)).toEqual({
|
||||
isUpdating: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import types from '../../../mutation-types';
|
||||
import { mutations } from '../../bulkActions';
|
||||
|
||||
describe('#mutations', () => {
|
||||
describe('#toggleUiFlag', () => {
|
||||
it('set update flags', () => {
|
||||
const state = { uiFlags: { isUpdating: false } };
|
||||
mutations[types.SET_BULK_ACTIONS_FLAG](state, { isUpdating: true });
|
||||
expect(state.uiFlags.isUpdating).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -12,12 +12,12 @@ describe('#actions', () => {
|
||||
axios.get.mockResolvedValue({
|
||||
data: { payload: agentsData },
|
||||
});
|
||||
await actions.fetch({ commit }, { inboxId: 1 });
|
||||
await actions.fetch({ commit }, [1]);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_INBOX_ASSIGNABLE_AGENTS_UI_FLAG, { isFetching: true }],
|
||||
[
|
||||
types.SET_INBOX_ASSIGNABLE_AGENTS,
|
||||
{ inboxId: 1, members: agentsData },
|
||||
{ inboxId: '1', members: agentsData },
|
||||
],
|
||||
[types.SET_INBOX_ASSIGNABLE_AGENTS_UI_FLAG, { isFetching: false }],
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user