Skip to content

Commit

Permalink
handle edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Mar 2, 2023
1 parent c285698 commit 8fa86ac
Showing 1 changed file with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,33 @@ import React from 'react';
import { EuiFilterGroup } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useSelector } from 'react-redux';
import { useGetUrlParams } from '../../../../hooks';
import { selectServiceLocationsState } from '../../../../state';

import {
SyntheticsMonitorFilterItem,
getSyntheticsFilterDisplayValues,
SyntheticsMonitorFilterChangeHandler,
LabelWithCountValue,
} from './filter_fields';
import { useFilters } from './use_filters';
import { FilterButton } from './filter_button';

const mixUrlValues = (
values?: LabelWithCountValue[],
urlLabels?: string[]
): LabelWithCountValue[] => {
const urlValues = urlLabels?.map((label) => ({ label, count: 0 })) ?? [];
const newValues = [...(values ?? [])];
// add url values that are not in the values
urlValues.forEach((urlValue) => {
if (!newValues.find((value) => value.label === urlValue.label)) {
newValues.push(urlValue);
}
});
return newValues;
};

export const FilterGroup = ({
handleFilterChange,
}: {
Expand All @@ -28,34 +45,56 @@ export const FilterGroup = ({

const { locations } = useSelector(selectServiceLocationsState);

const urlParams = useGetUrlParams();

const filters: SyntheticsMonitorFilterItem[] = [
{
label: TYPE_LABEL,
field: 'monitorTypes',
values: getSyntheticsFilterDisplayValues(data.monitorTypes, 'monitorTypes', locations),
values: getSyntheticsFilterDisplayValues(
mixUrlValues(data.monitorTypes, urlParams.monitorTypes),
'monitorTypes',
locations
),
},
{
label: LOCATION_LABEL,
field: 'locations',
values: getSyntheticsFilterDisplayValues(data.locations, 'locations', locations),
values: getSyntheticsFilterDisplayValues(
mixUrlValues(data.locations, urlParams.locations),
'locations',
locations
),
},
{
label: TAGS_LABEL,
field: 'tags',
values: getSyntheticsFilterDisplayValues(data.tags, 'tags', locations),
values: getSyntheticsFilterDisplayValues(
mixUrlValues(data.tags, urlParams.tags),
'tags',
locations
),
},
{
label: SCHEDULE_LABEL,
field: 'schedules',
values: getSyntheticsFilterDisplayValues(data.schedules, 'schedules', locations),
values: getSyntheticsFilterDisplayValues(
mixUrlValues(data.schedules, urlParams.schedules),
'schedules',
locations
),
},
];

if (data.projects.length > 0) {
filters.push({
label: PROJECT_LABEL,
field: 'projects',
values: getSyntheticsFilterDisplayValues(data.projects, 'projects', locations),
values: getSyntheticsFilterDisplayValues(
mixUrlValues(data.projects, urlParams.projects),
'projects',
locations
),
});
}

Expand Down

0 comments on commit 8fa86ac

Please sign in to comment.