@empathyco/x-components
Version:
Empathy X Components
111 lines (108 loc) • 3.57 kB
JavaScript
import { isFacetFilter } from '@empathyco/x-types';
import { setConfig, mergeConfig } from '../../../store/utils/config-store.utils.js';
import { groupItemsBy } from '../../../utils/array.js';
import { UNKNOWN_FACET_KEY } from '../../facets/store/constants.js';
import { fetchAndSaveAiSuggestionsSearch } from './actions/fetch-and-save-ai-suggestions-search.action.js';
import { fetchAndSaveAiSuggestions } from './actions/fetch-and-save-ai-suggestions.action.js';
import { saveOrigin } from './actions/save-origin.action.js';
import { setUrlParams } from './actions/set-url-params.action.js';
import { query } from './getters/ai-query.getter.js';
import { suggestionsRequest } from './getters/suggestions-request.getter.js';
import { suggestionsSearchRequest } from './getters/suggestions-search-request.getter.js';
/**
* {@link XStoreModule} For the ai module.
*
* @internal
*/
const aiXStoreModule = {
state: () => ({
...resettableAiState(),
selectedFilters: {},
query: '',
config: {},
params: {},
origin: null,
relatedTags: [],
}),
getters: {
suggestionsRequest,
suggestionsSearchRequest,
query,
},
mutations: {
/* Streamed fields */
setResponseText: (state, responseText) => {
state.responseText = responseText;
},
setSuggestionText: (state, suggestionText) => {
state.suggestionText = suggestionText;
},
setQueries: (state, queries) => {
state.queries = queries;
},
setTagging: (state, tagging) => {
state.tagging = tagging;
},
/* END Streamed fields */
setSuggestionsSearch: (state, suggestionsSearch) => {
state.suggestionsSearch = suggestionsSearch;
},
setSuggestionsLoading: (state, value) => {
state.suggestionsLoading = value;
},
setSuggestionsSearchLoading: (state, value) => {
state.suggestionsSearchLoading = value;
},
setQuery: (state, query) => {
state.query = query;
},
setParams(state, params) {
state.params = params;
},
setOrigin(state, origin = null) {
state.origin = origin;
},
resetAiState(state) {
Object.assign(state, resettableAiState());
},
setAiRelatedTags(state, relatedTags) {
state.relatedTags = relatedTags;
},
setIsNoResults(state, isNoResults) {
state.isNoResults = isNoResults;
},
setSelectedFilters(state, selectedFilters) {
state.selectedFilters = groupItemsBy(selectedFilters, filter => isFacetFilter(filter) ? filter.facetId : UNKNOWN_FACET_KEY);
},
setConfig,
mergeConfig,
},
actions: {
fetchAndSaveAiSuggestions,
fetchAndSaveAiSuggestionsSearch,
setUrlParams,
saveOrigin,
},
};
/**
* Function to return the "resettable" part of the state. This will be used in the `resetState`
* mutation to reset to the initial state.
*
* @returns The "resettable" part of the {@link AiState}.
*
* @internal
*/
function resettableAiState() {
return {
responseText: '',
suggestionText: '',
queries: [],
tagging: undefined,
suggestionsSearch: [],
suggestionsLoading: false,
suggestionsSearchLoading: false,
isNoResults: true,
};
}
export { aiXStoreModule };
//# sourceMappingURL=module.js.map