-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the capability to reload the current search
- Loading branch information
1 parent
18dc522
commit 1604e0b
Showing
7 changed files
with
232 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/x-components/src/x-modules/search/store/actions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/x-components/src/x-modules/search/store/actions/reload-search.action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { SearchRequest, SearchResponse } from '@empathyco/x-types'; | ||
import { createFetchAndSaveActions } from '../../../../store'; | ||
import { InternalSearchRequest } from '../../types'; | ||
import { SearchActionContext, SearchState } from '../types'; | ||
|
||
const { fetchAndSave, cancelPrevious } = createFetchAndSaveActions< | ||
SearchActionContext, | ||
InternalSearchRequest | null, | ||
SearchResponse | null | ||
>({ | ||
fetch({ dispatch, state }, request) { | ||
return request | ||
? dispatch('fetchSearchResponse', enrichRequest(request, state)) | ||
: Promise.resolve(null); | ||
}, | ||
onSuccess({ dispatch }, response) { | ||
if (response !== null) { | ||
dispatch('saveSearchResponse', response); | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Enriches the {@link SearchRequest} object with the origin and page properties taken from the | ||
* {@link SearchState | search state}. | ||
* | ||
* @param request - The {@link InternalSearchRequest}. | ||
* @param state - {@link SearchState}. | ||
* | ||
* @returns The search request. | ||
* @internal | ||
*/ | ||
function enrichRequest(request: InternalSearchRequest, state: SearchState): SearchRequest { | ||
const { page, ...restRequest } = request; | ||
const { | ||
config: { pageSize }, | ||
origin | ||
} = state; | ||
|
||
return { | ||
...restRequest, | ||
...(origin && { origin }), | ||
start: 0, | ||
rows: pageSize | ||
}; | ||
} | ||
|
||
/** | ||
* Default implementation for {@link SearchActions.fetchAndSaveSearchResponse} action. | ||
* | ||
* @param context - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the | ||
* actions, provided by Vuex. | ||
* @returns A promise that resolves after saving the response. | ||
* @public | ||
*/ | ||
export const reloadSearch = (context: SearchActionContext) => | ||
fetchAndSave(context, context.getters.request); | ||
|
||
/** | ||
* Default implementation for {@link SearchActions.cancelFetchAndSaveSearchResponse} action. | ||
* | ||
* @public | ||
*/ | ||
export const cancelReloadSearch = cancelPrevious; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters