Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Refactor code a bit

dominik-zeglend85d3f905f50
diff --git a/saleor/static/dashboard-next/components/Filter/FilterContent.tsx b/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
index eed7cf1c6a..a401e76512 100644
--- a/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
+++ b/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
@@ -21,11 +21,18 @@ export interface FilterContentProps {
function checkFilterValue(value: string | string[]): boolean {
if (typeof value === "string") {
- return value !== "";
+ return !!value;
}
return value.some(v => !!v);
}
+function getFilterChoices(items: IFilter) {
+ return items.map(filterItem => ({
+ label: filterItem.label,
+ value: filterItem.value
+ }));
+}
+
const useStyles = makeStyles({
input: {
padding: "20px 12px 17px"
@@ -41,9 +48,8 @@ const FilterContent: React.FC<FilterContentProps> = ({
const [filterValue, setFilterValue] = React.useState<string | string[]>("");
const classes = useStyles();
- const activeMenu = menuValue
- ? getMenuItemByValue(filters, menuValue)
- : undefined;
+ const activeMenu = getMenuItemByValue(filters, menuValue);
+
const menus = menuValue
? walkToRoot(filters, menuValue).slice(-1)
: undefined;
@@ -56,10 +62,7 @@ const FilterContent: React.FC<FilterContentProps> = ({
return (
<>
<SingleSelectField
- choices={filters.map(filterItem => ({
- label: filterItem.label,
- value: filterItem.value
- }))}
+ choices={getFilterChoices(filters)}
onChange={onMenuChange}
selectProps={{
classes: {
@@ -74,14 +77,11 @@ const FilterContent: React.FC<FilterContentProps> = ({
(filterItem, filterItemIndex) =>
!isLeaf(filterItem) && (
<React.Fragment
- key={filterItem.label.toString() + filterItem.value}
+ key={filterItem.label.toString() + ":" + filterItem.value}
>
<FormSpacer />
<SingleSelectField
- choices={filterItem.children.map(filterItem => ({
- label: filterItem.label,
- value: filterItem.value
- }))}
+ choices={getFilterChoices(filterItem.children)}
onChange={onMenuChange}
selectProps={{
classes: {
diff --git a/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx b/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
index 2886f29716..58260d0705 100644
--- a/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
+++ b/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
@@ -146,7 +146,7 @@ export const FilterChips: React.FC<FilterChipProps> = ({
onChange={onSearchChange}
/>
</div>
- {filtersList && filtersList.length > 0 ? (
+ {filtersList && filtersList.length ? (
<div className={classes.filterContainer}>
<div className={classes.filterChipContainer}>
{filtersList.map(filter => (
diff --git a/saleor/static/dashboard-next/utils/menu/menu.ts b/saleor/static/dashboard-next/utils/menu/menu.ts
index 4abae4363a..5484cdba43 100644
--- a/saleor/static/dashboard-next/utils/menu/menu.ts
+++ b/saleor/static/dashboard-next/utils/menu/menu.ts
@@ -45,6 +45,10 @@ export function getMenuItemByValue<TMenuData = {}>(
menu: IMenu<TMenuData>,
value: string
): IMenuItem<TMenuData> {
+ if (value === undefined) {
+ return undefined;
+ }
+
const flatMenu = toFlat(menu);
const flatMenuItem: IFlatMenuItem<TMenuData> = flatMenu.find(
menuItem => menuItem.value === value