Skip to content

Commit

Permalink
Add yourviews extension
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Apr 15, 2023
1 parent bed0271 commit ff95523
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 79 deletions.
66 changes: 66 additions & 0 deletions extensions/yourViews.ts
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;
};
2 changes: 1 addition & 1 deletion import_map.json
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]",
Expand Down
79 changes: 46 additions & 33 deletions live.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import config from "./deno.json" assert { type: "json" };
import { DecoManifest } from "$live/types.ts";

import * as $0 from "./functions/vtexConfig.ts";
import * as $1 from "./functions/vtexProductListingPage.ts";
import * as $2 from "./functions/vndaProductList.ts";
Expand Down Expand Up @@ -38,30 +37,34 @@ import * as $$$$$$$$5 from "./sections/configShopify.global.tsx";
import * as $$$$$$$$6 from "./sections/configVNDA.global.tsx";
import * as $$$$$$$$7 from "./sections/configVTEX.global.tsx";
import * as $$$$$$$$8 from "./sections/SEOPDP.tsx";
import * as $$$$$$$$$$$0 from "./extensions/yourViews.ts";
import * as $live_middleware from "$live/routes/_middleware.ts";
import * as $live_workbench from "$live/routes/live/workbench.ts";
import * as $live_invoke from "$live/routes/live/invoke/index.ts";
import * as $live_editorData from "$live/routes/live/editorData.ts";
import * as $live_inspect from "$live/routes/live/inspect.ts";
import * as $live_meta from "$live/routes/live/_meta.ts";
import * as $live_previews from "$live/routes/live/previews/[...block].tsx";
import * as $live_catchall from "$live/routes/[...catchall].tsx";
import * as i1$0 from "$live/handlers/routesSelection.ts";
import * as i1$1 from "$live/handlers/router.ts";
import * as i1$2 from "$live/handlers/devPage.ts";
import * as i1$3 from "$live/handlers/fresh.ts";
import * as i1$$0 from "$live/pages/LivePage.tsx";
import * as i1$$$0 from "$live/sections/PageInclude.tsx";
import * as i1$$$$0 from "$live/matchers/MatchDate.ts";
import * as i1$$$$1 from "$live/matchers/MatchUserAgent.ts";
import * as i1$$$$2 from "$live/matchers/MatchSite.ts";
import * as i1$$$$3 from "$live/matchers/MatchMulti.ts";
import * as i1$$$$4 from "$live/matchers/MatchRandom.ts";
import * as i1$$$$5 from "$live/matchers/MatchEnvironment.ts";
import * as i1$$$$6 from "$live/matchers/MatchAlways.ts";
import * as i1$$$$$0 from "$live/flags/audience.ts";
import * as i1$$$$$1 from "$live/flags/everyone.ts";
import * as i1$0 from "$live/loaders/addExtensions.ts";
import * as i1$$0 from "$live/handlers/routesSelection.ts";
import * as i1$$1 from "$live/handlers/router.ts";
import * as i1$$2 from "$live/handlers/devPage.ts";
import * as i1$$3 from "$live/handlers/fresh.ts";
import * as i1$$$0 from "$live/pages/LivePage.tsx";
import * as i1$$$$0 from "$live/sections/PageInclude.tsx";
import * as i1$$$$$0 from "$live/matchers/MatchDate.ts";
import * as i1$$$$$1 from "$live/matchers/MatchUserAgent.ts";
import * as i1$$$$$2 from "$live/matchers/MatchSite.ts";
import * as i1$$$$$3 from "$live/matchers/MatchMulti.ts";
import * as i1$$$$$4 from "$live/matchers/MatchRandom.ts";
import * as i1$$$$$5 from "$live/matchers/MatchEnvironment.ts";
import * as i1$$$$$6 from "$live/matchers/MatchAlways.ts";
import * as i1$$$$$$0 from "$live/flags/audience.ts";
import * as i1$$$$$$1 from "$live/flags/everyone.ts";
import * as i1$$$$$$$0 from "$live/extensions/composite.ts";

const manifest: DecoManifest = {
const manifest = {
"functions": {
"deco-sites/std/functions/vtexConfig.ts": $0,
"deco-sites/std/functions/vtexProductListingPage.ts": $1,
Expand Down Expand Up @@ -93,6 +96,7 @@ const manifest: DecoManifest = {
"./routes/404.tsx": $$$$0,
"./routes/_middleware.ts": $live_middleware,
"./routes/live/workbench.ts": $live_workbench,
"./routes/live/invoke/index.ts": $live_invoke,
"./routes/live/editorData.ts": $live_editorData,
"./routes/live/inspect.ts": $live_inspect,
"./routes/live/_meta.ts": $live_meta,
Expand All @@ -109,33 +113,42 @@ const manifest: DecoManifest = {
"deco-sites/std/sections/configVNDA.global.tsx": $$$$$$$$6,
"deco-sites/std/sections/configVTEX.global.tsx": $$$$$$$$7,
"deco-sites/std/sections/SEOPDP.tsx": $$$$$$$$8,
"$live/sections/PageInclude.tsx": i1$$$0,
"$live/sections/PageInclude.tsx": i1$$$$0,
},
"extensions": {
"deco-sites/std/extensions/yourViews.ts": $$$$$$$$$$$0,
"$live/extensions/composite.ts": i1$$$$$$$0,
},
"loaders": {
"$live/loaders/addExtensions.ts": i1$0,
},
"handlers": {
"$live/handlers/routesSelection.ts": i1$0,
"$live/handlers/router.ts": i1$1,
"$live/handlers/devPage.ts": i1$2,
"$live/handlers/fresh.ts": i1$3,
"$live/handlers/routesSelection.ts": i1$$0,
"$live/handlers/router.ts": i1$$1,
"$live/handlers/devPage.ts": i1$$2,
"$live/handlers/fresh.ts": i1$$3,
},
"pages": {
"$live/pages/LivePage.tsx": i1$$0,
"$live/pages/LivePage.tsx": i1$$$0,
},
"matchers": {
"$live/matchers/MatchDate.ts": i1$$$$0,
"$live/matchers/MatchUserAgent.ts": i1$$$$1,
"$live/matchers/MatchSite.ts": i1$$$$2,
"$live/matchers/MatchMulti.ts": i1$$$$3,
"$live/matchers/MatchRandom.ts": i1$$$$4,
"$live/matchers/MatchEnvironment.ts": i1$$$$5,
"$live/matchers/MatchAlways.ts": i1$$$$6,
"$live/matchers/MatchDate.ts": i1$$$$$0,
"$live/matchers/MatchUserAgent.ts": i1$$$$$1,
"$live/matchers/MatchSite.ts": i1$$$$$2,
"$live/matchers/MatchMulti.ts": i1$$$$$3,
"$live/matchers/MatchRandom.ts": i1$$$$$4,
"$live/matchers/MatchEnvironment.ts": i1$$$$$5,
"$live/matchers/MatchAlways.ts": i1$$$$$6,
},
"flags": {
"$live/flags/audience.ts": i1$$$$$0,
"$live/flags/everyone.ts": i1$$$$$1,
"$live/flags/audience.ts": i1$$$$$$0,
"$live/flags/everyone.ts": i1$$$$$$1,
},
"islands": {},
"config": config,
"baseUrl": import.meta.url,
};

export default manifest;
export type Manifest = typeof manifest;

export default manifest satisfies DecoManifest;
Loading

0 comments on commit ff95523

Please sign in to comment.