Skip to content

Commit

Permalink
feat: add the capability to reload the current search
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-cid committed Jun 17, 2024
1 parent 2d93900 commit ab634d0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
12 changes: 8 additions & 4 deletions packages/x-components/src/x-modules/search/events.types.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -26,6 +26,10 @@ export interface SearchXEvents {
* Payload: The new page number.
*/
PageChanged: number;
/**
* Reload the current search has been requested.
*/
ReloadSearchRequested: void;
/**
* Results have been changed.
* Payload: The new {@link @empathyco/x-types#Result | results}.
Expand Down
18 changes: 11 additions & 7 deletions packages/x-components/src/x-modules/search/store/module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -46,6 +46,10 @@ export const searchXStoreModule: SearchXStoreModule = {
resetState(state) {
Object.assign(state, resettableState());
},
resetStateForReload(state) {
const { query, facets, sort, page, ...resettable } = resettableState();
Object.assign(state, resettable);
},
setQuery,
setResults(state, results) {
state.results = results;
Expand Down
17 changes: 10 additions & 7 deletions packages/x-components/src/x-modules/search/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*
Expand Down
23 changes: 17 additions & 6 deletions packages/x-components/src/x-modules/search/wiring.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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`.
*
Expand Down Expand Up @@ -272,6 +280,9 @@ export const searchWiring = createWiring({
ResultsChanged: {
resetAppending
},
ReloadSearchRequested: {
resetStateForReloadWire
},
SelectedSortProvided: {
setSort
},
Expand Down
19 changes: 19 additions & 0 deletions packages/x-components/tests/e2e/reload-search.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: Reload search

Background:
Given a results API with 24 results
And a tracking API with a known response
And no special config for layout view

Scenario Outline: 1. Search is reloaded when event is emitted
When start button is clicked
Then empathize should be visible
When "<query>" is searched
Then results page number 1 is loaded
And search request contains parameter "query" with value "<query>"
When event ReloadSearchRequested is emitted
Then search request contains parameter "query" with value "<query>"

Examples:
| query |
| lego |
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { When } from '@badeball/cypress-cucumber-preprocessor';

When('event ReloadSearchRequested is emitted', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
cy.window().then((w: Window) => w.InterfaceX?.bus.emit('ReloadSearchRequested'));
});

0 comments on commit ab634d0

Please sign in to comment.