This commit is contained in:
amirhouieh 2023-05-14 18:55:53 +02:00 committed by Jinho Jang
parent 13859dc3c1
commit 0c664ddbda
7 changed files with 81 additions and 5 deletions

View File

@ -1,5 +1,20 @@
import styled from '@emotion/styled' import styled from '@emotion/styled'
import { FilterTags } from '@/components/FilterTags'
import { useSearchBarContext } from '@/context/searchbar.context'
export const NavbarFiller = styled.div` export const NavbarFiller = () => {
const { tags } = useSearchBarContext()
return (
<NavbarFillerContainer>
<FilterTags tags={tags} selectedTags={[]} />
</NavbarFillerContainer>
)
}
export const NavbarFillerContainer = styled.div`
height: var(--lpe-nav-rendered-height); height: var(--lpe-nav-rendered-height);
margin: auto;
width: 100%;
text-align: center;
display: flex;
justify-content: center;
` `

View File

@ -7,6 +7,8 @@ type SearchBarContext = SearchbarProps & {
setResultsNumber: (resultsNumber: number | null) => void setResultsNumber: (resultsNumber: number | null) => void
resultsHelperText: string | null resultsHelperText: string | null
setResultsHelperText: (resultsHelperText: string | null) => void setResultsHelperText: (resultsHelperText: string | null) => void
tags: string[]
setTags: (tags: string[]) => void
} }
const SearchBarContext = createContext<SearchBarContext>({ const SearchBarContext = createContext<SearchBarContext>({
@ -14,6 +16,8 @@ const SearchBarContext = createContext<SearchBarContext>({
setResultsNumber: () => {}, setResultsNumber: () => {},
resultsHelperText: null, resultsHelperText: null,
setResultsHelperText: () => {}, setResultsHelperText: () => {},
tags: [],
setTags: () => {},
}) })
export const SearchBarProvider = ({ children }: any) => { export const SearchBarProvider = ({ children }: any) => {
@ -21,7 +25,7 @@ export const SearchBarProvider = ({ children }: any) => {
const [resultsHelperText, setResultsHelperText] = useState<string | null>( const [resultsHelperText, setResultsHelperText] = useState<string | null>(
null, null,
) )
const [tags, setTags] = useState<string[]>([])
return ( return (
<SearchBarContext.Provider <SearchBarContext.Provider
value={{ value={{
@ -29,6 +33,8 @@ export const SearchBarProvider = ({ children }: any) => {
setResultsNumber, setResultsNumber,
resultsHelperText, resultsHelperText,
setResultsHelperText, setResultsHelperText,
tags,
setTags,
}} }}
> >
{children} {children}

View File

@ -6,16 +6,22 @@ import { NavbarFiller } from '@/components/Navbar/NavbarFiller'
import { Searchbar } from '@/components/Searchbar' import { Searchbar } from '@/components/Searchbar'
import { Footer } from '@/components/Footer' import { Footer } from '@/components/Footer'
import { Main } from '@/components/Main' import { Main } from '@/components/Main'
import { uiConfigs } from '@/configs/ui.configs'
export default function DefaultLayout(props: PropsWithChildren<any>) { export default function DefaultLayout(props: PropsWithChildren<any>) {
const isDarkState = useIsDarkState() const isDarkState = useIsDarkState()
return ( return (
<> <>
<header> <header
style={{
textAlign: 'center',
marginBlock: `${uiConfigs.navbarRenderedHeight}px`,
}}
>
<Navbar isDark={isDarkState.get()} toggle={isDarkState.toggle} /> <Navbar isDark={isDarkState.get()} toggle={isDarkState.toggle} />
<NavbarFiller />
<Hero /> <Hero />
<NavbarFiller />
<Searchbar /> <Searchbar />
</header> </header>
<Main>{props.children}</Main> <Main>{props.children}</Main>

View File

@ -37,6 +37,10 @@ export type UnbodyGraphQlResponseGoogleDoc = UnbodyGraphQlResponse<{
GoogleDoc: UnbodyGoogleDoc[] GoogleDoc: UnbodyGoogleDoc[]
}> }>
export type UnbodyGraphQlResponseTopics = UnbodyGraphQlResponse<{
GoogleDoc: { tags: string[] }[]
}>
export type UnbodyGraphQlResponseGoogleCalendarEvent = UnbodyGraphQlResponse<{ export type UnbodyGraphQlResponseGoogleCalendarEvent = UnbodyGraphQlResponse<{
GoogleCalendarEvent: UnbodyGoogleCalendarEvent[] GoogleCalendarEvent: UnbodyGoogleCalendarEvent[]
}> }>

View File

@ -7,14 +7,23 @@ import { Section } from '@/components/Section/Section'
import { getArticleCover } from '@/utils/data.utils' import { getArticleCover } from '@/utils/data.utils'
import { FeaturedPost } from '@/components/FeaturedPost' import { FeaturedPost } from '@/components/FeaturedPost'
import { PostListLayout } from '@/types/ui.types' import { PostListLayout } from '@/types/ui.types'
import { useSearchBarContext } from '@/context/searchbar.context'
import { useEffect } from 'react'
type Props = { type Props = {
posts: PostDataProps[] posts: PostDataProps[]
featured: PostDataProps | null featured: PostDataProps | null
error: string | null error: string | null
tags: string[]
} }
export default function Home({ posts, featured }: Props) { export default function Home({ posts, featured, tags }: Props) {
const { setTags } = useSearchBarContext()
useEffect(() => {
setTags(tags)
}, [setTags, tags])
return ( return (
<> <>
{featured && ( {featured && (
@ -35,6 +44,8 @@ export const getStaticProps = async () => {
errors, errors,
} = await api.getHomepagePosts() } = await api.getHomepagePosts()
const { data: topics, errors: topicErrors } = await api.getTopics()
return { return {
props: { props: {
featured: featured featured: featured
@ -60,6 +71,7 @@ export const getStaticProps = async () => {
} }
}), }),
errors, errors,
tags: topics || [],
}, },
} }
} }

9
src/queries/getTopics.ts Normal file
View File

@ -0,0 +1,9 @@
import { GetGoogleDocQuery } from '.'
import { UnbodyGetFilters } from '@/lib/unbody/unbody.types'
const defaultArgs: UnbodyGetFilters = {}
export const getTopicsQuery = (args: UnbodyGetFilters = defaultArgs) =>
GetGoogleDocQuery(args)(`
tags
`)

View File

@ -10,6 +10,7 @@ import {
GoogleDocEnhanced, GoogleDocEnhanced,
TextBlockEnhanced, TextBlockEnhanced,
ImageBlockEnhanced, ImageBlockEnhanced,
UnbodyGraphQlResponseTopics,
} from '@/lib/unbody/unbody.types' } from '@/lib/unbody/unbody.types'
import { UnbodyGraphQl } from '@/lib/unbody/unbody-content.types' import { UnbodyGraphQl } from '@/lib/unbody/unbody-content.types'
@ -28,6 +29,7 @@ import {
} from '@/types/data.types' } from '@/types/data.types'
import { getSearchBlocksQuery } from '@/queries/searchBlocks' import { getSearchBlocksQuery } from '@/queries/searchBlocks'
import axios from 'axios' import axios from 'axios'
import { getTopicsQuery } from '@/queries/getTopics'
const { UNBODY_API_KEY, UNBODY_LPE_PROJECT_ID } = process.env const { UNBODY_API_KEY, UNBODY_LPE_PROJECT_ID } = process.env
@ -480,6 +482,28 @@ class UnbodyService extends UnbodyClient {
}) })
.catch((e) => this.handleResponse([], e)) .catch((e) => this.handleResponse([], e))
} }
getTopics = (published: boolean = true): Promise<ApiResponse<string[]>> => {
const query = getTopicsQuery(
published
? {
where: Operands.WHERE_PUBLISHED(),
}
: {},
)
return this.request<UnbodyGraphQlResponseTopics>(query)
.then(({ data }) => {
if (!data || !data.Get || !data.Get.GoogleDoc)
return this.handleResponse([], 'No data')
const topics: string[] = data.Get.GoogleDoc.flatMap((item) => item.tags)
const uniqueTopics = topics.filter(
(item, index) => topics.indexOf(item) === index,
)
return this.handleResponse(uniqueTopics)
})
.catch((e) => this.handleResponse([], e))
}
} }
const unbodyApi = new UnbodyService() const unbodyApi = new UnbodyService()