Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Implement config utils in the modules with config #1321

Merged
merged 9 commits into from
Oct 12, 2023
9 changes: 4 additions & 5 deletions packages/x-components/src/store/utils/config-store.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Vue from 'vue';

/**
* Config mutations, containing a method to change the current config and other to merge
* a new one.
Expand Down Expand Up @@ -28,7 +30,7 @@ export interface ConfigMutations<T extends { config: T['config'] }> {
* @public
*/
export function setConfig<T extends { config: T['config'] }>(state: T, config: T['config']): void {
state.config = config;
Vue.set(state, 'config', config);
}

/**
Expand All @@ -43,8 +45,5 @@ export function mergeConfig<T extends { config: T['config'] }>(
state: T,
config: Partial<T['config']>
): void {
state.config = {
...state.config,
...config
};
Object.assign(<object>state.config, config);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setConfig, mergeConfig } from '../../../store/utils/config-store.utils';
import { EmpathizeXStoreModule } from './types';

/**
Expand All @@ -14,7 +15,9 @@ export const empathizeXStoreModule: EmpathizeXStoreModule = {
mutations: {
setIsOpen(state, isOpen) {
state.isOpen = isOpen;
}
},
setConfig,
mergeConfig
},
actions: {}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { XStoreModule } from '../../../store';
import { EmpathizeConfig } from '../config.types';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* Empathize store state.
Expand Down Expand Up @@ -33,7 +34,7 @@ export interface EmpathizeGetters {}
*
* @public
*/
export interface EmpathizeMutations {
export interface EmpathizeMutations extends ConfigMutations<EmpathizeState> {
/**
* Sets the isOpen state of the module, which is used in the Alias API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe('testing facets module getters', () => {
);

const store = createFacetsStore([parentFilter, childFilter, parentFilterWithoutChild], []);
store.commit('setFacetsConfig', {
store.commit('setConfig', {
filtersStrategyForRequest: 'leaves-only'
});

Expand Down
13 changes: 6 additions & 7 deletions packages/x-components/src/x-modules/facets/store/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Facet } from '@empathyco/x-types';
import Vue from 'vue';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import { setQuery } from '../../../store/utils/query.utils';
import { facets } from './getters/facets.getter';
import { selectedFiltersByFacet } from './getters/selected-filters-by-facet.getter';
import { selectedFilters } from './getters/selected-filters.getter';
import { FacetGroupEntry, FacetsConfig, FacetsXStoreModule } from './types';
import { FacetGroupEntry, FacetsXStoreModule } from './types';
import { selectedFiltersForRequest } from './getters/selected-filters-for-request.getter';

/**
Expand Down Expand Up @@ -55,12 +57,9 @@ export const facetsXStoreModule: FacetsXStoreModule = {
setFacet(state, facet: Facet) {
Vue.set(state.facets, facet.id, facet);
},
setFacetsConfig(state, config: FacetsConfig) {
state.config = config;
},
setQuery(state, query) {
state.query = query;
},
setConfig,
mergeConfig,
setQuery,
setStickyFilter(state, filter) {
if (!state.stickyFilters[filter.id]) {
Vue.set(state.stickyFilters, filter.id, filter);
Expand Down
9 changes: 2 additions & 7 deletions packages/x-components/src/x-modules/facets/store/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Facet, Filter, RawFilter } from '@empathyco/x-types';
import { XActionContext, XStoreModule } from '../../../store';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* Facets store state.
Expand Down Expand Up @@ -52,7 +53,7 @@ export interface FacetsGetters {
*
* @public
*/
export interface FacetsMutations {
export interface FacetsMutations extends ConfigMutations<FacetsState> {
/**
* Updates the state of a filter.
*
Expand Down Expand Up @@ -109,12 +110,6 @@ export interface FacetsMutations {
* @param facet - The facet to set in the store.
*/
setFacet(facet: Facet): void;
/**
* Sets the {@link FacetsState.facets | facets} config.
*
* @param config - The new config.
*/
setFacetsConfig(config: FacetsConfig): void;
/**
* Adds the filter to the {@link FacetsState.stickyFilters | sticky filters} record.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setQuery } from '../../../store/utils/query.utils';
import { localStorageService } from '../../../utils/storage';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import { addQueryToHistory } from './actions/add-query-to-history.action';
// eslint-disable-next-line max-len
import { loadHistoryQueriesFromBrowserStorage } from './actions/load-history-queries-from-browser-storage.action';
Expand Down Expand Up @@ -51,6 +52,8 @@ export const historyQueriesXStoreModule: HistoryQueriesXStoreModule = {
state.sessionTimeStampInMs = sessionTimeStamp;
},
setQuery,
setConfig,
mergeConfig,
setIsEnabled(state, isEnabled) {
state.isEnabled = isEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { QueryMutations, QueryState } from '../../../store/utils/query.utils';
import { UrlParams } from '../../../types/url-params';
import { HistoryQueriesConfig } from '../config.types';
import { InternalSearchResponse } from '../../search/index';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* HistoryQueries store state.
Expand Down Expand Up @@ -68,7 +69,9 @@ export interface HistoryQueriesGetters {
*
* @public
*/
export interface HistoryQueriesMutations extends QueryMutations {
export interface HistoryQueriesMutations
extends QueryMutations,
ConfigMutations<HistoryQueriesState> {
/**
* Sets the {@link HistoryQueriesState.historyQueries} property.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setQuery } from '../../../store/utils/query.utils';
import { setStatus } from '../../../store/utils/status-store.utils';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import {
cancelFetchAndSaveIdentifierResults,
fetchAndSaveIdentifierResults
Expand Down Expand Up @@ -47,7 +48,9 @@ export const identifierResultsXStoreModule: IdentifierResultsXStoreModule = {
state.params = params;
},
setQuery,
setStatus
setStatus,
setConfig,
mergeConfig
},
actions: {
cancelFetchAndSaveIdentifierResults,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { QueryMutations, QueryState } from '../../../store/utils/query.utils';
import { StatusMutations, StatusState } from '../../../store/utils/status-store.utils';
import { QueryOrigin, QueryOriginInit } from '../../../types/origin';
import { IdentifierResultsConfig } from '../config.types';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* IdentifierResults store state.
Expand Down Expand Up @@ -49,7 +50,10 @@ export interface IdentifierResultsGetters {
*
* @public
*/
export interface IdentifierResultsMutations extends StatusMutations, QueryMutations {
export interface IdentifierResultsMutations
extends StatusMutations,
QueryMutations,
ConfigMutations<IdentifierResultsState> {
/**
* Sets the identifier results of the module.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setQuery } from '../../../store/utils/query.utils';
import { setStatus } from '../../../store/utils/status-store.utils';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import {
cancelFetchAndSaveNextQueries,
fetchAndSaveNextQueries
Expand Down Expand Up @@ -54,7 +55,9 @@ export const nextQueriesXStoreModule: NextQueriesXStoreModule = {
},
resetResultsPreview(state) {
state.resultsPreview = {};
}
},
setConfig,
mergeConfig
},
actions: {
cancelFetchAndSaveNextQueries,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { StatusMutations, StatusState } from '../../../store/utils/status-store.
import { UrlParams } from '../../../types/url-params';
import { NextQueriesConfig } from '../config.types';
import { FeatureLocation } from '../../../types/index';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* Next queries module state.
Expand Down Expand Up @@ -42,7 +43,7 @@ export interface NextQueriesState extends StatusState, QueryState {
export interface NextQueriesGetters {
/**
* Request object to retrieve the next queries using the search adapter, or null if there is
* not valid data to conform a valid request.
* no valid data to conform a valid request.
*/
request: NextQueriesRequest | null;
/** List of next queries that have not been searched before. */
Expand All @@ -54,7 +55,10 @@ export interface NextQueriesGetters {
*
* @public
*/
export interface NextQueriesMutations extends StatusMutations, QueryMutations {
export interface NextQueriesMutations
extends StatusMutations,
QueryMutations,
ConfigMutations<NextQueriesState> {
/**
* Sets the query of the module, which is used to retrieve the next-queries.
*
Expand All @@ -79,15 +83,13 @@ export interface NextQueriesMutations extends StatusMutations, QueryMutations {
* @param params - The new extra params.
*/
setParams(params: Dictionary<unknown>): void;

/**
* Adds a new entry to the result's dictionary.
*
* @param resultsPreview - Object containing the next query,
* the totalResults and the results to add.
*/
setResultsPreview(resultsPreview: Dictionary<PreviewResults>): void;

/**
* Resets the result's dictionary.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setStatus } from '../../../store/utils/status-store.utils';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import {
cancelFetchAndSaveSuggestions,
fetchAndSaveSuggestions
Expand Down Expand Up @@ -35,10 +36,12 @@ export const popularSearchesXStoreModule: PopularSearchesXStoreModule = {
setSearchedQueries(state, searchedQueries) {
state.searchedQueries = searchedQueries;
},
setStatus,
setParams(state, params) {
state.params = params;
}
},
setStatus,
setConfig,
mergeConfig
},
actions: {
cancelFetchAndSaveSuggestions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Dictionary } from '@empathyco/x-utils';
import { XActionContext, XStoreModule } from '../../../store';
import { StatusMutations, StatusState } from '../../../store/utils/status-store.utils';
import { PopularSearchesConfig } from '../config.types';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* Popular searches module state.
Expand Down Expand Up @@ -38,7 +39,9 @@ export interface PopularSearchesGetters {
*
* @public
*/
export interface PopularSearchesMutations extends StatusMutations {
export interface PopularSearchesMutations
extends StatusMutations,
ConfigMutations<PopularSearchesState> {
/**
* Sets the suggestions of the module.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import { QueriesPreviewXStoreModule } from './types';
import { fetchQueryPreview } from './actions/fetch-query-preview.action';
import { fetchAndSaveQueryPreview } from './actions/fetch-and-save-query-preview.action';
Expand Down Expand Up @@ -38,7 +39,9 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = {
},
setSelectedQueryPreview(state, selectedQueryPreview) {
state.selectedQueryPreview = selectedQueryPreview;
}
},
setConfig,
mergeConfig
},
actions: {
fetchQueryPreview,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { XActionContext } from '../../../store/actions.types';
import { XStoreModule } from '../../../store/store.types';
import { RequestStatus, StatusState } from '../../../store/utils/status-store.utils';
import { QueriesPreviewConfig } from '../config.types';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* QueriesPreview store state.
Expand Down Expand Up @@ -75,7 +76,7 @@ export interface QueriesPreviewGetters {
*
* @public
*/
export interface QueriesPreviewMutations {
export interface QueriesPreviewMutations extends ConfigMutations<QueriesPreviewState> {
/**
* Removes a query preview entry from the queries preview's dictionary.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setQuery } from '../../../store/utils/query.utils';
import { setStatus } from '../../../store/utils/status-store.utils';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import {
cancelFetchAndSaveSuggestions,
fetchAndSaveSuggestions
Expand Down Expand Up @@ -41,7 +42,9 @@ export const querySuggestionsXStoreModule: QuerySuggestionsXStoreModule = {
setStatus,
setParams(state, params) {
state.params = params;
}
},
setConfig,
mergeConfig
},
actions: {
cancelFetchAndSaveSuggestions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { QueryMutations, QueryState } from '../../../store/utils/query.utils';
import { StatusMutations, StatusState } from '../../../store/utils/status-store.utils';
import { UrlParams } from '../../../types/url-params';
import { QuerySuggestionsConfig } from '../config.types';
import { ConfigMutations } from '../../../store/utils/config-store.utils';

/**
* QuerySuggestions store state.
Expand Down Expand Up @@ -44,7 +45,10 @@ export interface QuerySuggestionsGetters {
*
* @public
*/
export interface QuerySuggestionsMutations extends StatusMutations, QueryMutations {
export interface QuerySuggestionsMutations
extends StatusMutations,
QueryMutations,
ConfigMutations<QuerySuggestionsState> {
/**
* Sets the query of the query suggestions module.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setStatus } from '../../../store/utils/status-store.utils';
import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils';
import {
cancelFetchAndSaveRecommendations,
fetchAndSaveRecommendations
Expand Down Expand Up @@ -41,7 +42,9 @@ export const recommendationsXStoreModule: RecommendationsXStoreModule = {
if (stateRecommendation) {
Object.assign(stateRecommendation, recommendation);
}
}
},
setConfig,
mergeConfig
},
actions: {
cancelFetchAndSaveRecommendations,
Expand Down
Loading