Skip to content

Commit

Permalink
chore(dashboard): should use locale-specific display for filter categ…
Browse files Browse the repository at this point in the history
…ories
  • Loading branch information
Thuan Vo committed Aug 15, 2023
1 parent 5cbdc8a commit 68e2c56
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"DOWNLOAD": "Download",
"EDIT": "Edit",
"FILTER_NAME": "Name",
"FILTER_TOPIC": "Topic",
"HELP": "Help",
"HOUR": "Hour",
"HOUR_one": "Hour",
Expand Down
64 changes: 43 additions & 21 deletions src/app/Dashboard/AutomatedAnalysis/AutomatedAnalysisFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '@patternfly/react-core';
import { FilterIcon } from '@patternfly/react-icons';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { AutomatedAnalysisNameFilter } from './Filters/AutomatedAnalysisNameFilter';
import { AutomatedAnalysisTopicFilter } from './Filters/AutomatedAnalysisTopicFilter';
Expand All @@ -50,12 +51,19 @@ export interface AutomatedAnalysisFiltersProps {
updateFilters: (target: string, updateFilterOptions: UpdateFilterOptions) => void;
}

export const AutomatedAnalysisFilters: React.FC<AutomatedAnalysisFiltersProps> = ({ updateFilters, ...props }) => {
export const AutomatedAnalysisFilters: React.FC<AutomatedAnalysisFiltersProps> = ({
updateFilters,
target,
evaluations,
filters,
...props

Check warning on line 59 in src/app/Dashboard/AutomatedAnalysis/AutomatedAnalysisFilters.tsx

View workflow job for this annotation

GitHub Actions / eslint-check (18.x)

'props' is defined but never used. Allowed unused args must match /^_/u
}) => {
const dispatch = useDispatch<StateDispatch>();
const { t } = useTranslation();

const currentCategory = useSelector((state: RootState) => {
const targetAutomatedAnalysisFilters = state.automatedAnalysisFilters.targetFilters.filter(
(targetFilter) => targetFilter.target === props.target
(targetFilter) => targetFilter.target === target
);
if (!targetAutomatedAnalysisFilters.length) {
return 'Name';
Expand All @@ -72,33 +80,47 @@ export const AutomatedAnalysisFilters: React.FC<AutomatedAnalysisFiltersProps> =
const onCategorySelect = React.useCallback(
(category) => {
setIsCategoryDropdownOpen(false);
dispatch(automatedAnalysisUpdateCategoryIntent(props.target, category));
dispatch(automatedAnalysisUpdateCategoryIntent(target, category));
},
[dispatch, setIsCategoryDropdownOpen, props.target]
[dispatch, setIsCategoryDropdownOpen, target]
);

const onDelete = React.useCallback(
(category: string | ToolbarChipGroup, value) =>
updateFilters(props.target, { filterKey: category as string, filterValue: value, deleted: true }),
[updateFilters, props.target]
updateFilters(target, { filterKey: category as string, filterValue: value, deleted: true }),
[updateFilters, target]
);

const onDeleteGroup = React.useCallback(
(category: string | ToolbarChipGroup) =>
updateFilters(props.target, { filterKey: category as string, deleted: true, deleteOptions: { all: true } }),
[updateFilters, props.target]
updateFilters(target, { filterKey: category as string, deleted: true, deleteOptions: { all: true } }),
[updateFilters, target]
);

const onNameInput = React.useCallback(
(inputName: string) => updateFilters(props.target, { filterKey: currentCategory, filterValue: inputName }),
[updateFilters, currentCategory, props.target]
(inputName: string) => updateFilters(target, { filterKey: currentCategory, filterValue: inputName }),
[updateFilters, currentCategory, target]
);

const onTopicInput = React.useCallback(
(inputTopic: string) => {
updateFilters(props.target, { filterKey: currentCategory, filterValue: inputTopic });
updateFilters(target, { filterKey: currentCategory, filterValue: inputTopic });
},
[updateFilters, currentCategory, props.target]
[updateFilters, currentCategory, target]
);

const getCategoryDisplay = React.useCallback(
(category: string) => {
switch (category) {
case 'Name':
return t('FILTER_NAME', { ns: 'common' });
case 'Topic':
return t('FILTER_TOPIC', { ns: 'common' });
default:
throw new Error(`Unknown Automated Analysis Filter Category: ${category}`);
}
},
[t]
);

const categoryDropdown = React.useMemo(() => {
Expand All @@ -108,7 +130,7 @@ export const AutomatedAnalysisFilters: React.FC<AutomatedAnalysisFiltersProps> =
position={DropdownPosition.left}
toggle={
<DropdownToggle aria-label={currentCategory} onToggle={onCategoryToggle}>
<FilterIcon /> {currentCategory}
<FilterIcon /> {getCategoryDisplay(currentCategory)}
</DropdownToggle>
}
isOpen={isCategoryDropdownOpen}
Expand All @@ -125,31 +147,31 @@ export const AutomatedAnalysisFilters: React.FC<AutomatedAnalysisFiltersProps> =
() => [
<AutomatedAnalysisNameFilter
key={'name'}
evaluations={props.evaluations}
filteredNames={props.filters.Name}
evaluations={evaluations}
filteredNames={filters.Name}
onSubmit={onNameInput}
/>,
<AutomatedAnalysisTopicFilter
key={'topic'}
evaluations={props.evaluations}
filteredTopics={props.filters.Topic}
evaluations={evaluations}
filteredTopics={filters.Topic}
onSubmit={onTopicInput}
></AutomatedAnalysisTopicFilter>,
/>,
],
[props.evaluations, props.filters.Name, props.filters.Topic, onNameInput, onTopicInput]
[evaluations, filters.Name, filters.Topic, onNameInput, onTopicInput]
);

return (
<ToolbarToggleGroup toggleIcon={<FilterIcon />} breakpoint="xl">
<ToolbarGroup variant="filter-group">
<ToolbarItem>
{categoryDropdown}
{Object.keys(props.filters)
{Object.keys(filters)
.filter((f) => f !== 'Score')
.map((filterKey, i) => (
<ToolbarFilter
key={filterKey}
chips={props.filters[filterKey]}
chips={filters[filterKey]}
deleteChip={onDelete}
deleteChipGroup={onDeleteGroup}
categoryName={filterKey}
Expand Down

0 comments on commit 68e2c56

Please sign in to comment.