diff --git a/packages/klevu-core/src/connection/moi/moi.ts b/packages/klevu-core/src/connection/moi/moi.ts index 100a8edf..bef73420 100644 --- a/packages/klevu-core/src/connection/moi/moi.ts +++ b/packages/klevu-core/src/connection/moi/moi.ts @@ -337,7 +337,7 @@ export async function startMoi( ...(options.channelId && { channelId: options.channelId }), ...(options.locale && { locale: options.locale }), } - const storedSession = await getStoredSession() + const storedSession = getStoredSession() let menu: MoiMenuOptions["menuOptions"] let genericOptions: MoiResponseGenericOptions["genericOptions"] @@ -375,6 +375,9 @@ export async function startMoi( shouldSendMessage = storedSession.PQA[PQAKey].messages.length === 0 } } + if (storedSession.context.locale !== ctx.locale) { + shouldSendMessage = true + } } } @@ -556,18 +559,21 @@ export class MoiSession { throw new Error("Cannot save PQA session without url or productId") } - saveSession({ - context: this.context, - PQA: { - [PQAkey]: { - menu: this.menu, - genericOptions: this.genericOptions, - messages: this.messages, - feedbacks: this.feedbacks, - questions: this.questions || [], + saveSession( + { + context: this.context, + PQA: { + [PQAkey]: { + menu: this.menu, + genericOptions: this.genericOptions, + messages: this.messages, + feedbacks: this.feedbacks, + questions: this.questions || [], + }, }, }, - }) + { config: this.config, options: this.options } + ) } } async addFeedback(messageId: string, thumbs: "up" | "down", reason?: string) { @@ -601,8 +607,12 @@ function getStoredSession(): MoiSavedSession | undefined { return JSON.parse(storedSession) as MoiSavedSession } -function saveSession(session: MoiSavedSession) { +async function saveSession( + session: MoiSavedSession, + settings?: { config: KlevuConfig; options: MoiStartOptions } +) { const saved = KlevuStorage.getItem(STORAGE_KEY) + let shouldResendProductInfo = false let parsed: Partial = {} if (saved) { parsed = JSON.parse(saved) as MoiSavedSession @@ -616,11 +626,25 @@ function saveSession(session: MoiSavedSession) { parsed.MOI = session.MOI break case "PQA": + shouldResendProductInfo = !!( + parsed.context?.sessionId && + parsed.context?.sessionId !== session.context.sessionId + ) parsed.context = session.context if (!parsed.PQA) { parsed.PQA = {} } + if (settings && shouldResendProductInfo) { + try { + const productInfo = settings.options?.productInfo + if (productInfo) { + await saveProductInfo(settings.config, productInfo) + } + } catch (err) { + console.warn("Failed to save regenerated product Info", err) + } + } if (!key || !session.PQA) { throw new Error("No url, productId or PQA session") diff --git a/packages/klevu-ui/package-lock.json b/packages/klevu-ui/package-lock.json index 184a51d5..2fcf9427 100644 --- a/packages/klevu-ui/package-lock.json +++ b/packages/klevu-ui/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@floating-ui/dom": "^1.5.3", - "@klevu/core": "^5.5.0", + "@klevu/core": "file:../klevu-core", "@stencil/core": "^4.4.0", "axios": "^1.5.1", "lit-html": "^2.8.0", diff --git a/packages/klevu-ui/package.json b/packages/klevu-ui/package.json index c0ec96e9..efd256c7 100644 --- a/packages/klevu-ui/package.json +++ b/packages/klevu-ui/package.json @@ -48,7 +48,7 @@ }, "dependencies": { "@floating-ui/dom": "^1.5.3", - "@klevu/core": "^5.5.0", + "@klevu/core": "file:../klevu-core", "@stencil/core": "^4.4.0", "axios": "^1.5.1", "lit-html": "^2.8.0", diff --git a/packages/klevu-ui/src/components/klevu-product-query/klevu-product-query.stories.ts b/packages/klevu-ui/src/components/klevu-product-query/klevu-product-query.stories.ts index 4b113d5e..35053263 100644 --- a/packages/klevu-ui/src/components/klevu-product-query/klevu-product-query.stories.ts +++ b/packages/klevu-ui/src/components/klevu-product-query/klevu-product-query.stories.ts @@ -225,7 +225,7 @@ export const WithAdditionalData: StoryObj = { export const WithProductInfoAndItemId: StoryObj = { args: { productId: "7585342095542", - pqaWidgetId: "pqa-6d4a4513-2d81-4ed7-84a5-59f40f7b7b95", + pqaWidgetId: "pqa-bf93b743-f148-42aa-b8b5-d1cf0504ab4b", productInfoGenerator: "getProductInfo()", itemId: "7585342095542", itemGroupId: "7585342095542", @@ -297,7 +297,7 @@ export const WithProductInfoAndItemId: StoryObj = { export const EmbeddedView: StoryObj = { args: { productId: "7585342095542", - pqaWidgetId: "pqa-6d4a4513-2d81-4ed7-84a5-59f40f7b7b95", + pqaWidgetId: "pqa-bf93b743-f148-42aa-b8b5-d1cf0504ab4b", productInfoGenerator: "getProductInfo()", itemId: "7585342095542", itemGroupId: "7585342095542",