-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add enhanced use products (#207)
* feat: add use product enhanced hooks - adds a hook that gives back the main image and other files associated with the products returned * chore: add changeset - adds a hook that gives back the main image and other files associated with the products returned * fix: remove empty object on cart clear * chore: add changeset - adds a hook that gives back the main image and other files associated with the products returned * feat: use latest hooks - adds a hook that gives back the main image and other files associated with the products returned
- Loading branch information
Showing
11 changed files
with
150 additions
and
37 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 @@ | ||
--- | ||
"@elasticpath/d2c-schematics": minor | ||
--- | ||
|
||
remove empty object on cart clear |
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 @@ | ||
--- | ||
"@elasticpath/react-shopper-hooks": minor | ||
--- | ||
|
||
- adds a hook that gives back the main image and other files associated with the products returned |
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
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
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
13 changes: 11 additions & 2 deletions
13
packages/react-shopper-hooks/example/ProductListExample.tsx
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
107 changes: 107 additions & 0 deletions
107
packages/react-shopper-hooks/src/product/hooks/use-products-enhanced.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,107 @@ | ||
import type { | ||
ShopperCatalogResourcePage, | ||
ProductResponse, | ||
File, | ||
} from "@moltin/sdk" | ||
import { UseQueryResult } from "@tanstack/react-query" | ||
import { | ||
ShopperCatalogProductsInclude, | ||
useProducts, | ||
UseProductsParams, | ||
UseProductsQueryOptions, | ||
} from "./use-products" | ||
|
||
type UseProductsEnhancedData = ShopperCatalogResourcePage< | ||
ProductResponse & { | ||
enhanced: { mainImage: File | null; otherImages: File[] | null } | ||
} | ||
> | ||
|
||
export function useProductsEnhanced( | ||
params?: UseProductsParams, | ||
options?: UseProductsQueryOptions, | ||
): UseQueryResult<UseProductsEnhancedData, Error> { | ||
return useProducts( | ||
{ | ||
...params, | ||
include: combineIncludes( | ||
["main_image", "files", "component_products"], | ||
params, | ||
), | ||
}, | ||
{ | ||
...options, | ||
select: (data): UseProductsEnhancedData => { | ||
const transformedData = options?.select?.(data) ?? data | ||
|
||
const fileLookup = createFileLookupDict(transformedData.included) | ||
|
||
const enhancedData = transformedData.data.map((originalData) => { | ||
return { | ||
...originalData, | ||
enhanced: { | ||
mainImage: getProductMainImage(originalData, fileLookup), | ||
otherImages: getProductOtherImages(originalData, fileLookup), | ||
}, | ||
} | ||
}) | ||
|
||
return { | ||
...transformedData, | ||
data: enhancedData, | ||
} | ||
}, | ||
}, | ||
) as UseQueryResult<UseProductsEnhancedData, Error> | ||
} | ||
|
||
function getProductMainImage( | ||
product: ProductResponse, | ||
fileLookup: Record<string, File>, | ||
): File | null { | ||
const mainImageId = product?.relationships?.main_image?.data?.id | ||
return mainImageId ? fileLookup[mainImageId] : null | ||
} | ||
|
||
function getProductOtherImages( | ||
product: ProductResponse, | ||
fileLookup: Record<string, File>, | ||
): File[] | null { | ||
const otherImagesIds = | ||
product?.relationships?.files?.data?.map((file) => file.id) ?? [] | ||
const mainImageId = product?.relationships?.main_image?.data?.id | ||
return otherImagesIds | ||
.map((id) => fileLookup[id]) | ||
.filter((x) => x.id !== mainImageId) | ||
} | ||
|
||
function createFileLookupDict( | ||
includes: ShopperCatalogResourcePage<ProductResponse>["included"], | ||
): Record<string, File> { | ||
return ( | ||
includes?.files?.reduce((acc, curr) => { | ||
return { ...acc, [curr.id]: curr } | ||
}, {}) ?? {} | ||
) | ||
} | ||
|
||
function combineIncludes( | ||
include: ShopperCatalogProductsInclude[], | ||
params: UseProductsParams, | ||
): ShopperCatalogProductsInclude[] { | ||
return [ | ||
...new Set<ShopperCatalogProductsInclude>([ | ||
...include, | ||
...extractIncludeFromParams(params), | ||
]), | ||
] | ||
} | ||
|
||
function extractIncludeFromParams( | ||
params: UseProductsParams, | ||
): ShopperCatalogProductsInclude[] { | ||
if (!params?.include) { | ||
return [] | ||
} | ||
return Array.isArray(params.include) ? params.include : [params.include] | ||
} |
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