diff --git a/app/routes/api.update.tsx b/app/routes/api.update.tsx new file mode 100644 index 0000000..b10b0dd --- /dev/null +++ b/app/routes/api.update.tsx @@ -0,0 +1,45 @@ +import { json } from '@shopify/remix-oxygen' + +const METAOBJECT_UPDATE_MUTATION = `#graphql +mutation UpdateMetaobject($id: ID!, $metaobject: MetaobjectUpdateInput!) { + metaobjectUpdate(id: $id, metaobject: $metaobject) { + metaobject { + handle + } + userErrors { + field + message + code + } + } +}` + +export async function action({ request, context }) { + const body = await request.json() + const result = await fetch( + context.env.SHOPIFY_STORE_URL, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Shopify-Access-Token': + context.env.SHOPIFY_STORE_ACCESS_TOKEN, + }, + + body: JSON.stringify({ + query: METAOBJECT_UPDATE_MUTATION, + variables: { + id: body.id, + metaobject: { + fields: [{ key: 'quote', value: body.value }], + }, + }, + }), + }, + ).then((res) => res.json()) + + return json({ + status: 'success', + body: JSON.stringify(result), + }) +} diff --git a/app/routes/products.$handle/sections/reviews.jsx b/app/routes/products.$handle/sections/reviews.jsx index 3e83b47..100ff98 100644 --- a/app/routes/products.$handle/sections/reviews.jsx +++ b/app/routes/products.$handle/sections/reviews.jsx @@ -59,6 +59,7 @@ export default function Reviews() { return ( { + const newReview = prompt('New review quote') + fetch(`/api/update`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id, + value: newReview, + }), + }) + .then((res) => res.json()) + .then((r) => console.log(r)) + }, [id]) + return (
—{customer} +
) diff --git a/env.d.ts b/env.d.ts index 34a3e37..60922f3 100644 --- a/env.d.ts +++ b/env.d.ts @@ -30,6 +30,8 @@ declare global { PUBLIC_STOREFRONT_ID: string; PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID: string; PUBLIC_CUSTOMER_ACCOUNT_API_URL: string; + SHOPIFY_STORE_URL: string; + SHOPIFY_STORE_ACCESS_TOKEN: string; } }