Skip to content

Commit 7bbc0be

Browse files
densumeshskeptrunedev
authored andcommitted
feature: use app metafields for pdp questions
1 parent 77b24cf commit 7bbc0be

File tree

2 files changed

+77
-55
lines changed

2 files changed

+77
-55
lines changed

clients/trieve-shopify-extension/extensions/admin-block-pdp-questions/src/BlockExtension.tsx

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async function makeGraphQLQuery(
4545
value?: string;
4646
id?: any;
4747
appId?: string;
48+
metafieldsSetInput?: any;
4849
},
4950
) {
5051
const graphQLQuery = {
@@ -79,63 +80,81 @@ export async function updateTrievePDPQuestions(
7980
productId: string,
8081
newTrievePdpQuestions: TrievePDPQuestion[],
8182
) {
83+
const appId = await getAppId();
8284
return await makeGraphQLQuery(
83-
`mutation UpdateProductMetafield($id: ID!, $value: String!) {
84-
productUpdate(input: {
85-
id: $id
86-
metafields: [
87-
{
88-
namespace: "trieve"
89-
key: "trievePDPQuestions"
90-
value: $value
91-
type: "json"
85+
`mutation UpdateAppMetafield($metafieldsSetInput: [MetafieldsSetInput!]!) {
86+
metafieldsSet(metafields: $metafieldsSetInput) {
87+
metafields {
88+
id
89+
namespace
90+
key
9291
}
93-
]
94-
}) {
95-
product {
96-
metafield(namespace: "trieve", key: "trievePDPQuestions") {
97-
value
98-
type
92+
userErrors {
93+
field
94+
message
9995
}
10096
}
101-
userErrors {
102-
field
103-
message
104-
}
105-
}
10697
}
10798
`,
10899
{
109-
id: productId,
110-
value: JSON.stringify(newTrievePdpQuestions),
100+
metafieldsSetInput: [
101+
{
102+
namespace: "trieve",
103+
key: `trievePDPQuestions-${productId.split("/")[productId.split("/").length - 1]}`,
104+
value: JSON.stringify(newTrievePdpQuestions),
105+
type: "json",
106+
ownerId: appId.data.currentAppInstallation.id,
107+
},
108+
],
111109
},
112110
);
113111
}
114112

115-
export async function getTrievePDPQuestions(productId: string) {
113+
export async function getTrievePDPQuestions(productId: string): Promise<{
114+
data: {
115+
appInstallation: {
116+
metafield: { value: string; updatedAt: string } | null;
117+
};
118+
};
119+
}> {
116120
return await makeGraphQLQuery(
117-
`query Product($id: ID!) {
121+
`query GetProductMetafield($key: String!) {
122+
appInstallation {
123+
metafield(namespace: "trieve", key: $key) {
124+
value
125+
updatedAt
126+
}
127+
}
128+
}`,
129+
{
130+
key: `trievePDPQuestions-${productId.split("/")[productId.split("/").length - 1]}`,
131+
},
132+
);
133+
}
134+
135+
export async function getProductDetails(productId: string): Promise<{
136+
data: {
137+
product: {
138+
title: string;
139+
description: string;
140+
productType: string;
141+
};
142+
};
143+
}> {
144+
return await makeGraphQLQuery(
145+
`query GetProductDetails($id: ID!) {
118146
product(id: $id) {
119147
title
120148
description
121149
productType
122-
metafield(namespace: "trieve", key:"trievePDPQuestions") {
123-
value
124-
}
125-
variants(first: 2) {
126-
edges {
127-
node {
128-
id
129-
}
130-
}
131-
}
132150
}
133-
}
134-
`,
151+
}`,
135152
{ id: productId },
136153
);
137154
}
138155

156+
157+
139158
export async function getAppId() {
140159
return await makeGraphQLQuery(
141160
`query {
@@ -281,24 +300,25 @@ function App() {
281300

282301
getTrievePDPQuestions(productId).then((productData) => {
283302
let pdpQuestions = JSON.parse(
284-
productData.data.product.metafield?.value ?? "[]",
303+
productData.data.appInstallation.metafield?.value ?? "[]",
285304
);
286305
if (!pdpQuestions) {
287306
pdpQuestions = [];
288307
}
289-
290-
const curProductDetails = {
291-
title: productData.data.product.title,
292-
description: productData.data.product.description,
293-
productType: productData.data.product.productType,
294-
};
295-
setProductDetails(curProductDetails);
296-
if (pdpQuestions.length) {
297-
setPDPQuestions(pdpQuestions);
298-
setLoading(false);
299-
} else {
300-
generateSuggestedQuestions({ curProductDetails });
301-
}
308+
getProductDetails(productId).then((productDetails) => {
309+
const curProductDetails = {
310+
title: productDetails.data.product.title,
311+
description: productDetails.data.product.description,
312+
productType: productDetails.data.product.productType,
313+
};
314+
setProductDetails(curProductDetails);
315+
if (pdpQuestions.length) {
316+
setPDPQuestions(pdpQuestions);
317+
setLoading(false);
318+
} else {
319+
generateSuggestedQuestions({ curProductDetails });
320+
}
321+
});
302322
});
303323
}, [productId, trieveSdk]);
304324

clients/trieve-shopify-extension/extensions/global-search/blocks/inline_component.liquid

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<style>
2-
{{ block.settings.custom_css }}
2+
{{ block.settings.custom_css }}
33
</style>
44

55
<div class="trieve-inline-root"></div>
66

7-
<script type="module">
7+
<script
8+
type="module"
9+
>
810
import {renderToDiv} from 'https://cdn.trieve.ai/beta/search-component/vanilla/index.js';
911
import {addToCart, checkCartQuantity} from '{{'cart-manager.js' | asset_url}}';
1012
@@ -13,10 +15,10 @@
1315
const blockSettings = {{ block.settings | json }};
1416
const productId = {{ product.id | json }};
1517
const productTitle = {{ product.title | json }};
16-
const productMetafieldsTrieve = {{ product.metafields.trieve | json }};
18+
const productMetafieldsTrieve = {{ app.metafields.trieve | json }}[`trievePDPQuestions-${productId}`];
1719
let trievePDPQuestions = null;
18-
if (productMetafieldsTrieve && Object.keys(productMetafieldsTrieve).includes("trievePDPQuestions")) {
19-
trievePDPQuestions = productMetafieldsTrieve.trievePDPQuestions;
20+
if (productMetafieldsTrieve) {
21+
trievePDPQuestions = productMetafieldsTrieve;
2022
}
2123
2224
let defaultAiQuestions = {{ block.settings.default_ai_questions | json }};

0 commit comments

Comments
 (0)