Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/components/Campaign/Campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ export class Campaign extends NostoElement {
async load() {
await loadCampaign(this)
}

/**
* Extension point: override to enrich or modify the template context.
* This method is called during campaign rendering to create the context object
* that will be passed to the template. Subclasses can override this method
* to add custom properties, feature flags, or transform the data.
*
* @param raw - The raw JSON result from the Nosto API
* @returns The context object to be used in template rendering
*
* @example
* ```typescript
* class CustomCampaign extends Campaign {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the cases where we would extend the Campaign custom element? Wouldn't it make it complex trying to promote inheritance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see good alternatives for inheritance here, since registration of custom functionality into custom element classes is also not something that is available as a standard pattern

* async createContext(raw) {
* const context = await super.createContext(raw);
* return { ...context, customProperty: 'value' };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still not understanding.

  1. add utility functions - Can you elaborate on what kind of utility functions we are planning to expose to, for e.g., merchant's template? Are we going to create these CustomCampaign elements case-by-case basis and expose utility functions specific to every case?
  2. Use Shopify product data - How/Why do we mix Shopify product data into Nosto's recommendation data?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions that could be useful are

  • formatting functions
  • event listener functions

We replace Nosto recommendation data with Shopify data for alignment

* }
* }
* ```
*/
async createContext(raw: JSONResult): Promise<object> {
return getContext(raw)
}
}

export async function loadCampaign(element: Campaign) {
Expand All @@ -85,7 +108,7 @@ export async function loadCampaign(element: Campaign) {
if (rec) {
if (useTemplate) {
const template = getTemplate(element)
compile(element, template, getContext(rec as JSONResult))
compile(element, template, await element.createContext(rec as JSONResult))
api.attributeProductClicksInCampaign(element, rec as JSONResult)
} else {
await api.placements.injectCampaigns(
Expand Down
43 changes: 43 additions & 0 deletions test/components/Campaign.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jsx createElement */
import { describe, it, expect, vi, Mock } from "vitest"
import { Campaign } from "@/components/Campaign/Campaign"
import { JSONResult } from "@nosto/nosto-js/client"
import { mockNostoRecs } from "../mockNostoRecs"
import { createElement } from "../utils/jsx"

Expand Down Expand Up @@ -163,4 +164,46 @@ describe("Campaign", () => {
expect(mockObserver.observe).not.toHaveBeenCalled()
expect(mockBuilder.load).not.toHaveBeenCalled()
})

it("should allow subclasses to override createContext method", async () => {
// Create a custom campaign that extends the base Campaign
class CustomCampaign extends Campaign {
async createContext(raw: JSONResult) {
const context = await super.createContext(raw)
return { ...context, customProperty: "customValue", modified: true }
}
}

// Register the custom element
if (!customElements.get("custom-campaign")) {
customElements.define("custom-campaign", CustomCampaign)
}

const templateId = "custom-template"
const template = document.createElement("template")
template.id = templateId
template.innerHTML = `
<div class="custom">{{ customProperty }}</div>
<div class="modified">{{ modified }}</div>
<div class="title">{{ title }}</div>
`
document.body.appendChild(template)

const { mockBuilder } = mockNostoRecs({
"custom-123": {
title: "Custom Campaign Test"
}
})

// Create custom element using JSX syntax
const customCampaign = (<custom-campaign placement="custom-123" template={templateId} />) as Campaign
document.body.appendChild(customCampaign)

await customCampaign.connectedCallback()

expect(customCampaign.innerHTML).toContain("customValue")
expect(customCampaign.innerHTML).toContain("true")
expect(customCampaign.innerHTML).toContain("Custom Campaign Test")
expect(mockBuilder.load).toHaveBeenCalledWith()
})
})