-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add store for conversation watchers (#5808)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
- Loading branch information
1 parent
6ff0c93
commit 0d9ed06
Showing
8 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/javascript/dashboard/store/modules/conversationWatchers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import Vue from 'vue'; | ||
import types from '../mutation-types'; | ||
import { throwErrorMessage } from 'dashboard/store/utils/api'; | ||
|
||
import ConversationInboxApi from '../../api/inbox/conversation'; | ||
|
||
const state = { | ||
records: {}, | ||
uiFlags: { | ||
isFetching: false, | ||
isUpdating: false, | ||
}, | ||
}; | ||
|
||
export const getters = { | ||
getUIFlags($state) { | ||
return $state.uiFlags; | ||
}, | ||
getByConversationId: _state => conversationId => { | ||
return _state.records[conversationId]; | ||
}, | ||
}; | ||
|
||
export const actions = { | ||
show: async ({ commit }, { conversationId }) => { | ||
commit(types.SET_CONVERSATION_WATCHERS_UI_FLAG, { | ||
isFetching: true, | ||
}); | ||
|
||
try { | ||
const response = await ConversationInboxApi.fetchParticipants( | ||
conversationId | ||
); | ||
commit(types.SET_CONVERSATION_WATCHERS, { | ||
conversationId, | ||
data: response.data, | ||
}); | ||
} catch (error) { | ||
throwErrorMessage(error); | ||
} finally { | ||
commit(types.SET_CONVERSATION_WATCHERS_UI_FLAG, { | ||
isFetching: false, | ||
}); | ||
} | ||
}, | ||
|
||
update: async ({ commit }, { conversationId, userIds }) => { | ||
commit(types.SET_CONVERSATION_WATCHERS_UI_FLAG, { | ||
isUpdating: true, | ||
}); | ||
|
||
try { | ||
const response = await ConversationInboxApi.updateParticipants({ | ||
conversationId, | ||
userIds, | ||
}); | ||
commit(types.SET_CONVERSATION_WATCHERS, { | ||
conversationId, | ||
data: response.data, | ||
}); | ||
} catch (error) { | ||
throwErrorMessage(error); | ||
} finally { | ||
commit(types.SET_CONVERSATION_WATCHERS_UI_FLAG, { | ||
isUpdating: false, | ||
}); | ||
} | ||
}, | ||
}; | ||
|
||
export const mutations = { | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG]($state, data) { | ||
$state.uiFlags = { | ||
...$state.uiFlags, | ||
...data, | ||
}; | ||
}, | ||
|
||
[types.SET_CONVERSATION_WATCHERS]($state, { data, conversationId }) { | ||
Vue.set($state.records, conversationId, data); | ||
}, | ||
}; | ||
|
||
export default { | ||
namespaced: true, | ||
state, | ||
getters, | ||
actions, | ||
mutations, | ||
}; |
62 changes: 62 additions & 0 deletions
62
app/javascript/dashboard/store/modules/specs/conversationWatchers/actions.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import axios from 'axios'; | ||
import { actions } from '../../conversationWatchers'; | ||
import types from '../../../mutation-types'; | ||
|
||
const commit = jest.fn(); | ||
global.axios = axios; | ||
jest.mock('axios'); | ||
|
||
describe('#actions', () => { | ||
describe('#get', () => { | ||
it('sends correct actions if API is success', async () => { | ||
axios.get.mockResolvedValue({ data: { id: 1 } }); | ||
await actions.show({ commit }, { conversationId: 1 }); | ||
expect(commit.mock.calls).toEqual([ | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isFetching: true }], | ||
[ | ||
types.SET_CONVERSATION_WATCHERS, | ||
{ conversationId: 1, data: { id: 1 } }, | ||
], | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isFetching: false }], | ||
]); | ||
}); | ||
it('sends correct actions if API is error', async () => { | ||
axios.get.mockRejectedValue({ message: 'Incorrect header' }); | ||
await expect( | ||
actions.show({ commit }, { conversationId: 1 }) | ||
).rejects.toThrow(Error); | ||
expect(commit.mock.calls).toEqual([ | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isFetching: true }], | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isFetching: false }], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('#update', () => { | ||
it('sends correct actions if API is success', async () => { | ||
axios.patch.mockResolvedValue({ data: [{ id: 2 }] }); | ||
await actions.update( | ||
{ commit }, | ||
{ conversationId: 2, userIds: [{ id: 2 }] } | ||
); | ||
expect(commit.mock.calls).toEqual([ | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isUpdating: true }], | ||
[ | ||
types.SET_CONVERSATION_WATCHERS, | ||
{ conversationId: 2, data: [{ id: 2 }] }, | ||
], | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isUpdating: false }], | ||
]); | ||
}); | ||
it('sends correct actions if API is error', async () => { | ||
axios.patch.mockRejectedValue({ message: 'Incorrect header' }); | ||
await expect( | ||
actions.update({ commit }, { conversationId: 1, content: 'hi' }) | ||
).rejects.toThrow(Error); | ||
expect(commit.mock.calls).toEqual([ | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isUpdating: true }], | ||
[types.SET_CONVERSATION_WATCHERS_UI_FLAG, { isUpdating: false }], | ||
]); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
app/javascript/dashboard/store/modules/specs/conversationWatchers/fixtures.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const data = [ | ||
{ | ||
id: 1, | ||
name: 'Uno', | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Dos', | ||
}, | ||
{ | ||
id: 3, | ||
name: 'Tres', | ||
}, | ||
]; |
22 changes: 22 additions & 0 deletions
22
app/javascript/dashboard/store/modules/specs/conversationWatchers/getters.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { getters } from '../../conversationWatchers'; | ||
import watchersData from './fixtures'; | ||
|
||
describe('#getters', () => { | ||
it('getByConversationId', () => { | ||
const state = { records: { 1: watchersData } }; | ||
expect(getters.getByConversationId(state)(1)).toEqual(watchersData); | ||
}); | ||
|
||
it('getUIFlags', () => { | ||
const state = { | ||
uiFlags: { | ||
isFetching: false, | ||
isUpdating: false, | ||
}, | ||
}; | ||
expect(getters.getUIFlags(state)).toEqual({ | ||
isFetching: false, | ||
isUpdating: false, | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
app/javascript/dashboard/store/modules/specs/conversationWatchers/mutations.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import types from '../../../mutation-types'; | ||
import { mutations } from '../../conversationWatchers'; | ||
|
||
describe('#mutations', () => { | ||
describe('#SET_CONVERSATION_WATCHERS', () => { | ||
it('sets an individual record', () => { | ||
let state = { | ||
records: {}, | ||
}; | ||
|
||
mutations[types.SET_CONVERSATION_WATCHERS](state, { | ||
data: [], | ||
conversationId: 1, | ||
}); | ||
expect(state.records).toEqual({ 1: [] }); | ||
}); | ||
}); | ||
|
||
describe('#SET_CONVERSATION_WATCHERS_UI_FLAG', () => { | ||
it('set ui flags', () => { | ||
let state = { | ||
uiFlags: { | ||
isFetching: true, | ||
isUpdating: false, | ||
}, | ||
}; | ||
|
||
mutations[types.SET_CONVERSATION_WATCHERS_UI_FLAG](state, { | ||
isFetching: false, | ||
}); | ||
expect(state.uiFlags).toEqual({ | ||
isFetching: false, | ||
isUpdating: false, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters