Skip to content

Commit

Permalink
Add support for searching by productId on productList loader (#46)
Browse files Browse the repository at this point in the history
* Added search by product id on productList loader

* Remove skus from props, fix interfaces
  • Loading branch information
ingridflack authored Sep 13, 2023
1 parent 617199d commit 6ab3874
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions vtex/loaders/legacy/productList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../../utils/segment.ts";
import { withIsSimilarTo } from "../../utils/similars.ts";
import { toProduct } from "../../utils/transform.ts";
import type { LegacySort, ProductID } from "../../utils/types.ts";
import type { LegacySort } from "../../utils/types.ts";

export interface CollectionProps extends CommonProps {
// TODO: pattern property isn't being handled by RJSF
Expand Down Expand Up @@ -49,11 +49,18 @@ export interface FQProps extends CommonProps {
count: number;
}

export interface ProductIDProps extends CommonProps {
export interface SkuIDProps extends CommonProps {
/**
* @description SKU ids to retrieve
*/
ids: ProductID[];
ids?: string[];
}

export interface ProductIDProps extends CommonProps {
/**
* @description Product ids to retrieve
*/
productIds?: string[];
}

export interface CommonProps {
Expand All @@ -63,23 +70,31 @@ export interface CommonProps {
similars?: boolean;
}

export type Props = CollectionProps | TermProps | ProductIDProps | FQProps;
export type Props =
| CollectionProps
| TermProps
| ProductIDProps
| SkuIDProps
| FQProps;

// deno-lint-ignore no-explicit-any
const isCollectionProps = (p: any): p is CollectionProps =>
typeof p.collection === "string" && typeof p.count === "number";

// deno-lint-ignore no-explicit-any
const isValidArrayProp = (prop: any) => Array.isArray(prop) && prop.length > 0;

// deno-lint-ignore no-explicit-any
const isSKUIDProps = (p: any): p is SkuIDProps => isValidArrayProp(p.ids);

// deno-lint-ignore no-explicit-any
const isProductIDProps = (p: any): p is ProductIDProps =>
Array.isArray(p.ids) && p.ids.length > 0;
isValidArrayProp(p.productIds);

// deno-lint-ignore no-explicit-any
const isFQProps = (p: any): p is FQProps =>
Array.isArray(p.fq) && p.fq.length > 0;
const isFQProps = (p: any): p is FQProps => isValidArrayProp(p.fq);

const fromProps = (
props: Props,
) => {
const fromProps = (props: Props) => {
const params = { fq: [] } as {
_from?: number;
_to?: number;
Expand All @@ -88,10 +103,22 @@ const fromProps = (
O?: LegacySort;
};

if (isSKUIDProps(props)) {
const skuIds = props.ids || [];

skuIds.forEach((skuId) => params.fq.push(`skuId:${skuId}`));
params._from = 0;
params._to = Math.max(skuIds.length - 1, 0);

return params;
}

if (isProductIDProps(props)) {
props.ids.forEach((skuId) => params.fq.push(`skuId:${skuId}`));
const productIds = props.productIds || [];

productIds.forEach((productId) => params.fq.push(`productId:${productId}`));
params._from = 0;
params._to = Math.max(props.ids.length - 1, 0);
params._to = Math.max(productIds.length - 1, 0);

return params;
}
Expand Down

0 comments on commit 6ab3874

Please sign in to comment.