fix blank article page on navigation from search

This commit is contained in:
amirhouieh 2023-05-16 10:40:12 +02:00
parent 7e203e184d
commit e7a621b917
3 changed files with 22 additions and 5 deletions

View File

@ -1,6 +1,7 @@
// context for searchbar
import { SearchbarProps } from '@/components/Searchbar/Searchbar'
import { createContext, useContext, useState } from 'react'
import { createContext, useContext, useEffect, useState } from 'react'
import { useRouter } from 'next/router'
type SearchBarContext = SearchbarProps & {
resultsNumber: number | null
@ -21,11 +22,25 @@ const SearchBarContext = createContext<SearchBarContext>({
})
export const SearchBarProvider = ({ children }: any) => {
const router = useRouter()
const [resultsNumber, setResultsNumber] = useState<number | null>(null)
const [resultsHelperText, setResultsHelperText] = useState<string | null>(
null,
)
const [tags, setTags] = useState<string[]>([])
useEffect(() => {
router.events.on('routeChangeStart', resetResults)
return () => {
router.events.off('routeChangeStart', resetResults)
}
}, [router])
const resetResults = () => {
setResultsNumber(null)
setResultsHelperText(null)
}
return (
<SearchBarContext.Provider
value={{

View File

@ -42,6 +42,7 @@ export async function getStaticPaths() {
export const getStaticProps = async ({ params }: GetStaticPropsContext) => {
const { slug } = params!
console.log('rendering', slug)
if (!slug) {
return {

View File

@ -287,7 +287,8 @@ class UnbodyService extends UnbodyClient {
return this.request<UnbodyGraphQlResponseGoogleDoc>(query)
.then(({ data }) => {
if (!data) return this.handleResponse([], 'No data')
if (!data)
return this.handleResponse([], 'No data for related articles')
return this.handleResponse(data.Get.GoogleDoc.map(enhanceGoogleDoc))
})
.catch((e) => this.handleResponse([], e))
@ -312,7 +313,7 @@ class UnbodyService extends UnbodyClient {
return this.request<UnbodyGraphQlResponseGoogleDoc>(query)
.then(({ data }) => {
if (!data || !data.Get || !data.Get.GoogleDoc)
return this.handleResponse([], 'No data')
return this.handleResponse([], 'No data for same authors')
return this.handleResponse(data.Get.GoogleDoc.map(enhanceGoogleDoc))
})
.catch((e) => this.handleResponse([], e))
@ -355,7 +356,7 @@ class UnbodyService extends UnbodyClient {
return this.request<UnbodyGraphQlResponseBlocks>(query)
.then(({ data }) => {
if (!data || !(data.Get.ImageBlock || data.Get.TextBlock))
return this.handleResponse([], 'No data')
return this.handleResponse([], 'No data for search blocks')
const blocks = [
...(data.Get.ImageBlock || []),
@ -415,7 +416,7 @@ class UnbodyService extends UnbodyClient {
return this.request<UnbodyGraphQlResponseBlocks>(query)
.then(({ data }) => {
if (!data || !(data.Get.ImageBlock || data.Get.TextBlock))
return this.handleResponse([], 'No data')
return this.handleResponse([], 'No data for search blocks')
const blocks = [
...(data.Get.ImageBlock || []),