@resourge/react-fetch
Version:
[](LICENSE)
176 lines (173 loc) • 4.75 kB
JavaScript
/**
* react-fetch v1.41.3
*
* Copyright (c) resourge.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
import { useMemo, useEffect } from 'react';
import { HistoryStore } from '@resourge/history-store';
import { parseParams, createNewUrlWithSearch, parseSearchParams } from '@resourge/history-store/utils';
import { deepCompare } from '../utils/comparationUtils';
import { useMultipleFiltersId, removeCacheIds, filterByCacheIds } from './useMultipleFiltersId';
const useFilterSearchParams = ({
defaultFilter,
initialPage,
initialPerPage,
defaultSort,
fetch,
preloadRef,
hash,
deps,
filterKeysRef,
fId: _fId
}) => {
const fId = _fId != null ? _fId : useMultipleFiltersId({
hash
});
function getParams(url) {
let searchParams = url.searchParams;
if (hash) {
const hashUrl = new URL(url.hash.slice(1), window.location.origin);
searchParams = hashUrl.searchParams;
}
return parseSearchParams(searchParams, {
...defaultFilter,
page: initialPage,
perPage: initialPerPage,
sort: defaultSort
});
}
function getDataFromParams() {
var _params$fId, _removeCacheIds;
const [url] = HistoryStore.getValue();
const params = getParams(url);
const {
sort,
page,
perPage,
...filter
} = fId ? (_params$fId = params[fId]) != null ? _params$fId : {} : params;
return {
filter: (_removeCacheIds = removeCacheIds(filter)) != null ? _removeCacheIds : defaultFilter,
sort: sort != null ? sort : defaultSort,
pagination: {
page: page != null ? page : initialPage,
perPage: perPage != null ? perPage : initialPerPage,
totalItems: 0,
totalPages: 0
},
url
};
}
const data = useMemo(getDataFromParams, deps);
const setParams = newFilter => {
const [url] = HistoryStore.getValue();
const params = filterByCacheIds(fId, getParams(url));
const newSearch = parseParams({
...params,
...(fId ? {
[fId]: newFilter
} : newFilter)
});
if (url.search !== newSearch) {
const newURL = createNewUrlWithSearch(url, newSearch, hash);
HistoryStore.navigate(newURL, {
replace: true
});
}
};
const setFilter = newFilter => {
setParams({
...data.filter,
sort: data.sort,
page: data.pagination.page,
perPage: data.pagination.perPage,
...newFilter
});
};
const sortTable = (orderBy, orderColumn) => {
if (Array.isArray(orderBy)) {
setFilter({
page: initialPage,
sort: orderBy
});
return;
}
const sort = data.sort ? [...data.sort] : [];
const index = sort.findIndex(val => val.orderColumn === orderColumn);
if (index > -1) {
sort[index] = {
orderBy,
orderColumn
};
} else {
sort.push({
orderBy,
orderColumn
});
}
setFilter({
page: initialPage,
sort
});
};
useEffect(() => {
return HistoryStore.subscribe(() => {
const {
filter,
pagination: {
page,
perPage
},
sort,
url: subscribeURL
} = getDataFromParams();
const whatChanged = new Set();
if (perPage !== undefined && data.pagination.perPage !== perPage) {
preloadRef.current = {};
whatChanged.add('pagination');
data.pagination.perPage = perPage;
}
if (page !== undefined && data.pagination.page !== page) {
whatChanged.add('pagination');
data.pagination.page = page;
}
if (!deepCompare(sort, data.sort)) {
whatChanged.add('sort');
preloadRef.current = {};
data.sort = sort;
}
if (!deepCompare(filter, data.filter, filterKeysRef.current.state)) {
whatChanged.add('filter');
preloadRef.current = {};
data.filter = filter;
}
if (whatChanged.size) {
if (hash) {
const newRenderURL = new URL(data.url.hash.slice(1), window.location.origin);
const newSubscribeURL = new URL(subscribeURL.hash.slice(1), window.location.origin);
if (newRenderURL.hash !== newSubscribeURL.hash) {
return;
}
} else if (data.url.pathname !== subscribeURL.pathname) {
return;
}
fetch(data, whatChanged);
}
});
}, [data]);
return {
pagination: data.pagination,
filter: data.filter,
sort: data.sort,
setParams,
setFilter,
sortTable: sortTable
};
};
export { useFilterSearchParams };
//# sourceMappingURL=useFilterSearchParams.js.map