Skip to content

Commit

Permalink
implement PR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MGJamJam committed May 14, 2024
1 parent b970808 commit f877515
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Props = {
readonly onDelete: () => void;
readonly addFilterDisabled: boolean;
readonly onAddFilter: () => void;
readonly parsingValueError?: string;
readonly parsingValueError: string | undefined;
};

export function FilterInput(props: Props) {
Expand Down
71 changes: 38 additions & 33 deletions bitmovin-analytics-datasource/src/components/FilterRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import { QueryAttribute, SELECTABLE_QUERY_ATTRIBUTES } from '../types/queryAttri
import { FilterInput } from './FilterInput';
import { convertFilterValueToProperType } from '../utils/filterUtils';

type Props = {
readonly isAdAnalytics: boolean;
readonly onChange: (newFilters: QueryFilter[]) => void;
};

type Filter = {
selectedAttribute: SelectableValue<QueryAdAttribute | QueryAttribute>;
selectedOperator: SelectableValue<QueryFilterOperator>;
Expand All @@ -22,27 +17,35 @@ type Filter = {
parsingValueError: string;
};

export function FilterRow(props: Props) {
const [filters, setFilters] = useState<Filter[]>([]);
const mapFilterAttributesToSelectableValue = (
filters: Filter[],
isAdAnalytics: boolean
): Array<SelectableValue<QueryAttribute | QueryAdAttribute>> => {
const selectedAttributes = filters.map((filter) => filter.selectedAttribute);
if (isAdAnalytics) {
return difference(SELECTABLE_QUERY_AD_ATTRIBUTES, selectedAttributes);
} else {
return difference(SELECTABLE_QUERY_ATTRIBUTES, selectedAttributes);
}
};

const mapFilterAttributesToSelectableValue = (): Array<SelectableValue<QueryAttribute | QueryAdAttribute>> => {
const selectedAttributes = filters.map((filter) => filter.selectedAttribute);
if (props.isAdAnalytics) {
return difference(SELECTABLE_QUERY_AD_ATTRIBUTES, selectedAttributes);
} else {
return difference(SELECTABLE_QUERY_ATTRIBUTES, selectedAttributes);
}
};
const mapFiltersToQueryFilters = (filters: Filter[]): QueryFilter[] => {
return filters.map((filter) => {
return {
name: filter.selectedAttribute.value!,
operator: filter.selectedOperator.value!,
value: filter.convertedFilterValue,
} as QueryFilter;
});
};

const mapFiltersToQueryFilters = (filters: Filter[]): QueryFilter[] => {
return filters.map((filter) => {
return {
name: filter.selectedAttribute.value!,
operator: filter.selectedOperator.value!,
value: filter.convertedFilterValue,
} as QueryFilter;
});
};
type Props = {
readonly isAdAnalytics: boolean;
readonly onChange: (newFilters: QueryFilter[]) => void;
};

export function FilterRow(props: Props) {
const [filters, setFilters] = useState<Filter[]>([]);

const addFilterInput = () => {
setFilters((prevState) => [
Expand Down Expand Up @@ -76,14 +79,16 @@ export function FilterRow(props: Props) {
setFilters(newFilters);

props.onChange(mapFiltersToQueryFilters(newFilters));
} catch (e: any) {
const errorMessage = e.message;
const newFilter = { ...filter, parsingValueError: errorMessage } as Filter;
} catch (e: unknown) {
if (e instanceof Error) {
const errorMessage = e.message;
const newFilter = { ...filter, parsingValueError: errorMessage } as Filter;

const newFilters = [...filters];
newFilters.splice(index, 1, newFilter);
const newFilters = [...filters];
newFilters.splice(index, 1, newFilter);

setFilters(newFilters);
setFilters(newFilters);
}
}
};

Expand Down Expand Up @@ -138,11 +143,11 @@ export function FilterRow(props: Props) {
</InlineLabel>
</HorizontalGroup>
)}
{filters.map((filter, index) => (
{filters.map((filter, index, filtersArray) => (
<FilterInput
key={index}
isAdAnalytics={props.isAdAnalytics}
selectableFilterAttributes={mapFilterAttributesToSelectableValue()}
selectableFilterAttributes={mapFilterAttributesToSelectableValue(filtersArray, props.isAdAnalytics)}
onAttributeChange={(newValue: SelectableValue<QueryAdAttribute | QueryAttribute>) =>
onAttributesChange(index, newValue)
}
Expand All @@ -151,7 +156,7 @@ export function FilterRow(props: Props) {
onDelete={() => deleteFilterInput(index)}
addFilterDisabled={isEmpty(filter.selectedAttribute) || isEmpty(filter.selectedOperator)}
onAddFilter={() => onAddFilter(index)}
parsingValueError={filter.parsingValueError === '' ? undefined : filter.parsingValueError}
parsingValueError={isEmpty(filter.parsingValueError) ? undefined : filter.parsingValueError}
/>
))}

Expand Down
16 changes: 14 additions & 2 deletions bitmovin-analytics-datasource/src/utils/filterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { isEmpty } from 'lodash';

import { QUERY_AD_ATTRIBUTES, QueryAdAttribute } from '../types/queryAdAttributes';
import { QUERY_FILTER_OPERATORS, QueryFilterOperator, QueryFilterValue } from '../types/queryFilter';
import { QUERY_ATTRIBUTES, QueryAttribute } from '../types/queryAttributes';

export const isNullFilter = (filterAttribute: QueryAttribute | QueryAdAttribute): boolean => {
const isNullFilter = (filterAttribute: QueryAttribute | QueryAdAttribute): boolean => {
switch (filterAttribute) {
case QUERY_ATTRIBUTES.CDN_PROVIDER:
case QUERY_ATTRIBUTES.CUSTOM_DATA_1:
Expand Down Expand Up @@ -158,14 +160,24 @@ const convertFilter = (rawValue: string, filterAttribute: QueryAttribute, filter
}
};

/**
* Transforms the string filter Value from the UI to the appropriate type for our API.
*
* @param {string} rawValue The raw string value from the Filter Input.
* @param {QueryAttribute | QueryAdAttribute} filterAttribute The filter attribute.
* @param {string} filterAttributeLabel The filter attribute label.
* @param {QueryFilterOperator} filterOperator The filter operator.
* @param {boolean} isAdAnalytics If Ad Analytics are queried.
* @returns {QueryFilterValue} The correctly converted Filter Value.
* */
export const convertFilterValueToProperType = (
rawValue: string,
filterAttribute: QueryAttribute | QueryAdAttribute,
filterAttributeLabel: string,
filterOperator: QueryFilterOperator,
isAdAnalytics: boolean
): QueryFilterValue => {
if (rawValue === '' && isNullFilter(filterAttribute)) {
if (isEmpty(rawValue) && isNullFilter(filterAttribute)) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"target": "es5",
"sourceMap": true,
"rootDir": "src",
"outDir": "dist"
"outDir": "dist",
"strict": true
},
"include": ["src/**/*"]
}

0 comments on commit f877515

Please sign in to comment.