forked from crowetic/commerce
Adding Features APO
This commit is contained in:
parent
b7919ed0de
commit
c620355448
@ -4,8 +4,6 @@ import Link from 'next/link'
|
|||||||
import type { Product } from '@commerce/types'
|
import type { Product } from '@commerce/types'
|
||||||
import s from './ProductCard.module.css'
|
import s from './ProductCard.module.css'
|
||||||
import Image, { ImageProps } from 'next/image'
|
import Image, { ImageProps } from 'next/image'
|
||||||
import frameworkConfig from '@framework/config.json'
|
|
||||||
const isWishlistEnabled = !!frameworkConfig.features.wishlist
|
|
||||||
import WishlistButton from '@components/wishlist/WishlistButton'
|
import WishlistButton from '@components/wishlist/WishlistButton'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -13,6 +11,7 @@ interface Props {
|
|||||||
product: Product
|
product: Product
|
||||||
variant?: 'slim' | 'simple'
|
variant?: 'slim' | 'simple'
|
||||||
imgProps?: Omit<ImageProps, 'src'>
|
imgProps?: Omit<ImageProps, 'src'>
|
||||||
|
wishlist?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const placeholderImg = '/product-img-placeholder.svg'
|
const placeholderImg = '/product-img-placeholder.svg'
|
||||||
@ -22,6 +21,7 @@ const ProductCard: FC<Props> = ({
|
|||||||
product,
|
product,
|
||||||
variant,
|
variant,
|
||||||
imgProps,
|
imgProps,
|
||||||
|
wishlist = false,
|
||||||
...props
|
...props
|
||||||
}) => (
|
}) => (
|
||||||
<Link href={`/product/${product.slug}`} {...props}>
|
<Link href={`/product/${product.slug}`} {...props}>
|
||||||
@ -59,7 +59,7 @@ const ProductCard: FC<Props> = ({
|
|||||||
{product.price.currencyCode}
|
{product.price.currencyCode}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{isWishlistEnabled && (
|
{wishlist && (
|
||||||
<WishlistButton
|
<WishlistButton
|
||||||
className={s.wishlistButton}
|
className={s.wishlistButton}
|
||||||
productId={product.id}
|
productId={product.id}
|
||||||
|
@ -13,15 +13,16 @@ import usePrice from '@framework/product/use-price'
|
|||||||
import { useAddItem } from '@framework/cart'
|
import { useAddItem } from '@framework/cart'
|
||||||
|
|
||||||
import { getVariant, SelectedOptions } from '../helpers'
|
import { getVariant, SelectedOptions } from '../helpers'
|
||||||
// import WishlistButton from '@components/wishlist/WishlistButton'
|
import WishlistButton from '@components/wishlist/WishlistButton'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string
|
className?: string
|
||||||
children?: any
|
children?: any
|
||||||
product: Product
|
product: Product
|
||||||
|
wishlist?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const ProductView: FC<Props> = ({ product }) => {
|
const ProductView: FC<Props> = ({ product, wishlist = false }) => {
|
||||||
const addItem = useAddItem()
|
const addItem = useAddItem()
|
||||||
const { price } = usePrice({
|
const { price } = usePrice({
|
||||||
amount: product.price.value,
|
amount: product.price.value,
|
||||||
@ -151,11 +152,13 @@ const ProductView: FC<Props> = ({ product }) => {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/* <WishlistButton
|
{wishlist && (
|
||||||
className={s.wishlistButton}
|
<WishlistButton
|
||||||
productId={product.id}
|
className={s.wishlistButton}
|
||||||
variant={product.variants[0]!}
|
productId={product.id}
|
||||||
/> */}
|
variant={product.variants[0]!}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</Container>
|
</Container>
|
||||||
)
|
)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"features": {
|
"features": {
|
||||||
"wishlist": false
|
"wishlist": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,10 @@ import type { Wishlist as BCWishlist } from '@framework/api/wishlist'
|
|||||||
import type { Customer as BCCustomer } from '@framework/api/customers'
|
import type { Customer as BCCustomer } from '@framework/api/customers'
|
||||||
import type { SearchProductsData as BCSearchProductsData } from '@framework/api/catalog/products'
|
import type { SearchProductsData as BCSearchProductsData } from '@framework/api/catalog/products'
|
||||||
|
|
||||||
|
export type CommerceProviderConfig = {
|
||||||
|
features: Record<string, boolean>
|
||||||
|
}
|
||||||
|
|
||||||
export type Discount = {
|
export type Discount = {
|
||||||
// The value of the discount, can be an amount or percentage
|
// The value of the discount, can be an amount or percentage
|
||||||
value: number
|
value: number
|
||||||
|
37
framework/commerce/utils/features.ts
Normal file
37
framework/commerce/utils/features.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import commerceProviderConfig from '@framework/config.json'
|
||||||
|
import type { CommerceProviderConfig } from '../types'
|
||||||
|
import memo from 'lodash.memoize'
|
||||||
|
|
||||||
|
type FeaturesAPI = {
|
||||||
|
isEnabled: (desideredFeature: string) => boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFeatureEnabled(config: CommerceProviderConfig) {
|
||||||
|
const features = config.features
|
||||||
|
return (desideredFeature: string) =>
|
||||||
|
Object.keys(features)
|
||||||
|
.filter((k) => features[k])
|
||||||
|
.includes(desideredFeature)
|
||||||
|
}
|
||||||
|
|
||||||
|
function boostrap(): FeaturesAPI {
|
||||||
|
const basis = {
|
||||||
|
isEnabled: () => false,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!commerceProviderConfig) {
|
||||||
|
console.log('No config.json found - Please add a config.json')
|
||||||
|
return basis
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commerceProviderConfig.features) {
|
||||||
|
return {
|
||||||
|
...basis,
|
||||||
|
isEnabled: memo(isFeatureEnabled(commerceProviderConfig)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return basis
|
||||||
|
}
|
||||||
|
|
||||||
|
export default boostrap()
|
@ -21,6 +21,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@reach/portal": "^0.11.2",
|
"@reach/portal": "^0.11.2",
|
||||||
|
"@types/lodash.memoize": "^4.1.6",
|
||||||
"@vercel/fetch": "^6.1.0",
|
"@vercel/fetch": "^6.1.0",
|
||||||
"body-scroll-lock": "^3.1.5",
|
"body-scroll-lock": "^3.1.5",
|
||||||
"bowser": "^2.11.0",
|
"bowser": "^2.11.0",
|
||||||
@ -32,6 +33,7 @@
|
|||||||
"js-cookie": "^2.2.1",
|
"js-cookie": "^2.2.1",
|
||||||
"keen-slider": "^5.2.4",
|
"keen-slider": "^5.2.4",
|
||||||
"lodash.debounce": "^4.0.8",
|
"lodash.debounce": "^4.0.8",
|
||||||
|
"lodash.memoize": "^4.1.2",
|
||||||
"lodash.random": "^3.2.0",
|
"lodash.random": "^3.2.0",
|
||||||
"lodash.throttle": "^4.1.1",
|
"lodash.throttle": "^4.1.1",
|
||||||
"next": "^10.0.7-canary.3",
|
"next": "^10.0.7-canary.3",
|
||||||
|
@ -8,6 +8,7 @@ import { getConfig } from '@framework/api'
|
|||||||
import getAllProducts from '@framework/product/get-all-products'
|
import getAllProducts from '@framework/product/get-all-products'
|
||||||
import getSiteInfo from '@framework/common/get-site-info'
|
import getSiteInfo from '@framework/common/get-site-info'
|
||||||
import getAllPages from '@framework/common/get-all-pages'
|
import getAllPages from '@framework/common/get-all-pages'
|
||||||
|
import Features from '@commerce/utils/features'
|
||||||
|
|
||||||
export async function getStaticProps({
|
export async function getStaticProps({
|
||||||
preview,
|
preview,
|
||||||
@ -23,6 +24,7 @@ export async function getStaticProps({
|
|||||||
|
|
||||||
const { categories, brands } = await getSiteInfo({ config, preview })
|
const { categories, brands } = await getSiteInfo({ config, preview })
|
||||||
const { pages } = await getAllPages({ config, preview })
|
const { pages } = await getAllPages({ config, preview })
|
||||||
|
const isWishlistEnabled = Features.isEnabled('wishlist')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
@ -30,6 +32,9 @@ export async function getStaticProps({
|
|||||||
categories,
|
categories,
|
||||||
brands,
|
brands,
|
||||||
pages,
|
pages,
|
||||||
|
commerceFeatures: {
|
||||||
|
wishlist: isWishlistEnabled,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
revalidate: 14400,
|
revalidate: 14400,
|
||||||
}
|
}
|
||||||
@ -39,6 +44,7 @@ export default function Home({
|
|||||||
products,
|
products,
|
||||||
brands,
|
brands,
|
||||||
categories,
|
categories,
|
||||||
|
commerceFeatures,
|
||||||
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -51,6 +57,7 @@ export default function Home({
|
|||||||
width: i === 0 ? 1080 : 540,
|
width: i === 0 ? 1080 : 540,
|
||||||
height: i === 0 ? 1080 : 540,
|
height: i === 0 ? 1080 : 540,
|
||||||
}}
|
}}
|
||||||
|
wishlist={commerceFeatures.wishlist}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</Grid>
|
</Grid>
|
||||||
@ -64,6 +71,7 @@ export default function Home({
|
|||||||
width: 320,
|
width: 320,
|
||||||
height: 320,
|
height: 320,
|
||||||
}}
|
}}
|
||||||
|
wishlist={commerceFeatures.wishlist}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</Marquee>
|
</Marquee>
|
||||||
@ -86,6 +94,7 @@ export default function Home({
|
|||||||
width: i === 0 ? 1080 : 540,
|
width: i === 0 ? 1080 : 540,
|
||||||
height: i === 0 ? 1080 : 540,
|
height: i === 0 ? 1080 : 540,
|
||||||
}}
|
}}
|
||||||
|
wishlist={commerceFeatures.wishlist}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</Grid>
|
</Grid>
|
||||||
@ -99,6 +108,7 @@ export default function Home({
|
|||||||
width: 320,
|
width: 320,
|
||||||
height: 320,
|
height: 320,
|
||||||
}}
|
}}
|
||||||
|
wishlist={commerceFeatures.wishlist}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</Marquee>
|
</Marquee>
|
||||||
|
@ -11,14 +11,15 @@ import { getConfig } from '@framework/api'
|
|||||||
import getProduct from '@framework/product/get-product'
|
import getProduct from '@framework/product/get-product'
|
||||||
import getAllPages from '@framework/common/get-all-pages'
|
import getAllPages from '@framework/common/get-all-pages'
|
||||||
import getAllProductPaths from '@framework/product/get-all-product-paths'
|
import getAllProductPaths from '@framework/product/get-all-product-paths'
|
||||||
|
import Features from '@commerce/utils/features'
|
||||||
|
|
||||||
export async function getStaticProps({
|
export async function getStaticProps({
|
||||||
params,
|
params,
|
||||||
locale,
|
locale,
|
||||||
preview,
|
preview,
|
||||||
}: GetStaticPropsContext<{ slug: string }>) {
|
}: GetStaticPropsContext<{ slug: string }>) {
|
||||||
|
const isWishlistEnabled = Features.isEnabled('wishlist')
|
||||||
const config = getConfig({ locale })
|
const config = getConfig({ locale })
|
||||||
|
|
||||||
const { pages } = await getAllPages({ config, preview })
|
const { pages } = await getAllPages({ config, preview })
|
||||||
const { product } = await getProduct({
|
const { product } = await getProduct({
|
||||||
variables: { slug: params!.slug },
|
variables: { slug: params!.slug },
|
||||||
@ -31,7 +32,13 @@ export async function getStaticProps({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: { pages, product },
|
props: {
|
||||||
|
pages,
|
||||||
|
product,
|
||||||
|
commerceFeatures: {
|
||||||
|
wishlist: isWishlistEnabled,
|
||||||
|
},
|
||||||
|
},
|
||||||
revalidate: 200,
|
revalidate: 200,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,13 +62,17 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {
|
|||||||
|
|
||||||
export default function Slug({
|
export default function Slug({
|
||||||
product,
|
product,
|
||||||
|
commerceFeatures,
|
||||||
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
return router.isFallback ? (
|
return router.isFallback ? (
|
||||||
<h1>Loading...</h1> // TODO (BC) Add Skeleton Views
|
<h1>Loading...</h1> // TODO (BC) Add Skeleton Views
|
||||||
) : (
|
) : (
|
||||||
<ProductView product={product as any} />
|
<ProductView
|
||||||
|
product={product as any}
|
||||||
|
wishlist={commerceFeatures.wishlist}
|
||||||
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
yarn.lock
12
yarn.lock
@ -1060,6 +1060,13 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
"@types/lodash" "*"
|
"@types/lodash" "*"
|
||||||
|
|
||||||
|
"@types/lodash.memoize@^4.1.6":
|
||||||
|
version "4.1.6"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/lodash.memoize/-/lodash.memoize-4.1.6.tgz#3221f981790a415cab1a239f25c17efd8b604c23"
|
||||||
|
integrity sha512-mYxjKiKzRadRJVClLKxS4wb3Iy9kzwJ1CkbyKiadVxejnswnRByyofmPMscFKscmYpl36BEEhCMPuWhA1R/1ZQ==
|
||||||
|
dependencies:
|
||||||
|
"@types/lodash" "*"
|
||||||
|
|
||||||
"@types/lodash.random@^3.2.6":
|
"@types/lodash.random@^3.2.6":
|
||||||
version "3.2.6"
|
version "3.2.6"
|
||||||
resolved "https://registry.yarnpkg.com/@types/lodash.random/-/lodash.random-3.2.6.tgz#64b08abad168dca39c778ed40cce75b2f9e168eb"
|
resolved "https://registry.yarnpkg.com/@types/lodash.random/-/lodash.random-3.2.6.tgz#64b08abad168dca39c778ed40cce75b2f9e168eb"
|
||||||
@ -4232,6 +4239,11 @@ lodash.isstring@^4.0.1:
|
|||||||
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
|
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
|
||||||
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
|
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
|
||||||
|
|
||||||
|
lodash.memoize@^4.1.2:
|
||||||
|
version "4.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
|
||||||
|
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
|
||||||
|
|
||||||
lodash.once@^4.0.0:
|
lodash.once@^4.0.0:
|
||||||
version "4.1.1"
|
version "4.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
|
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user