-
Notifications
You must be signed in to change notification settings - Fork 30
feat: added new smarthint product listing loader #1571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { Product } from "../../commerce/types.ts"; | ||
| import { AppContext } from "../mod.ts"; | ||
| import { getFilterParam, toProduct } from "../utils/transform.ts"; | ||
| import { ComplexPageType, FilterProp } from "../utils/typings.ts"; | ||
| import { getSessionCookie } from "../utils/getSession.ts"; | ||
| import { getCategoriesParam, getProductParam } from "./recommendations.ts"; | ||
|
|
||
| export interface Props { | ||
| /** | ||
| * @hide | ||
| */ | ||
| filter?: FilterProp[]; | ||
| /** | ||
| * @hide | ||
| */ | ||
| categories?: string; | ||
| /** | ||
| * @hide | ||
| */ | ||
| products?: string[]; | ||
| /** | ||
| * @description Your recommendations are divided by positions, defining which position of the recommendations according to your desire. All recommendations configured in the Admin Panel will be returned. | ||
| */ | ||
| position: string; | ||
| /** | ||
| * @description Type of page you are setting up. | ||
| */ | ||
| pagetype: ComplexPageType; | ||
| /** | ||
| * @default padrao | ||
| */ | ||
| channel?: string; | ||
| } | ||
|
|
||
| /** | ||
| * @title SmartHint Integration | ||
| * @description Product List from Recommendations (for ProductShelf) | ||
| */ | ||
| const loader = async ( | ||
| props: Props, | ||
| req: Request, | ||
| ctx: AppContext, | ||
| ): Promise<Product[] | null> => { | ||
| const { recs, shcode, publicUrl, categoryTree } = ctx; | ||
| const { | ||
| categories: categoriesParam, | ||
| filter = [], | ||
| position, | ||
| products: productsParam = [], | ||
| pagetype, | ||
| channel = "padrao", | ||
| } = props; | ||
|
|
||
| const url = new URL(req.url); | ||
|
|
||
| const { anonymous } = getSessionCookie(req.headers); | ||
|
|
||
| const pageIdentifier = new URL(url.pathname, publicUrl)?.href; | ||
|
|
||
| const filters = getFilterParam(url, filter); | ||
|
|
||
| const productsString = getProductParam(pagetype, productsParam); | ||
|
|
||
| const categories = getCategoriesParam({ | ||
| categoriesParam, | ||
| categoryTree, | ||
| url, | ||
| }); | ||
|
|
||
| const data = await recs["GET /recommendationByPage/withProducts"]({ | ||
| shcode, | ||
| anonymous, | ||
| categories, | ||
| channel, | ||
| filter: filters, | ||
| pageIdentifier, | ||
| pagetype: pagetype.type, | ||
| position, | ||
| products: productsString, | ||
| }).then((r) => r.json()); | ||
|
|
||
| const positionItem = data.find((item) => | ||
| Number(item.SmartHintPosition) == Number(position) | ||
| ); | ||
|
|
||
| if (!positionItem) return null; | ||
|
|
||
| const products: Product[] = []; | ||
|
|
||
| // Extract products from RecommendationsProducts | ||
| positionItem.RecommendationsProducts?.forEach((rec) => { | ||
| rec.Products?.forEach((p) => products.push(toProduct(p))); | ||
| }); | ||
|
|
||
| // Extract products from RecommendationsPromotional | ||
| positionItem.RecommendationsPromotional?.forEach((rec) => { | ||
| rec.Products?.forEach((p) => products.push(toProduct(p))); | ||
| }); | ||
|
|
||
| // Extract products from RecommendationsCombination combos | ||
| positionItem.RecommendationsCombination?.forEach((rec) => { | ||
| rec.combos?.forEach((combo) => { | ||
| combo.Products?.forEach((p) => products.push(toProduct(p))); | ||
| }); | ||
| }); | ||
|
|
||
| return products.length ? products : null; | ||
| }; | ||
|
|
||
| export default loader; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,7 @@ export interface Props { | |||||||||||||||||||||||
| channel?: string; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function getProductParam(pagetype: ComplexPageType, productsParam: string[]) { | ||||||||||||||||||||||||
| export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) { | ||||||||||||||||||||||||
| if (productsParam.length) { | ||||||||||||||||||||||||
| return productsParam.map((productId) => `productid:${productId}`).join("&"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+38
to
41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix formatting for
💡 Suggested patch-export function getProductParam(pagetype: ComplexPageType, productsParam: string[]) {
+export function getProductParam(
+ pagetype: ComplexPageType,
+ productsParam: string[],
+) {📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: ci[error] 38-41: deno fmt --check failed. Found 1 not formatted file in 2112 files. Formatting diff indicates function declaration 'getProductParam' needs reformatting (parameter list spanning multiple lines). 🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
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.
Guard external response before parsing and
.find().This path assumes a successful JSON array response. A non-OK status, invalid JSON, or non-array payload will throw and break the loader.
🛡️ Suggested hardening
📝 Committable suggestion
🤖 Prompt for AI Agents