This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
feat(client): filters handler for store-api #740
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc1a033
feat(client): filters handler for store-api
mkucmus 2492e9b
feat(client): extend ShopwareParams interface for store-api filters
mkucmus 502011b
tests(client): store-api filters
mkucmus cbfcd42
chore: lint
mkucmus 5a624b3
docs: update client definitions
mkucmus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
49 changes: 49 additions & 0 deletions
49
packages/shopware-6-client/__tests__/helpers/convertToStoreApiFilters.spec.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,49 @@ | ||
import { convertToStoreApiFilters } from "../../src/helpers/convertToStoreApiFilters"; | ||
|
||
import { setup } from "@shopware-pwa/shopware-6-client"; | ||
|
||
describe("SearchConverter - convertToStoreApiFilters", () => { | ||
beforeEach(() => { | ||
setup(); | ||
}); | ||
it("should return empty object if no params provided", () => { | ||
const result = convertToStoreApiFilters(undefined as any); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it("should return empty object if any filter does not match", () => { | ||
const result = convertToStoreApiFilters([ | ||
{ | ||
field: "test", | ||
} as any, | ||
]); | ||
expect(result).toEqual({}); | ||
}); | ||
it("should return object with manufacturer property if manufacturerId filter exists", () => { | ||
const result = convertToStoreApiFilters([ | ||
{ | ||
field: "manufacturerId", | ||
value: ["divante", "shopware"], | ||
} as any, | ||
]); | ||
expect(result).toEqual({ | ||
manufacturer: "divante|shopware", | ||
}); | ||
}); | ||
it("should return object with properties property if multi filter containing propertyIds exists", () => { | ||
const result = convertToStoreApiFilters([ | ||
{ | ||
type: "multi", | ||
queries: [ | ||
{ | ||
field: "propertyIds", | ||
value: ["white", "black", "xs", "l"], | ||
}, | ||
], | ||
} as any, | ||
]); | ||
expect(result).toEqual({ | ||
properties: "white|black|xs|l", | ||
}); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/shopware-6-client/src/helpers/convertToStoreApiFilters.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,48 @@ | ||
import { | ||
SearchFilter, | ||
SearchFilterType, | ||
} from "@shopware-pwa/commons/interfaces/search/SearchFilter"; | ||
|
||
const concatIds = (ids: string[]) => ids.join("|"); | ||
const isFilterForProperty = (property: string, filter: any) => | ||
filter.hasOwnProperty("field") && filter.field === property; | ||
|
||
interface StoreApiListingFilters { | ||
manufacturer?: string; | ||
properties?: string; | ||
rating?: number; // to be handled later on | ||
"shipping-free"?: boolean; // to be handled later on | ||
"min-price"?: number; // to be handled later on | ||
"max-price"?: number; // to be handled later on | ||
} | ||
|
||
interface GenericFilter extends SearchFilter { | ||
value?: string[]; | ||
queries?: GenericFilter[]; | ||
} | ||
|
||
export function convertToStoreApiFilters( | ||
filters: Array<GenericFilter> | ||
): StoreApiListingFilters { | ||
let params: StoreApiListingFilters = {}; | ||
|
||
if (!filters || !filters.length) { | ||
return {}; | ||
} | ||
|
||
for (const filter of filters) { | ||
if (isFilterForProperty("manufacturerId", filter) && filter.value) { | ||
params.manufacturer = concatIds(filter.value); | ||
} | ||
if (filter.type === SearchFilterType.MULTI && filter.queries) { | ||
for (const subFilter of filter.queries) { | ||
/* istanbul ignore else */ | ||
if (isFilterForProperty("propertyIds", subFilter)) { | ||
params.properties = concatIds(subFilter.value as any); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return params; | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be just