apisearch-ui
Version:
Javascript User Interface of Apisearch.
68 lines (61 loc) • 1.64 kB
text/typescript
/**
* SortBy actions
*/
import {Query, Repository} from "apisearch";
import {APISEARCH_DISPATCHER} from "../../Constants";
import container from "../../Container";
import Clone from "../Clone";
import {applySortByToQuery} from "./SortByHelper";
/**
* Initial sortBy
*
* @param environmentId
* @param currentQuery
* @param initialOption
*/
export function initialSortBySetup(
environmentId: string,
currentQuery: Query,
initialOption: string
) {
const dispatcher = container.get(`${APISEARCH_DISPATCHER}__${environmentId}`);
const clonedQuery = Clone.object(currentQuery);
applySortByToQuery(clonedQuery, initialOption);
clonedQuery.page = 1;
dispatcher.dispatch("UPDATE_APISEARCH_SETUP", {
query: clonedQuery,
});
}
/**
* ON change search action
*
* @param environmentId
* @param currentQuery
* @param repository
* @param selectedOption
*/
export function onChangeSearchAction(
environmentId: string,
currentQuery: Query,
repository: Repository,
selectedOption: string,
) {
window.postMessage({
name: "apisearch_scroll_top",
}, "*");
const clonedQuery = Clone.object(currentQuery);
applySortByToQuery(clonedQuery, selectedOption);
clonedQuery.page = 1;
const dispatcher = container.get(`${APISEARCH_DISPATCHER}__${environmentId}`);
repository
.query(clonedQuery)
.then((result) => {
dispatcher.dispatch("RENDER_FETCHED_DATA", {
query: clonedQuery,
result: result,
});
})
.catch((error) => {
// Do nothing
});
}