Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added settings option to hide price #41

Merged
merged 4 commits into from
Feb 9, 2024
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
8 changes: 7 additions & 1 deletion react/ProductOpenGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
} 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
Expand All @@ -21,7 +23,6 @@
function ProductOpenGraph() {
const productContext = useContext(ProductContext) as ProductContext
const runtime = useRuntime() as RenderContext

const hasValue = productContext?.product

if (!hasValue) {
Expand Down Expand Up @@ -111,6 +112,7 @@
}

function productPrice(selectedItem?: SKU): MetaTag | null {
const { disableOffers } = useAppSettings()

Check failure on line 115 in react/ProductOpenGraph.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook "useAppSettings" is called in function "productPrice" that is neither a React function component nor a custom React Hook function
const seller = selectedItem?.sellers.find(
({ commertialOffer }) => commertialOffer.AvailableQuantity > 0
)
Expand All @@ -119,6 +121,10 @@
return null
}

if (disableOffers) {
return null
}

return {
property: 'product:price:amount',
content: `${seller.commertialOffer.spotPrice}`,
Expand Down
27 changes: 27 additions & 0 deletions react/hooks/useAppSettings.ts
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions react/queries/getSettings.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query getSettings {
publicSettingsForApp(app: "vtex.open-graph", version: "1.x")
@context(provider: "vtex.apps-graphql") {
message
}
}
Loading