generated from deco-sites/start
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marcos Candeia <[email protected]>
- Loading branch information
Showing
4 changed files
with
312 additions
and
79 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,66 @@ | ||
import { Product } from "deco-sites/std/commerce/types.ts"; | ||
import { | ||
ConfigYourViews, | ||
createClient, | ||
} from "deco-sites/std/commerce/yourViews/client.ts"; | ||
import { ExtensionOf } from "https://denopkg.com/deco-cx/live@3c5ca2344ff1d8168085a3d5685c57100e6bdedb/blocks/extension.ts"; | ||
import DataLoader from "https://esm.sh/[email protected]"; | ||
import { Rating } from "../commerce/yourViews/types.ts"; | ||
|
||
export type Props = ConfigYourViews; | ||
|
||
type RatingFetcher = (productId: string) => Promise<Rating | undefined>; | ||
const aggregateRatingFor = | ||
(fetcher: RatingFetcher) => async ({ isVariantOf }: Product) => { | ||
const productId = isVariantOf!.productGroupID; | ||
const rating = await fetcher(productId); | ||
|
||
return rating | ||
? { | ||
"@type": "AggregateRating" as const, | ||
ratingCount: rating.TotalRatings, | ||
ratingValue: rating.Rating, | ||
} | ||
: undefined; | ||
}; | ||
|
||
const fetcherPool: Record< | ||
string, | ||
RatingFetcher | ||
> = {}; | ||
|
||
const getFetcher = ( | ||
config: Props, | ||
): RatingFetcher => { | ||
const key = `${config.appId}${config.token}`; | ||
if (fetcherPool[key]) { | ||
return fetcherPool[key]; | ||
} | ||
const client = createClient(config); | ||
const dl = new DataLoader((productIds: readonly string[]) => | ||
client.ratings({ productIds: productIds as string[] }).then((rat) => | ||
sameOrder(productIds, rat.Element) | ||
) | ||
); | ||
return fetcherPool[key] = dl.load.bind(dl); | ||
}; | ||
|
||
export default function AddYourViews(config: Props): ExtensionOf<Product> { | ||
const client = getFetcher(config); | ||
const aggregateRating = aggregateRatingFor(client); | ||
|
||
return { | ||
aggregateRating, | ||
}; | ||
} | ||
|
||
const sameOrder = ( | ||
productIds: readonly string[], | ||
elements: Rating[], | ||
): (Rating | undefined)[] => { | ||
const ordered = new Array<Rating | undefined>(productIds.length); | ||
for (let i = 0; i < productIds.length; i++) { | ||
ordered[i] = elements.find((e) => e.ProductId === productIds[i]); | ||
} | ||
return ordered; | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"imports": { | ||
"deco-sites/std/": "./", | ||
"$live/": "https://denopkg.com/deco-cx/live@1.0.0-rc.46/", | ||
"$live/": "https://denopkg.com/deco-cx/live@3c5ca2344ff1d8168085a3d5685c57100e6bdedb/", | ||
"partytown/": "https://deno.land/x/[email protected]/", | ||
"$fresh/": "https://deno.land/x/[email protected]/", | ||
"preact": "https://esm.sh/[email protected]", | ||
|
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.