improved toQueryParams version, fixed infinite reload on Incidents due to using 'replace' instead of 'partial'

This commit is contained in:
Rares Mardare 2022-11-22 14:37:18 +02:00
parent 47c80eb3f5
commit 4d1da2ecd2
2 changed files with 15 additions and 11 deletions

View file

@ -150,7 +150,7 @@ class Incidents extends React.Component<IncidentsPageProps, IncidentsPageState>
fetchIncidentData = (filters: IncidentsFiltersType, isOnMount: boolean) => {
const { store } = this.props;
store.alertGroupStore.updateIncidentFilters(filters, isOnMount); // this line fetches incidents
LocationHelper.update({ page: 'incidents', ...store.alertGroupStore.incidentFilters }, 'replace');
LocationHelper.update({ page: 'incidents', ...store.alertGroupStore.incidentFilters }, 'partial');
};
onChangeCursor = (cursor: string, direction: 'prev' | 'next') => {
@ -579,7 +579,9 @@ class Incidents extends React.Component<IncidentsPageProps, IncidentsPageState>
}
setPollingInterval(filters: IncidentsFiltersType = this.state.filters, isOnMount = false) {
this.pollingIntervalId = setInterval(() => this.fetchIncidentData(filters, isOnMount), POLLING_NUM_SECONDS * 1000);
this.pollingIntervalId = setInterval(() => {
this.fetchIncidentData(filters, isOnMount);
}, POLLING_NUM_SECONDS * 1000);
}
}

View file

@ -10,23 +10,25 @@ class LocationHelper {
const sortedExistingParams = sort(queryParams);
const sortedNewParams = sort(params);
if (getPathFromQueryParams(sortedExistingParams) !== getPathFromQueryParams(sortedNewParams)) {
if (toQueryString(sortedExistingParams) !== toQueryString(sortedNewParams)) {
if (method === 'partial') {
locationService.partial(params);
} else {
locationService[method](getPathFromQueryParams(sortedNewParams));
locationService[method](toQueryString(sortedNewParams));
}
}
}
}
function getPathFromQueryParams(queryParams) {
return Object.keys(queryParams)
.map((key) => `${key}=${queryParams[key]}`)
.reduce((result, param, index) => {
const delimitator = `${index > 0 ? '&' : ''}`;
return `${result}${delimitator}${param}`;
}, '?');
function toQueryString(queryParams: KeyValue) {
const urlParams = new URLSearchParams(queryParams);
for (const [key, value] of Object.entries(queryParams)) {
if (Array.isArray(value)) {
urlParams.delete(key);
value.forEach((v) => urlParams.append(key, v));
}
}
return urlParams.toString();
}
function sort(object: KeyValue) {