Files
chatwoot/app/javascript/dashboard/store/modules/conversationSearch.js
Nithin David Thomas 402428fb4d feat: Splits search api by resources to improve query time [cw-47] (#6942)
* feat: Splits search api by resources to improve query time

* Review fixes

* Spacing fixes

* Update app/javascript/dashboard/modules/search/components/SearchView.vue

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>

* Review fixes

* Refactor searchview

---------

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2023-04-25 17:59:38 +05:30

156 lines
4.3 KiB
JavaScript

import SearchAPI from '../../api/search';
import types from '../mutation-types';
export const initialState = {
records: [],
contactRecords: [],
conversationRecords: [],
messageRecords: [],
uiFlags: {
isFetching: false,
isSearchCompleted: false,
contact: { isFetching: false },
conversation: { isFetching: false },
message: { isFetching: false },
},
};
export const getters = {
getConversations(state) {
return state.records;
},
getContactRecords(state) {
return state.contactRecords;
},
getConversationRecords(state) {
return state.conversationRecords;
},
getMessageRecords(state) {
return state.messageRecords;
},
getUIFlags(state) {
return state.uiFlags;
},
};
export const actions = {
async get({ commit }, { q }) {
commit(types.SEARCH_CONVERSATIONS_SET, []);
if (!q) {
return;
}
commit(types.SEARCH_CONVERSATIONS_SET_UI_FLAG, { isFetching: true });
try {
const {
data: { payload },
} = await SearchAPI.get({ q });
commit(types.SEARCH_CONVERSATIONS_SET, payload);
} catch (error) {
// Ignore error
} finally {
commit(types.SEARCH_CONVERSATIONS_SET_UI_FLAG, {
isFetching: false,
});
}
},
async fullSearch({ commit, dispatch }, { q }) {
if (!q) {
return;
}
commit(types.FULL_SEARCH_SET_UI_FLAG, {
isFetching: true,
isSearchCompleted: false,
});
try {
dispatch('contactSearch', { q });
dispatch('conversationSearch', { q });
dispatch('messageSearch', { q });
} catch (error) {
// Ignore error
} finally {
commit(types.FULL_SEARCH_SET_UI_FLAG, {
isFetching: false,
isSearchCompleted: true,
});
}
},
async contactSearch({ commit }, { q }) {
commit(types.CONTACT_SEARCH_SET, []);
commit(types.CONTACT_SEARCH_SET_UI_FLAG, { isFetching: true });
try {
const { data } = await SearchAPI.contacts({ q });
commit(types.CONTACT_SEARCH_SET, data.payload.contacts);
} catch (error) {
// Ignore error
} finally {
commit(types.CONTACT_SEARCH_SET_UI_FLAG, { isFetching: false });
}
},
async conversationSearch({ commit }, { q }) {
commit(types.CONVERSATION_SEARCH_SET, []);
commit(types.CONVERSATION_SEARCH_SET_UI_FLAG, { isFetching: true });
try {
const { data } = await SearchAPI.conversations({ q });
commit(types.CONVERSATION_SEARCH_SET, data.payload.conversations);
} catch (error) {
// Ignore error
} finally {
commit(types.CONVERSATION_SEARCH_SET_UI_FLAG, { isFetching: false });
}
},
async messageSearch({ commit }, { q }) {
commit(types.MESSAGE_SEARCH_SET, []);
commit(types.MESSAGE_SEARCH_SET_UI_FLAG, { isFetching: true });
try {
const { data } = await SearchAPI.messages({ q });
commit(types.MESSAGE_SEARCH_SET, data.payload.messages);
} catch (error) {
// Ignore error
} finally {
commit(types.MESSAGE_SEARCH_SET_UI_FLAG, { isFetching: false });
}
},
async clearSearchResults({ commit }) {
commit(types.MESSAGE_SEARCH_SET, []);
commit(types.CONVERSATION_SEARCH_SET, []);
commit(types.CONTACT_SEARCH_SET, []);
},
};
export const mutations = {
[types.SEARCH_CONVERSATIONS_SET](state, records) {
state.records = records;
},
[types.CONTACT_SEARCH_SET](state, records) {
state.contactRecords = records;
},
[types.CONVERSATION_SEARCH_SET](state, records) {
state.conversationRecords = records;
},
[types.MESSAGE_SEARCH_SET](state, records) {
state.messageRecords = records;
},
[types.SEARCH_CONVERSATIONS_SET_UI_FLAG](state, uiFlags) {
state.uiFlags = { ...state.uiFlags, ...uiFlags };
},
[types.FULL_SEARCH_SET_UI_FLAG](state, uiFlags) {
state.uiFlags = { ...state.uiFlags, ...uiFlags };
},
[types.CONTACT_SEARCH_SET_UI_FLAG](state, uiFlags) {
state.uiFlags.contact = { ...state.uiFlags.contact, ...uiFlags };
},
[types.CONVERSATION_SEARCH_SET_UI_FLAG](state, uiFlags) {
state.uiFlags.conversation = { ...state.uiFlags.conversation, ...uiFlags };
},
[types.MESSAGE_SEARCH_SET_UI_FLAG](state, uiFlags) {
state.uiFlags.message = { ...state.uiFlags.message, ...uiFlags };
},
};
export default {
namespaced: true,
state: initialState,
getters,
actions,
mutations,
};