Unverified Commit ad61e841 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Update SFCC example to use TypeScript + `@next/font` (#42865)

This is now ready for review.
Co-authored-by: 's avatarHassan El Mghari <hassan4709@gmail.com>
parent 9faba368
...@@ -169,12 +169,13 @@ In the example below, we use the font `Inter` from `@next/font/google` (You can ...@@ -169,12 +169,13 @@ In the example below, we use the font `Inter` from `@next/font/google` (You can
import { Inter } from '@next/font/google' import { Inter } from '@next/font/google'
const inter = Inter({ const inter = Inter({
subsets: ['latin'],
variable: '--font-inter', variable: '--font-inter',
}) })
export default function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }) {
return ( return (
<main className={inter.variable}> <main className={`${inter.variable} font-sans`}>
<Component {...pageProps} /> <Component {...pageProps} />
</main> </main>
) )
......
export default function Footer() {
return (
<footer className="center mt-5 flex justify-center space-x-4 bg-[#E7E8EF] p-4 text-xs">
<p>Powered by Next.js, Salesforce Commerce Cloud, and Vercel </p>
<span>|</span>
<a
href="https://github.com/vercel/next.js/tree/canary/examples/with-sfcc"
className="font-medium text-orange-600"
>
Source code
</a>
</footer>
)
}
import Image from 'next/image' import Image from 'next/image'
import img from '../public/hero.jpg'
export default function Header({ scrollHandler }) { export default function Header({ scrollHandler }) {
return ( return (
...@@ -11,7 +12,8 @@ export default function Header({ scrollHandler }) { ...@@ -11,7 +12,8 @@ export default function Header({ scrollHandler }) {
priority priority
fill fill
className="h-full w-full object-cover" className="h-full w-full object-cover"
src="/hero.jpg" src={img}
placeholder="blur"
alt="Coffee grinder" alt="Coffee grinder"
/> />
<div className="absolute inset-0 bg-orange-100 mix-blend-multiply" /> <div className="absolute inset-0 bg-orange-100 mix-blend-multiply" />
......
...@@ -13,7 +13,7 @@ export default function ProductCard({ product }) { ...@@ -13,7 +13,7 @@ export default function ProductCard({ product }) {
<Link href={`/products/${product.id}`} className="group"> <Link href={`/products/${product.id}`} className="group">
<div className="aspect-w-1 aspect-h-1 w-full overflow-hidden rounded-lg bg-gray-200 xl:aspect-w-7 xl:aspect-h-8"> <div className="aspect-w-1 aspect-h-1 w-full overflow-hidden rounded-lg bg-gray-200 xl:aspect-w-7 xl:aspect-h-8">
<Image <Image
alt="" alt="product image"
src={product.imageGroups[0].images[0].link} src={product.imageGroups[0].images[0].link}
fill fill
className={cn( className={cn(
......
export default function Layout({ children }) {
return (
<>
<main>{children}</main>
<footer className="center mt-5 flex justify-center space-x-4 bg-[#E7E8EF] p-4 text-xs">
<p>Powered by Next.js, Salesforce Commerce Cloud, and Vercel </p>
<span>|</span>
<a
href="https://github.com/vercel/next.js/tree/canary/examples/with-sfcc"
className="font-medium text-orange-600"
>
Source code
</a>
</footer>
</>
)
}
module.exports = { module.exports = {
reactStrictMode: true, reactStrictMode: true,
images: { images: {
domains: ['zzte-003.sandbox.us02.dx.commercecloud.salesforce.com'], domains: ['zzte-003.dx.commercecloud.salesforce.com'],
}, },
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@next/font": "latest",
"commerce-sdk": "^2.8.0", "commerce-sdk": "^2.8.0",
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.2.0",
...@@ -13,11 +14,11 @@ ...@@ -13,11 +14,11 @@
}, },
"devDependencies": { "devDependencies": {
"@tailwindcss/aspect-ratio": "^0.4.0", "@tailwindcss/aspect-ratio": "^0.4.0",
"@types/node": "17.0.4", "@types/node": "18.11.9",
"@types/react": "17.0.38", "@types/react": "18.0.25",
"autoprefixer": "^10.4.0", "autoprefixer": "^10.4.0",
"postcss": "^8.4.5", "postcss": "^8.4.5",
"tailwindcss": "^3.0.7", "tailwindcss": "^3.0.7",
"typescript": "4.5.4" "typescript": "4.8.4"
} }
} }
import '../styles/globals.css' import '../styles/globals.css'
import Layout from '../components/layout' import Footer from '../components/Footer'
import Head from 'next/head' import Head from 'next/head'
import { Montserrat } from '@next/font/google'
function MyApp({ Component, pageProps }) { const montserrat = Montserrat({
subsets: ['latin'],
variable: '--font-montserrat',
})
export default function MyApp({ Component, pageProps }) {
return ( return (
<> <>
<Head> <Head>
<title>The Coffee House</title> <title>The Coffee House</title>
</Head> </Head>
<Layout> <main className={`${montserrat.variable} font-sans`}>
<Component {...pageProps} /> <Component {...pageProps} />
</Layout> <Footer />
</main>
</> </>
) )
} }
export default MyApp
...@@ -8,12 +8,7 @@ export default function Document() { ...@@ -8,12 +8,7 @@ export default function Document() {
name="description" name="description"
content="The premiere coffee delivery service." content="The premiere coffee delivery service."
/> />
<link
href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"
rel="stylesheet"
/>
</Head> </Head>
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
......
...@@ -4,10 +4,11 @@ import ProductCard from '../components/ProductCard' ...@@ -4,10 +4,11 @@ import ProductCard from '../components/ProductCard'
import getProducts from '../sfcc.js' import getProducts from '../sfcc.js'
export default function Gallery({ data }) { export default function Gallery({ data }) {
let coffeeRef = useRef() let coffeeRef = useRef<HTMLParagraphElement>()
const scrollHandler = (e) => { const scrollHandler = (e) => {
e.preventDefault() e.preventDefault()
// @ts-ignore
coffeeRef.scrollIntoView({ coffeeRef.scrollIntoView({
behavior: 'smooth', behavior: 'smooth',
block: 'start', block: 'start',
...@@ -22,7 +23,7 @@ export default function Gallery({ data }) { ...@@ -22,7 +23,7 @@ export default function Gallery({ data }) {
<div className="text-center"> <div className="text-center">
<p <p
className="mt-1 text-4xl font-bold uppercase text-gray-900 sm:text-5xl sm:tracking-tight lg:text-5xl" className="mt-1 text-4xl font-bold uppercase text-gray-900 sm:text-5xl sm:tracking-tight lg:text-5xl"
ref={(element) => (coffeeRef = element)} ref={coffeeRef}
> >
Crafted by us, for you Crafted by us, for you
</p> </p>
......
...@@ -33,9 +33,8 @@ export default function Product({ product }) { ...@@ -33,9 +33,8 @@ export default function Product({ product }) {
export async function getStaticProps({ params }) { export async function getStaticProps({ params }) {
const searchResults = await getProducts(params.slug) const searchResults = await getProducts(params.slug)
console.log('search Results are: ', searchResults)
const coffeeProduct = searchResults[0] const coffeeProduct = searchResults[0]
return { return {
props: { props: {
product: coffeeProduct, product: coffeeProduct,
...@@ -44,11 +43,9 @@ export async function getStaticProps({ params }) { ...@@ -44,11 +43,9 @@ export async function getStaticProps({ params }) {
} }
export async function getStaticPaths() { export async function getStaticPaths() {
const searchResults = await getProducts('coffee') const coffeeProducts = await getProducts('coffee')
let coffeeProducts = searchResults
let fullPaths = [] let fullPaths = []
for (let product of coffeeProducts) { for (let product of coffeeProducts) {
fullPaths.push({ params: { slug: product.id } }) fullPaths.push({ params: { slug: product.id } })
} }
......
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
* {
font-family: Montserrat, sans-serif;
}
/** @type {import('tailwindcss').Config} \*/
module.exports = { module.exports = {
content: [ content: [
'./pages/**/*.{js,ts,jsx,tsx}', './pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}',
], ],
theme: {
fontFamily: {
sans: ['var(--font-montserrat)'],
},
},
future: {
hoverOnlyWhenSupported: true,
},
plugins: [require('@tailwindcss/aspect-ratio')], plugins: [require('@tailwindcss/aspect-ratio')],
} }
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment