mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
fix to category search
This commit is contained in:
parent
3114388069
commit
00227d8bf0
@ -1,6 +1,7 @@
|
|||||||
import React, { FC } from 'react'
|
import React, { FC, useEffect } from 'react'
|
||||||
import Search from '@components/search'
|
import Search from '@components/search'
|
||||||
import { ModuleWithInit } from '@agility/nextjs'
|
import { ModuleWithInit } from '@agility/nextjs'
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
|
|
||||||
interface ICustomData {
|
interface ICustomData {
|
||||||
categories: any
|
categories: any
|
||||||
|
@ -95,13 +95,16 @@ const Layout: FC<Props> = (props) => {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
children,
|
children,
|
||||||
pageProps: { agilityProps, categories = [], ...pageProps },
|
pageProps: { agilityProps, ...pageProps },
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
|
|
||||||
const { acceptedCookies, onAcceptCookies } = useAcceptCookies()
|
const { acceptedCookies, onAcceptCookies } = useAcceptCookies()
|
||||||
const { locale = 'en-US' } = useRouter()
|
const { locale = 'en-US' } = useRouter()
|
||||||
const navBarlinks = categories.slice(0, 2).map((c) => ({
|
|
||||||
|
const categories = agilityProps?.globalData?.sitedata?.categoryLinks || []
|
||||||
|
|
||||||
|
const navBarlinks = categories.slice(0, 2).map((c:any) => ({
|
||||||
label: c.name,
|
label: c.name,
|
||||||
href: `/search/${c.slug}`,
|
href: `/search/${c.slug}`,
|
||||||
}))
|
}))
|
||||||
|
@ -42,10 +42,13 @@ export default function Search({ categories, brands }: SearchPropsType) {
|
|||||||
|
|
||||||
const { pathname, category, brand } = useSearchMeta(asPath)
|
const { pathname, category, brand } = useSearchMeta(asPath)
|
||||||
const activeCategory = categories.find((cat: any) => cat.slug === category)
|
const activeCategory = categories.find((cat: any) => cat.slug === category)
|
||||||
|
|
||||||
|
|
||||||
const activeBrand = brands.find(
|
const activeBrand = brands.find(
|
||||||
(b: any) => getSlug(b.node.path) === `brands/${brand}`
|
(b: any) => getSlug(b.node.path) === `brands/${brand}`
|
||||||
)?.node
|
)?.node
|
||||||
|
|
||||||
|
|
||||||
const { data } = useSearch({
|
const { data } = useSearch({
|
||||||
search: typeof q === 'string' ? q : '',
|
search: typeof q === 'string' ? q : '',
|
||||||
categoryId: activeCategory?.id,
|
categoryId: activeCategory?.id,
|
||||||
|
@ -21,9 +21,11 @@ export const handler: SWRHook<SearchProductsHook> = {
|
|||||||
// Use a dummy base as we only care about the relative path
|
// Use a dummy base as we only care about the relative path
|
||||||
const url = new URL(options.url!, 'http://a')
|
const url = new URL(options.url!, 'http://a')
|
||||||
|
|
||||||
|
const catID = parseInt(`${categoryId}`)
|
||||||
|
|
||||||
if (search) url.searchParams.set('search', search)
|
if (search) url.searchParams.set('search', search)
|
||||||
if (Number.isInteger(categoryId))
|
if (Number.isInteger(catID))
|
||||||
url.searchParams.set('categoryId', String(categoryId))
|
url.searchParams.set('categoryId', String(catID))
|
||||||
if (Number.isInteger(brandId))
|
if (Number.isInteger(brandId))
|
||||||
url.searchParams.set('brandId', String(brandId))
|
url.searchParams.set('brandId', String(brandId))
|
||||||
if (sort) url.searchParams.set('sort', sort)
|
if (sort) url.searchParams.set('sort', sort)
|
||||||
@ -34,6 +36,7 @@ export const handler: SWRHook<SearchProductsHook> = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
useHook: ({ useData }) => (input = {}) => {
|
useHook: ({ useData }) => (input = {}) => {
|
||||||
|
|
||||||
return useData({
|
return useData({
|
||||||
input: [
|
input: [
|
||||||
['search', input.search],
|
['search', input.search],
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Fetcher } from '@commerce/utils/types'
|
import { Fetcher } from '@commerce/utils/types'
|
||||||
|
|
||||||
export const fetcher: Fetcher = async () => {
|
export const fetcher: Fetcher = async () => {
|
||||||
console.log('FETCHER')
|
|
||||||
const res = await fetch('./data.json')
|
const res = await fetch('./data.json')
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const { data } = await res.json()
|
const { data } = await res.json()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { ComponentWithInit } from "@agility/nextjs"
|
import { ComponentWithInit } from "@agility/nextjs"
|
||||||
|
import commerce from '@lib/api/commerce'
|
||||||
|
|
||||||
interface ICustomData {
|
interface ICustomData {
|
||||||
name: any,
|
name: any,
|
||||||
@ -20,6 +21,10 @@ SiteData.getCustomInitialProps = async function ({ agility, languageCode, channe
|
|||||||
// set up links
|
// set up links
|
||||||
let links = [];
|
let links = [];
|
||||||
|
|
||||||
|
let categoryLinks = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// try to fetch our site header
|
// try to fetch our site header
|
||||||
let header = await api.getContentList({
|
let header = await api.getContentList({
|
||||||
@ -60,11 +65,27 @@ SiteData.getCustomInitialProps = async function ({ agility, languageCode, channe
|
|||||||
throw new Error(`Could not load nested sitemap: ${error}`)
|
throw new Error(`Could not load nested sitemap: ${error}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
const locale = "en-US"
|
||||||
|
const preview = false
|
||||||
|
const config = { locale, locales: [locale] }
|
||||||
|
|
||||||
|
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
|
||||||
|
const { categories, brands } = await siteInfoPromise
|
||||||
|
|
||||||
|
categoryLinks = categories
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Could not load ecommerce categories: ${error}`)
|
||||||
|
}
|
||||||
|
|
||||||
// return clean object...
|
// return clean object...
|
||||||
return {
|
return {
|
||||||
name: contentItem.fields.name,
|
name: contentItem.fields.name,
|
||||||
logo: contentItem.fields.logo,
|
logo: contentItem.fields.logo,
|
||||||
links,
|
links,
|
||||||
|
categoryLinks
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ const getCustomInitialProps = async ({ agility, channelName, languageCode }:any)
|
|||||||
const locale = "en-US"
|
const locale = "en-US"
|
||||||
const preview = false
|
const preview = false
|
||||||
const config = { locale, locales: [locale] }
|
const config = { locale, locales: [locale] }
|
||||||
const pagesPromise = commerce.getAllPages({ config, preview })
|
|
||||||
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
|
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
|
||||||
|
|
||||||
const { categories, brands } = await siteInfoPromise
|
const { categories, brands } = await siteInfoPromise
|
||||||
|
@ -20,11 +20,18 @@ export async function getStaticProps({ preview, params, locale, locales, default
|
|||||||
try {
|
try {
|
||||||
let productCode: string | null = null
|
let productCode: string | null = null
|
||||||
|
|
||||||
//check if this page is a product...
|
|
||||||
if (params?.slug.length === 2
|
if (params?.slug.length === 2) {
|
||||||
&& params?.slug[0] === "product") {
|
if (params.slug[0] === "product") {
|
||||||
productCode = params.slug[1]
|
//check if this page is a product...
|
||||||
params.slug[1] = "product-details"
|
productCode = params.slug[1]
|
||||||
|
params.slug[1] = "product-details"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((params?.slug.length || 0) >= 1 && params?.slug[0] === "search") {
|
||||||
|
//if this is a search/category page
|
||||||
|
params.slug = ["search"]
|
||||||
}
|
}
|
||||||
|
|
||||||
//add any global data accessor here
|
//add any global data accessor here
|
||||||
|
Loading…
x
Reference in New Issue
Block a user