-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add bundled SectionCampaign mode #299
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 +1,5 @@ | ||
| import { nostojs } from "@nosto/nosto-js" | ||
| import { getText } from "@/utils/fetch" | ||
| import { getText, postJSON } from "@/utils/fetch" | ||
| import { customElement } from "../decorators" | ||
| import { NostoElement } from "../Element" | ||
| import { addRequest } from "../Campaign/orchestrator" | ||
|
|
@@ -9,19 +9,32 @@ import { JSONResult } from "@nosto/nosto-js/client" | |
| * NostoSectionCampaign is a custom element that fetches Nosto placement results and renders the results | ||
| * using a Shopify section template via the Section Rendering API. | ||
| * | ||
| * default mode: | ||
| * Section is fetched via /search endpoint with product handles as query param | ||
| * | ||
| * bundled mode: | ||
| * Section is fetched via /cart/update.js endpoint with section name in payload | ||
| * and campaign metadata is persisted in cart attributes | ||
| * | ||
| * @property {string} placement - The placement identifier for the campaign. | ||
| * @property {string} section - The section to be used for Section Rendering API based rendering. | ||
| * @property {string} handles - (internal) colon-separated list of product handles currently rendered | ||
| * @property {string} mode - (internal) rendering mode, supported values: "section" (default), "bundled" | ||
| */ | ||
| @customElement("nosto-section-campaign") | ||
| export class SectionCampaign extends NostoElement { | ||
| /** @private */ | ||
| static attributes = { | ||
| placement: String, | ||
| section: String | ||
| section: String, | ||
| handles: String, | ||
| mode: String | ||
| } | ||
|
|
||
| placement!: string | ||
| section!: string | ||
| handles!: string | ||
| mode?: "section" | "bundled" | ||
|
|
||
| async connectedCallback() { | ||
| this.toggleAttribute("loading", true) | ||
|
|
@@ -41,14 +54,38 @@ export class SectionCampaign extends NostoElement { | |
| if (!rec) { | ||
| return | ||
| } | ||
| const markup = await getSectionMarkup(this, rec) | ||
| this.innerHTML = markup | ||
| const handles = rec.products.map(product => product.handle).join(":") | ||
| const bundled = this.mode === "bundled" | ||
| if (!bundled || this.handles !== handles) { | ||
| const markup = await (bundled ? getBundledMarkup : getSectionMarkup)(this, handles, rec) | ||
| if (markup) { | ||
| this.innerHTML = markup | ||
| } | ||
| } | ||
| api.attributeProductClicksInCampaign(this, rec) | ||
| } | ||
| } | ||
|
|
||
| async function getSectionMarkup(element: SectionCampaign, rec: JSONResult) { | ||
| const handles = rec.products.map(product => product.handle).join(":") | ||
| async function getBundledMarkup(element: SectionCampaign, handles: string, rec: JSONResult) { | ||
| const target = new URL("/cart/update.js", window.location.href) | ||
| const payload = { | ||
| attributes: { | ||
| [`nosto_${element.placement}_title`]: rec.title || "", | ||
| [`nosto_${element.placement}_handles`]: handles | ||
| }, | ||
| sections: element.section | ||
| } | ||
| const reponse = await postJSON<{ sections: Record<string, string> }>(target.href, payload) | ||
| const sectionHtml = reponse.sections[element.section] || "" | ||
| if (sectionHtml) { | ||
| const parser = new DOMParser() | ||
| const doc = parser.parseFromString(sectionHtml, "text/html") | ||
| return doc.querySelector(`nosto-bundled-campaign[placement="${element.placement}"]`)?.innerHTML || "" | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| async function getSectionMarkup(element: SectionCampaign, handles: string, rec: JSONResult) { | ||
| const target = new URL("/search", window.location.href) | ||
|
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.
|
||
| target.searchParams.set("section_id", element.section) | ||
| target.searchParams.set("q", handles) | ||
|
|
||
This file contains hidden or 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 hidden or 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,63 @@ | ||
| /** @jsx createElement */ | ||
| import { describe, it, expect } from "vitest" | ||
| import { mockNostoRecs } from "../mockNostoRecs" | ||
| import { createElement } from "../utils/jsx" | ||
| import { addHandlers } from "../msw.setup" | ||
| import { http, HttpResponse } from "msw" | ||
| import { SectionCampaign } from "@/main" | ||
|
|
||
| describe("SectionCampaign bundled", () => { | ||
| it("renders bundled section markup and attributes product clicks", async () => { | ||
| const products = [{ handle: "product-a" }, { handle: "product-b" }] | ||
| const { attributeProductClicksInCampaign, load } = mockNostoRecs({ placement1: { products, title: "Rec Title" } }) | ||
|
|
||
| const innerHTML = `<div class=\"inner\">Bundled Render</div>` | ||
| addHandlers( | ||
| http.post("/cart/update.js", () => { | ||
| return HttpResponse.json({ | ||
| sections: { | ||
| "nosto-product-titles": `<nosto-bundled-campaign placement=\"placement1\">${innerHTML}</nosto-bundled-campaign>` | ||
| } | ||
| }) | ||
| }) | ||
| ) | ||
|
|
||
| const el = ( | ||
| <nosto-section-campaign mode="bundled" placement="placement1" section="nosto-product-titles" /> | ||
| ) as SectionCampaign | ||
| document.body.appendChild(el) | ||
|
|
||
| await el.connectedCallback() | ||
|
|
||
| expect(load).toHaveBeenCalled() | ||
| expect(el.innerHTML).toBe(innerHTML) | ||
| expect(attributeProductClicksInCampaign).toHaveBeenCalledWith(el, { products, title: "Rec Title" }) | ||
| expect(el.hasAttribute("loading")).toBe(false) | ||
| }) | ||
|
|
||
| it("skips re-render when current handles match computed handles", async () => { | ||
| const products = [{ handle: "product-a" }] | ||
| mockNostoRecs({ placement1: { products } }) | ||
|
|
||
| // No handler needed, should skip network | ||
| const el = ( | ||
| <nosto-section-campaign mode="bundled" placement="placement1" section="nosto-product-titles" /> | ||
| ) as SectionCampaign | ||
| el.handles = "product-a" // pre-set to match, should skip postJSON | ||
|
|
||
| await el.connectedCallback() | ||
| // No fetch should be made, so nothing to assert here except no error thrown | ||
| }) | ||
|
|
||
| it("propagates post errors and clears loading", async () => { | ||
| mockNostoRecs({ placement1: { products: [{ handle: "x" }] } }) | ||
| addHandlers(http.get("/cart/update.js", () => HttpResponse.text("Error", { status: 500 }))) | ||
|
|
||
| const el = ( | ||
| <nosto-section-campaign mode="bundled" placement="placement1" section="nosto-product-titles" /> | ||
| ) as SectionCampaign | ||
|
|
||
| await expect(el.connectedCallback()).rejects.toThrow() | ||
| expect(el.hasAttribute("loading")).toBe(false) | ||
| }) | ||
| }) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use with locale