@empathyco/x-components
Version:
Empathy X Components
263 lines (260 loc) • 7.26 kB
JavaScript
import { createRawFilters } from '../../utils/filters.js';
import '../../utils/storage.js';
import { namespacedWireCommit, namespacedWireCommitWithoutPayload, namespacedWireDispatch, namespacedWireDispatchWithoutPayload } from '../../wiring/namespaced-wires.factory.js';
import { filterTruthyPayload } from '../../wiring/wires.operators.js';
import { createWiring } from '../../wiring/wiring.utils.js';
/**
* `search` {@link XModuleName | XModule name}.
*
* @internal
*/
const moduleName = 'search';
/**
* WireCommit for {@link SearchXModule}.
*
* @internal
*/
const wireCommit = namespacedWireCommit(moduleName);
/**
* WireCommit without Payload for {@link SearchXModule}.
*
* @internal
*/
const wireCommitWithoutPayload = namespacedWireCommitWithoutPayload(moduleName);
/**
* WireDispatch for {@link SearchXModule}.
*
* @internal
*/
const wireDispatch = namespacedWireDispatch(moduleName);
/**
* WireDispatchWithoutPayload for {@link SearchXModule}.
*
* @internal
*/
const wireDispatchWithoutPayload = namespacedWireDispatchWithoutPayload(moduleName);
/**
* Cancels the {@link SearchActions.fetchAndSaveSearchResponse} request promise.
*
* @public
*/
const cancelFetchAndSaveSearchResponseWire = wireDispatchWithoutPayload('cancelFetchAndSaveSearchResponse');
/**
* Sets the search state `origin`.
*
* @public
*/
const saveOriginWire = wireDispatch('saveOrigin', ({ metadata }) => metadata);
/**
* Requests and stores the search response.
*
* @public
*/
const fetchAndSaveSearchResponseWire = wireDispatch('fetchAndSaveSearchResponse');
/**
* Resets the search state `spellcheckedQuery` to its initial value, an empty string.
*
* @public
*/
const resetSpellcheckQuery = wireCommit('setSpellcheck', '');
/**
* Sets the search state `relatedTags`.
*
* @public
*/
const setRelatedTags = wireCommit('setRelatedTags');
/**
* Sets the search state `query`.
*
* @public
*/
const setSearchQuery = wireCommit('setQuery');
/**
* Clears the search state `query`.
*
* @public
*/
const clearSearchQuery = wireCommit('setQuery', '');
/**
* Sets the search state `selectedFilters`.
*
* @public
*/
const setSelectedFilters = wireCommit('setSelectedFilters');
/**
* Sets the search state `sort`.
*
* @public
*/
const setSort = wireCommit('setSort');
/**
* Sets the search state `query`.
*
* @public
*/
const setUrlParams = wireDispatch('setUrlParams');
/**
* Sets the search state `page`.
*
* @public
*/
const setSearchPage = wireCommit('setPage');
/**
* Sets the search state `params`.
*
* @public
*/
const setSearchExtraParams = wireCommit('setParams');
/**
* Resets the search state to reload the current search.
*
* @public
*/
const resetStateForReloadWire = wireCommitWithoutPayload('resetStateForReload');
/**
* Resets the search state `isNoResults`.
*
* @public
*/
const resetIsNoResults = wireCommit('setIsNoResults', false);
/**
* Resets the search state `fromNoResultsWithFilters`.
*
* @public
*/
const resetFromNoResultsWithFilters = wireCommit('setFromNoResultsWithFilters', false);
/**
* Increases the current search state `page` by one.
*
* @public
*/
const increasePageAppendingResultsWire = wireDispatchWithoutPayload('increasePageAppendingResults');
/**
* Resets the search state `isAppendingResults`.
*
* @public
*/
const resetAppending = wireCommit('setIsAppendResults', false);
/**
* Resets the {@link SearchGetters.request} parameters when refining request and before the actual
* request is launched.
*
* @public
*/
const resetRequestOnRefinementWire = wireDispatch('resetRequestOnRefinement', ({ eventPayload: newRequest, metadata: { oldValue } }) => ({
newRequest,
oldRequest: oldValue,
}));
/**
* Resets the search state when the request is changed to null. See the
* {@link SearchXStoreModule} for details.
*
* @public
*/
const resetStateIfNoRequestWire = filterTruthyPayload(wireCommitWithoutPayload('resetState'));
/**
* Sets the search state `query` with the selectedQueryPreview's query.
*
* @public
*/
const setSearchQueryFromPreview = wireCommit('setQuery', ({ eventPayload: { query } }) => query);
/**
* Sets the search state `params` with the selectedQueryPreview's extraParams.
*
* @public
*/
const setSearchExtraParamsFromPreview = wireCommit('setParams', ({ eventPayload: { extraParams } }) => extraParams);
/**
* Sets the search state `selectedFilters` with the selectedQueryPreview's filters.
*
* @public
*/
const setSearchSelectedFiltersFromPreview = wireCommit('setSelectedFilters', ({ eventPayload: { filters } }) => (filters ? createRawFilters(filters) : []));
/**
* Sets the search state `selectedFilters` with a selectedHistoryQuery's filters.
*
* @public
*/
const setSearchSelectedFiltersFromHistoryQuery = wireCommit('setSelectedFilters', ({ eventPayload: { selectedFilters } }) => selectedFilters ?? []);
/**
* Search wiring.
*
* @internal
*/
const searchWiring = createWiring({
ParamsLoadedFromUrl: {
setUrlParams,
saveOriginWire,
},
UserAcceptedAQuery: {
setSearchQuery,
saveOriginWire,
},
UserAcceptedSpellcheckQuery: {
resetSpellcheckQuery,
},
UserClearedQuery: {
setSearchQuery,
cancelFetchAndSaveSearchResponseWire,
resetFromNoResultsWithFilters,
resetIsNoResults,
},
UserClickedASort: {
setSort,
},
UserPickedARelatedTag: {
saveOriginWire,
},
UserReachedResultsListEnd: {
increasePageAppendingResultsWire,
},
SearchRequestUpdated: {
resetStateIfNoRequestWire,
fetchAndSaveSearchResponseWire,
},
SearchRequestChanged: {
resetRequestOnRefinementWire,
},
SelectedRelatedTagsChanged: {
setRelatedTags,
},
SelectedFiltersForRequestChanged: {
setSelectedFilters,
},
ResultsChanged: {
resetAppending,
},
ReloadSearchRequested: {
resetStateForReloadWire,
},
SelectedSortProvided: {
setSort,
},
ExtraParamsChanged: {
setSearchExtraParams,
},
UserClickedCloseX: {
clearSearchQuery,
},
UserClickedOutOfMainModal: {
clearSearchQuery,
},
UserAcceptedAQueryPreview: {
setSearchQueryFromPreview,
setSearchExtraParamsFromPreview,
setSearchSelectedFiltersFromPreview,
saveOriginWire,
},
QueryPreviewUnselected: {
setSearchExtraParams,
},
UserSelectedAHistoryQuery: {
setSearchSelectedFiltersFromHistoryQuery,
},
UserSelectedAPage: {
setSearchPage,
resetAppending,
},
});
export { cancelFetchAndSaveSearchResponseWire, clearSearchQuery, fetchAndSaveSearchResponseWire, increasePageAppendingResultsWire, resetAppending, resetFromNoResultsWithFilters, resetIsNoResults, resetRequestOnRefinementWire, resetSpellcheckQuery, resetStateForReloadWire, resetStateIfNoRequestWire, saveOriginWire, searchWiring, setRelatedTags, setSearchExtraParams, setSearchExtraParamsFromPreview, setSearchPage, setSearchQuery, setSearchQueryFromPreview, setSearchSelectedFiltersFromHistoryQuery, setSearchSelectedFiltersFromPreview, setSelectedFilters, setSort, setUrlParams };
//# sourceMappingURL=wiring.js.map