diff --git a/packages/x-components/src/x-modules/search/events.types.ts b/packages/x-components/src/x-modules/search/events.types.ts index 267884e35d..28d1a9ccdd 100644 --- a/packages/x-components/src/x-modules/search/events.types.ts +++ b/packages/x-components/src/x-modules/search/events.types.ts @@ -1,11 +1,11 @@ import { + Banner, Facet, + Promoted, + Redirection, Result, Sort, - Redirection, - TaggingRequest, - Promoted, - Banner + TaggingRequest } from '@empathyco/x-types'; import { InternalSearchRequest, InternalSearchResponse } from './types'; @@ -26,6 +26,10 @@ export interface SearchXEvents { * Payload: The new page number. */ PageChanged: number; + /** + * Reload the current search has been requested. + */ + ReloadSearchRequested: undefined; /** * Results have been changed. * Payload: The new {@link @empathyco/x-types#Result | results}. diff --git a/packages/x-components/src/x-modules/search/store/module.ts b/packages/x-components/src/x-modules/search/store/module.ts index 2da7948952..9c23e3d3b7 100644 --- a/packages/x-components/src/x-modules/search/store/module.ts +++ b/packages/x-components/src/x-modules/search/store/module.ts @@ -1,18 +1,18 @@ import { isFacetFilter } from '@empathyco/x-types'; import { setQuery } from '../../../store/utils/query.utils'; -import { setStatus } from '../../../store/utils/status-store.utils'; +import { setStatus } from '../../../store'; import { groupItemsBy } from '../../../utils/array'; import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils'; import { UNKNOWN_FACET_KEY } from '../../facets/store/constants'; import { cancelFetchAndSaveSearchResponse, - fetchAndSaveSearchResponse -} from './actions/fetch-and-save-search-response.action'; -import { fetchSearchResponse } from './actions/fetch-search-response.action'; -import { increasePageAppendingResults } from './actions/increase-page-apending-results.action'; -import { resetRequestOnRefinement } from './actions/reset-request-on-refinement.action'; + fetchAndSaveSearchResponse, + fetchSearchResponse, + increasePageAppendingResults, + resetRequestOnRefinement, + saveSearchResponse +} from './actions'; import { saveOrigin } from './actions/save-origin.action'; -import { saveSearchResponse } from './actions/save-search-response.action'; import { setUrlParams } from './actions/set-url-params.action'; import { query } from './getters/query.getter'; import { request } from './getters/request.getter'; @@ -46,6 +46,14 @@ export const searchXStoreModule: SearchXStoreModule = { resetState(state) { Object.assign(state, resettableState()); }, + resetStateForReload(state) { + const resettable = resettableState(); + resettable.query = state.query; + resettable.facets = state.facets as never[]; + resettable.sort = state.sort; + resettable.page = state.page; + Object.assign(state, resettable); + }, setQuery, setResults(state, results) { state.results = results; diff --git a/packages/x-components/src/x-modules/search/store/types.ts b/packages/x-components/src/x-modules/search/store/types.ts index 848532f4da..08c457936a 100644 --- a/packages/x-components/src/x-modules/search/store/types.ts +++ b/packages/x-components/src/x-modules/search/store/types.ts @@ -7,17 +7,15 @@ import { Redirection, RelatedTag, Result, - Sort, - TaggingRequest, SearchRequest, - SearchResponse + SearchResponse, + Sort, + TaggingRequest } from '@empathyco/x-types'; import { Dictionary } from '@empathyco/x-utils'; -import { XActionContext, XStoreModule } from '../../../store'; +import { StatusMutations, StatusState, XActionContext, XStoreModule } from '../../../store'; import { QueryMutations, QueryState } from '../../../store/utils/query.utils'; -import { StatusMutations, StatusState } from '../../../store/utils/status-store.utils'; -import { QueryOrigin, QueryOriginInit } from '../../../types/origin'; -import { UrlParams } from '../../../types/url-params'; +import { QueryOrigin, QueryOriginInit, UrlParams } from '../../../types'; import { SearchConfig } from '../config.types'; import { InternalSearchRequest, WatchedInternalSearchRequest } from '../types'; import { ConfigMutations } from '../../../store/utils/config-store.utils'; @@ -108,6 +106,11 @@ export interface SearchMutations * {@link searchXStoreModule} for details. */ resetState(): void; + /** + * Resets the "resettable" part of the Search state like {@link SearchMutations.resetState} but + * maintains the values required to perform the search request again. + */ + resetStateForReload(): void; /** * Sets the banners of the module. * diff --git a/packages/x-components/src/x-modules/search/wiring.ts b/packages/x-components/src/x-modules/search/wiring.ts index 7679bce076..8ed51ef822 100644 --- a/packages/x-components/src/x-modules/search/wiring.ts +++ b/packages/x-components/src/x-modules/search/wiring.ts @@ -1,12 +1,13 @@ -import { filterTruthyPayload, namespacedWireCommitWithoutPayload } from '../../wiring'; import { + createWiring, + filterTruthyPayload, namespacedWireCommit, + namespacedWireCommitWithoutPayload, namespacedWireDispatch, - namespacedWireDispatchWithoutPayload -} from '../../wiring/namespaced-wires.factory'; -import { WirePayload } from '../../wiring/wiring.types'; -import { createWiring } from '../../wiring/wiring.utils'; -import { createRawFilters } from '../../utils/filters'; + namespacedWireDispatchWithoutPayload, + WirePayload +} from '../../wiring'; +import { createRawFilters } from '../../utils'; import { InternalSearchRequest } from './types'; /** @@ -130,6 +131,13 @@ export const setSearchPage = wireCommit('setPage'); */ export const setSearchExtraParams = wireCommit('setParams'); +/** + * Resets the search state to reload the current search. + * + * @public + */ +export const resetStateForReloadWire = wireCommitWithoutPayload('resetStateForReload'); + /** * Resets the search state `isNoResults`. * @@ -272,6 +280,9 @@ export const searchWiring = createWiring({ ResultsChanged: { resetAppending }, + ReloadSearchRequested: { + resetStateForReloadWire + }, SelectedSortProvided: { setSort },