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
48 changes: 36 additions & 12 deletions packages/klevu-core/src/connection/moi/moi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -375,6 +375,9 @@ export async function startMoi(
shouldSendMessage = storedSession.PQA[PQAKey].messages.length === 0
}
}
if (storedSession.context.locale !== ctx.locale) {
shouldSendMessage = true
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<MoiSavedSession> = {}
if (saved) {
parsed = JSON.parse(saved) as MoiSavedSession
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion packages/klevu-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/klevu-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const WithAdditionalData: StoryObj<KlevuProductQuery> = {
export const WithProductInfoAndItemId: StoryObj<KlevuProductQuery> = {
args: {
productId: "7585342095542",
pqaWidgetId: "pqa-6d4a4513-2d81-4ed7-84a5-59f40f7b7b95",
pqaWidgetId: "pqa-bf93b743-f148-42aa-b8b5-d1cf0504ab4b",
productInfoGenerator: "getProductInfo()",
itemId: "7585342095542",
itemGroupId: "7585342095542",
Expand Down Expand Up @@ -297,7 +297,7 @@ export const WithProductInfoAndItemId: StoryObj<KlevuProductQuery> = {
export const EmbeddedView: StoryObj<KlevuProductQuery> = {
args: {
productId: "7585342095542",
pqaWidgetId: "pqa-6d4a4513-2d81-4ed7-84a5-59f40f7b7b95",
pqaWidgetId: "pqa-bf93b743-f148-42aa-b8b5-d1cf0504ab4b",
productInfoGenerator: "getProductInfo()",
itemId: "7585342095542",
itemGroupId: "7585342095542",
Expand Down