-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "view products" link when deleting type (#5186)
* fix 'view products' link * fix 'view products' link * loading state and type fixes * fix type * fix tests * small fixes * remove unnecessary 'slug' from page type * remove usage of legacy filtering url * change test descriptions --------- Co-authored-by: Paweł Chyła <chyla1988@gmail.com>
- Loading branch information
Showing
16 changed files
with
203 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
When deleting product type with products the "View products" link now navigates to product list with correctly applied product type filter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FilterContainer } from "../FilterElement"; | ||
import { UrlEntry } from "./UrlToken"; | ||
|
||
type Structure = Array<string | UrlEntry | Structure>; | ||
|
||
export const prepareStructure = (filterValue: FilterContainer): Structure => | ||
filterValue.map(f => { | ||
if (typeof f === "string") { | ||
return f; | ||
} | ||
|
||
if (Array.isArray(f)) { | ||
return prepareStructure(f); | ||
} | ||
|
||
return f.asUrlEntry(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/components/TypeDeleteWarningDialog/useViewProducts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useViewProducts } from "./useViewProducts"; | ||
|
||
describe("useViewProducts", () => { | ||
const productTypeBaseData = [ | ||
{ | ||
id: "123", | ||
name: "Audiobooks", | ||
slug: "audiobooks", | ||
}, | ||
]; | ||
|
||
it("should return URL with product type filtered", () => { | ||
// Arrange & Act | ||
const { result } = renderHook(() => useViewProducts({ productTypeBaseData })); | ||
|
||
// Assert | ||
expect(result.current).not.toBeNull(); | ||
|
||
const expectedQuery = "0[s0.productType][0]=audiobooks"; | ||
const receivedQuery = decodeURIComponent(result.current!.split("?")[1]); | ||
|
||
expect(receivedQuery).toBe(expectedQuery); | ||
}); | ||
|
||
it("should return URL with product type filtered when multiple product types are selected", () => { | ||
// Arrange | ||
const multipleProductTypeBaseData = [ | ||
...productTypeBaseData, | ||
{ | ||
id: "456", | ||
name: "Shirts", | ||
slug: "shirts", | ||
}, | ||
]; | ||
|
||
// Act | ||
const { result } = renderHook(() => | ||
useViewProducts({ productTypeBaseData: multipleProductTypeBaseData }), | ||
); | ||
|
||
// Assert | ||
expect(result.current).not.toBeNull(); | ||
|
||
const expectedQuery = "0[s2.productType][0]=audiobooks&0[s2.productType][1]=shirts"; | ||
|
||
const receivedQuery = decodeURIComponent(result.current!.split("?")[1]); | ||
|
||
expect(receivedQuery).toBe(expectedQuery); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { productListPath } from "@dashboard/products/urls"; | ||
import { stringify } from "qs"; | ||
import { useMemo } from "react"; | ||
import urljoin from "url-join"; | ||
|
||
import { FilterElement } from "../ConditionalFilter/FilterElement"; | ||
import { prepareStructure } from "../ConditionalFilter/ValueProvider/utils"; | ||
import { TypeBaseData } from "./types"; | ||
|
||
export interface ProductTypeBaseData extends TypeBaseData { | ||
slug: string; | ||
} | ||
|
||
interface ViewProductsProps { | ||
productTypeBaseData: ProductTypeBaseData[] | undefined; | ||
} | ||
|
||
const baseDataToCondition = (baseData: ProductTypeBaseData) => ({ | ||
label: baseData.name, | ||
slug: baseData.slug, | ||
value: baseData.id, | ||
}); | ||
|
||
export const useViewProducts = ({ productTypeBaseData }: ViewProductsProps) => { | ||
const viewProductsUrl = useMemo(() => { | ||
if (!productTypeBaseData) { | ||
return null; | ||
} | ||
|
||
const productFilterElement = FilterElement.createStaticBySlug("productType"); | ||
|
||
const condition = | ||
productTypeBaseData.length > 1 | ||
? productFilterElement.condition.options[1] | ||
: productFilterElement.condition.options[0]; // "in" or "is" | ||
|
||
productFilterElement.updateCondition(condition); | ||
|
||
productFilterElement.updateRightOperator(productTypeBaseData.map(baseDataToCondition)); | ||
|
||
const url = stringify({ | ||
...prepareStructure([productFilterElement]), | ||
}); | ||
|
||
return urljoin(productListPath, `?${url}`); | ||
}, [productTypeBaseData]); | ||
|
||
return viewProductsUrl; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.