diff --git a/CHANGELOG.md b/CHANGELOG.md index e2330a4..ccf9851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- Settings option to hide price. + ## [1.2.2] - 2022-10-14 ### Added diff --git a/manifest.json b/manifest.json index 65b9e25..5555f84 100644 --- a/manifest.json +++ b/manifest.json @@ -13,7 +13,21 @@ "postreleasy": "vtex publish --verbose" }, "dependencies": { - "vtex.product-context": "0.x" + "vtex.product-context": "0.x", + "vtex.apps-graphql": "3.x" + }, + "settingsSchema": { + "title": "Structured data", + "type": "object", + "access": "public", + "properties": { + "disableOffers": { + "title": "Disable Offers", + "type": "boolean", + "default": false, + "description": "Disable product offers" + } + } }, "$schema": "https://raw.githubusercontent.com/vtex/node-vtex-api/master/gen/manifest.schema" } diff --git a/react/ProductOpenGraph.tsx b/react/ProductOpenGraph.tsx index 2e986bb..b3e69d8 100644 --- a/react/ProductOpenGraph.tsx +++ b/react/ProductOpenGraph.tsx @@ -7,6 +7,8 @@ import { } from 'vtex.render-runtime' import { ProductContext, SKU } from 'vtex.product-context' +import useAppSettings from './hooks/useAppSettings' + // eslint-disable-next-line no-var declare var global: { __hostname__: string @@ -21,7 +23,6 @@ interface MetaTag { function ProductOpenGraph() { const productContext = useContext(ProductContext) as ProductContext const runtime = useRuntime() as RenderContext - const hasValue = productContext?.product if (!hasValue) { @@ -111,6 +112,7 @@ function productAvailability(selectedItem?: SKU): MetaTag { } function productPrice(selectedItem?: SKU): MetaTag | null { + const { disableOffers } = useAppSettings() const seller = selectedItem?.sellers.find( ({ commertialOffer }) => commertialOffer.AvailableQuantity > 0 ) @@ -119,6 +121,10 @@ function productPrice(selectedItem?: SKU): MetaTag | null { return null } + if (disableOffers) { + return null + } + return { property: 'product:price:amount', content: `${seller.commertialOffer.spotPrice}`, diff --git a/react/hooks/useAppSettings.ts b/react/hooks/useAppSettings.ts new file mode 100644 index 0000000..787fbd9 --- /dev/null +++ b/react/hooks/useAppSettings.ts @@ -0,0 +1,27 @@ +import { useQuery } from 'react-apollo' + +import GET_SETTINGS from '../queries/getSettings.graphql' + +const DEFAULT_DISABLE_OFFERS = false + +interface Settings { + disableOffers: boolean +} + +const useAppSettings = (): Settings => { + const { data } = useQuery(GET_SETTINGS, { ssr: false }) + + if (data?.publicSettingsForApp?.message) { + const { disableOffers } = JSON.parse(data.publicSettingsForApp.message) + + return { + disableOffers: disableOffers || DEFAULT_DISABLE_OFFERS, + } + } + + return { + disableOffers: DEFAULT_DISABLE_OFFERS, + } +} + +export default useAppSettings diff --git a/react/queries/getSettings.graphql b/react/queries/getSettings.graphql new file mode 100644 index 0000000..3552687 --- /dev/null +++ b/react/queries/getSettings.graphql @@ -0,0 +1,6 @@ +query getSettings { + publicSettingsForApp(app: "vtex.open-graph", version: "1.x") + @context(provider: "vtex.apps-graphql") { + message + } +} \ No newline at end of file