|
10 | 10 | import {addToCart, checkCartQuantity} from '{{'cart-manager.js' | asset_url}}'; |
11 | 11 |
|
12 | 12 | const inlineRoot = document.querySelector(".trieve-inline-root"); |
13 | | - console.log("root", inlineRoot,"please", "metafields {{ app.metafields }}", "{{ block.settings.product.id }}", "{{ block.settings.product.metafields }}"); |
| 13 | + const appMetafieldTrieve = {{ app.metafields.trieve | json }}; |
| 14 | +
|
| 15 | + const blockSettings = {{ block.settings | json }}; |
| 16 | + const productMetafieldTrieve = {{ product.metafields.trieve | json }}; |
| 17 | + let trievePDPQuestions = null; |
| 18 | + if (productMetafieldTrieve && Object.keys(productMetafieldTrieve).includes("trievePDPQuestions")) { |
| 19 | + trievePDPQuestions = productMetafieldTrieve.trievePDPQuestions; |
| 20 | + } |
| 21 | + let defaultAiQuestions = null; |
| 22 | + if (trievePDPQuestions && trievePDPQuestions.length > 0) { |
| 23 | + defaultAiQuestions = trievePDPQuestions.map((question) => question.text); |
| 24 | + } |
14 | 25 |
|
15 | 26 | const toBool = (value) => value === 'true'; |
16 | 27 | const getValue = (value, defaultValue) => { |
|
20 | 31 | return value; |
21 | 32 | }; |
22 | 33 |
|
23 | | - renderToDiv(inlineRoot, { |
24 | | - inline: true, |
25 | | - apiKey: getValue("{{ app.metafields.trieve.api_key }}", ""), |
26 | | - datasetId: getValue("{{ app.metafields.trieve.dataset_id }}", ""), |
27 | | - baseUrl: getValue("{{ block.settings.base_url }}", "https://api.trieve.ai"), |
28 | | - type: "ecommerce", |
29 | | - useGroupSearch: true, |
30 | | - allowSwitchingModes: toBool(getValue("{{ block.settings.allow_switching_modes }}", true)), |
31 | | - zIndex: Number(getValue("{{ block.settings.z_index }}", 21474830000000)), |
32 | | - scaleRem: true, |
33 | | - defaultSearchMode: getValue("{{ block.settings.default_search_mode }}", "chat"), |
34 | | - brandName: getValue("{{ block.settings.brand_name }}", ""), |
35 | | - brandColor: getValue("{{ block.settings.brand_color }}", ""), |
36 | | - placeholder: getValue("{{ block.settings.placeholder }}", "Search..."), |
37 | | - chatPlaceholder: getValue("{{ block.settings.chat_placeholder }}", "Ask Anything..."), |
38 | | - suggestedQueries: toBool(getValue("{{ block.settings.suggested_queries }}", true)), |
39 | | - followupQuestions: toBool(getValue("{{ block.settings.suggested_queries }}", true)), |
40 | | - numberOfSuggestions: Number(getValue("{{ block.settings.number_of_suggestions }}", 3)), |
41 | | - theme: getValue("{{ block.settings.theme }}", "light"), |
42 | | - inlineHeader: "{{ block.settings.inline_header }} ", |
43 | | - onAddToCart: (chunk) => { |
44 | | - addToCart(parseInt(chunk.tracking_id)) |
45 | | - }, |
46 | | - getCartQuantity: (trackingId) => { |
47 | | - return checkCartQuantity(parseInt(trackingId)); |
48 | | - }, |
49 | | - }); |
| 34 | + if (!appMetafieldTrieve.dataset_id || !appMetafieldTrieve.api_key) { |
| 35 | + console.error("Trieve: Cannut render inline Triece comopnent due to missing dataset_id or api_key in the app metafields"); |
| 36 | + } else { |
| 37 | + console.log("Trieve: Found dataset_id and api_key in the app metafields", appMetafieldTrieve.dataset_id, appMetafieldTrieve.api_key, defaultAiQuestions); |
| 38 | +
|
| 39 | + renderToDiv(inlineRoot, { |
| 40 | + inline: true, |
| 41 | + apiKey: appMetafieldTrieve.api_key, |
| 42 | + datasetId: appMetafieldTrieve.dataset_id, |
| 43 | + baseUrl: getValue("{{ block.settings.base_url }}", "https://api.trieve.ai"), |
| 44 | + type: "ecommerce", |
| 45 | + useGroupSearch: true, |
| 46 | + allowSwitchingModes: toBool(getValue("{{ block.settings.allow_switching_modes }}", true)), |
| 47 | + zIndex: Number(getValue("{{ block.settings.z_index }}", 2147483645)), |
| 48 | + scaleRem: true, |
| 49 | + defaultSearchMode: getValue("{{ block.settings.default_search_mode }}", "chat"), |
| 50 | + brandName: getValue("{{ block.settings.brand_name }}", ""), |
| 51 | + brandColor: getValue("{{ block.settings.brand_color }}", ""), |
| 52 | + placeholder: getValue("{{ block.settings.placeholder }}", "Search..."), |
| 53 | + chatPlaceholder: getValue("{{ block.settings.chat_placeholder }}", "Ask Anything..."), |
| 54 | + suggestedQueries: toBool(getValue("{{ block.settings.suggested_queries }}", true)), |
| 55 | + followupQuestions: toBool(getValue("{{ block.settings.suggested_queries }}", true)), |
| 56 | + numberOfSuggestions: Number(getValue("{{ block.settings.number_of_suggestions }}", 3)), |
| 57 | + defaultAiQuestions, |
| 58 | + theme: getValue("{{ block.settings.theme }}", "light"), |
| 59 | + inlineHeader: "{{ block.settings.inline_header }} ", |
| 60 | + onAddToCart: (chunk) => { |
| 61 | + addToCart(parseInt(chunk.tracking_id)) |
| 62 | + }, |
| 63 | + getCartQuantity: (trackingId) => { |
| 64 | + return checkCartQuantity(parseInt(trackingId)); |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
50 | 68 | </script> |
51 | 69 |
|
52 | 70 | {% comment %} |
|
57 | 75 | {% schema %} |
58 | 76 | { |
59 | 77 | "name": "Trieve Product Chat", |
60 | | - "target": "section", |
61 | | - "settings": [ |
62 | | - { |
63 | | - "type": "text", |
64 | | - "id": "base_url", |
65 | | - "label": "Base URL", |
66 | | - "default": "https://api.trieve.ai" |
67 | | - }, |
68 | | - { |
69 | | - "type": "select", |
70 | | - "id": "default_search_mode", |
71 | | - "label": "Default Search Mode", |
72 | | - "options": [ |
73 | | - { "label": "Search", "value": "search" }, |
74 | | - { "label": "Chat", "value": "chat" } |
75 | | - ], |
76 | | - "default": "chat" |
77 | | - }, |
78 | | - { |
79 | | - "type": "text", |
80 | | - "id": "placeholder", |
81 | | - "label": "Search Placeholder Text", |
82 | | - "default": "Search..." |
83 | | - }, |
84 | | - { |
85 | | - "type": "text", |
86 | | - "id": "chat_placeholder", |
87 | | - "label": "Chat Placeholder Text", |
88 | | - "default": "Ask Anything..." |
89 | | - }, |
90 | | - { |
91 | | - "type": "select", |
92 | | - "id": "theme", |
93 | | - "label": "Theme", |
94 | | - "options": [ |
95 | | - { "label": "Light", "value": "light" }, |
96 | | - { "label": "Dark", "value": "dark" } |
97 | | - ], |
98 | | - "default": "light" |
99 | | - }, |
100 | | - { |
101 | | - "type": "color", |
102 | | - "id": "brand_color", |
103 | | - "label": "Brand Color", |
104 | | - "default": "#ff0000" |
105 | | - }, |
106 | | - { |
107 | | - "type": "text", |
108 | | - "id": "brand_name", |
109 | | - "label": "Brand Name", |
110 | | - "default": "TrieveAi" |
111 | | - }, |
112 | | - { |
113 | | - "type": "checkbox", |
114 | | - "id": "suggested_queries", |
115 | | - "label": "Show Suggested Queries", |
116 | | - "default": true |
117 | | - }, |
118 | | - { |
119 | | - "type": "number", |
120 | | - "id": "number_of_suggestions", |
121 | | - "label": "Number of Suggestions", |
122 | | - "default": 3 |
123 | | - }, |
124 | | - { |
125 | | - "type": "select", |
126 | | - "id": "type", |
127 | | - "label": "Modal Type", |
128 | | - "options": [ |
129 | | - { "label": "Docs", "value": "docs" }, |
130 | | - { "label": "E-commerce", "value": "ecommerce" } |
131 | | - ], |
132 | | - "default": "docs" |
133 | | - }, |
134 | | - { |
135 | | - "type": "checkbox", |
136 | | - "id": "allow_switching_modes", |
137 | | - "label": "Allow Switching Modes", |
138 | | - "default": true |
139 | | - }, |
140 | | - { |
141 | | - "type": "checkbox", |
142 | | - "id": "open_links_new_tab", |
143 | | - "label": "Open Links in New Tab", |
144 | | - "default": false |
145 | | - }, |
146 | | - { |
147 | | - "type": "text", |
148 | | - "id": "inline_header", |
149 | | - "label": "Inline Header" |
150 | | - }, |
151 | | - { |
152 | | - "type": "number", |
153 | | - "id": "z_index", |
154 | | - "label": "Z-Index" |
155 | | - } |
156 | | - ] |
| 78 | + "target": "section" |
157 | 79 | } |
158 | 80 | {% endschema %} |
0 commit comments