enable publish and draft

This commit is contained in:
amirhouieh 2023-05-10 18:03:44 +02:00
parent 56afa5d3e8
commit 44afe38e25
6 changed files with 135 additions and 40 deletions

View File

@ -4,7 +4,7 @@ import { PostDataProps, PostProps } from './Post'
type Props = {
posts: PostDataProps[]
featuredPost: PostDataProps
featuredPost: PostDataProps | null
}
const PostsDemo = (props: Props) => {
@ -14,14 +14,16 @@ const PostsDemo = (props: Props) => {
<div style={{ marginBlock: '78px' }}>
{/* For Demo purposes only. Use inline CSS and styled components temporarily */}
{/*@TODO @jinho, wht PostContainer should recive an array of postData instead of only One?*/}
<PostContainer
title="Featured"
postsData={[
{
data: props.featuredPost,
},
]}
/>
{props.featuredPost && (
<PostContainer
title="Featured"
postsData={[
{
data: props.featuredPost,
},
]}
/>
)}
{posts.length > 0 ? (
<PostContainer
style={{ marginTop: '108px' }}

View File

@ -152,7 +152,7 @@ export namespace UnbodyGraphQl {
modifiedAt: string
originalName: string
path: string[]
pathstring: string
pathString: string
remoteId: string
size: number
sourceId: string
@ -298,7 +298,7 @@ export namespace UnbodyGraphQl {
export interface WhereOperandsInpObj {
operator?: WhereOperatorEnum
path: string[]
path: string[] | string
operands?: WhereOperandsInpObj[]
valueGeoRange?: WhereGeoRangeInpObj
valueNumber?: number
@ -330,7 +330,7 @@ export namespace UnbodyGraphQl {
}
export interface WhereInpObj {
path?: string[]
path?: string[] | string
valueInt?: number
valueNumber?: number
valueGeoRange?: WhereGeoRangeInpObj

View File

@ -20,6 +20,13 @@ export const getStaticProps = async ({ params }: GetStaticPropsContext) => {
}
}
const { data: article, errors } = await api.getArticlePost(remoteId as string)
if (!article) {
return {
notFound: true,
}
}
return {
props: {
data: article,

View File

@ -7,22 +7,39 @@ import { GetStaticProps } from 'next'
type Props = {
posts: PostDataProps[]
featured: PostDataProps | null
error: string | null
}
export default function Home({ posts }: Props) {
export default function Home({ posts, featured }: Props) {
return (
<>
<PostsDemo posts={posts} featuredPost={posts[0]} />
<PostsDemo posts={posts} featuredPost={featured} />
</>
)
}
export const getStaticProps = async () => {
const { data: posts, errors } = await api.getHomepagePosts()
const {
data: { posts, featured },
errors,
} = await api.getHomepagePosts()
return {
props: {
featured: featured
? {
remoteId: featured.remoteId,
date: featured.modifiedAt,
title: featured.title,
description: featured.summary,
author: 'Jinho',
tags: featured.tags,
...(featured.blocks && featured.blocks!.length > 0
? { coverImage: featured.blocks![0] as UnbodyImageBlock }
: {}),
}
: null,
posts: posts.map((post) => ({
remoteId: post.remoteId,
date: post.modifiedAt,

View File

@ -16,6 +16,7 @@ export const getHomePagePostsQuery = (args: UnbodyGetFilters = defaultArgs) =>
tags
createdAt
modifiedAt
pathString
blocks{
...on ImageBlock{
url

View File

@ -6,10 +6,13 @@ import {
UnbodyImageBlock,
UnbodyTextBlock,
UnbodyGraphQlResponseBlocks,
UnbodyGraphQlResponse,
} from '@/lib/unbody/unbody.types'
import { UnbodyGraphQl } from '@/lib/unbody/unbody-content.types'
type WhereOperandsInpObj = UnbodyGraphQl.Filters.WhereOperandsInpObj
import { getArticlePostQuery } from '@/queries/getPost'
import { getHomePagePostsQuery } from '@/queries/getPosts'
import { getAllPostsSlugQuery } from '@/queries/getPostsSlugs'
@ -35,8 +38,41 @@ type HomepagePost = Pick<
| 'blocks'
>
type HomePageData = {
posts: HomepagePost[]
featured: HomepagePost | null
}
type UnbodyDocTypes = UnbodyGoogleDoc | UnbodyImageBlock | UnbodyTextBlock
const OperandFactory = (
operator: UnbodyGraphQl.Filters.WhereOperatorEnum,
path: string | string[],
value: string,
valuePath: string,
): WhereOperandsInpObj => ({
path,
operator,
[valuePath]: value,
})
export const Operands: Record<string, (...a: any) => WhereOperandsInpObj> = {
WHERE_PUBLISHED: () =>
OperandFactory(
UnbodyGraphQl.Filters.WhereOperatorEnum.Like,
'pathString',
'*/published/*',
'valueString',
),
WHERE_ID_IS: (id) =>
OperandFactory(
UnbodyGraphQl.Filters.WhereOperatorEnum.Equal,
'remoteId',
id,
'valueString',
),
}
const mapSearchResultItem = <T extends UnbodyDocTypes>(
q: string,
tags: string[],
@ -68,13 +104,25 @@ class UnbodyService extends UnbodyClient {
}
}
getHomepagePosts = (): Promise<ApiResponse<HomepagePost[]>> => {
return this.request<UnbodyGraphQlResponseGoogleDoc>(getHomePagePostsQuery())
getHomepagePosts = (): Promise<ApiResponse<HomePageData>> => {
const query = getHomePagePostsQuery({
where: Operands.WHERE_PUBLISHED(),
})
return this.request<UnbodyGraphQlResponseGoogleDoc>(query)
.then(({ data }) => {
if (!data) return this.handleResponse([], 'No data')
return this.handleResponse(data.Get.GoogleDoc)
if (!data)
return this.handleResponse({ featured: null, posts: [] }, 'No data')
const featured =
data.Get.GoogleDoc.find((post) =>
post.pathString.includes('/featured/'),
) || null
const posts = data.Get.GoogleDoc.filter(
(post) => !post.pathString.includes('/featured/'),
)
return this.handleResponse({ featured, posts })
})
.catch((e) => this.handleResponse([], e))
.catch((e) => this.handleResponse({ featured: null, posts: [] }, e))
}
getAllArticlePostSlugs = (): Promise<ApiResponse<{ remoteId: string }[]>> => {
@ -88,13 +136,15 @@ class UnbodyService extends UnbodyClient {
getArticlePost = (
id: string,
published: boolean = true,
): Promise<ApiResponse<UnbodyGoogleDoc | null>> => {
const query = getArticlePostQuery({
where: {
path: ['remoteId'],
operator: UnbodyGraphQl.Filters.WhereOperatorEnum.Equal,
valueString: id,
},
where: published
? {
operator: UnbodyGraphQl.Filters.WhereOperatorEnum.And,
operands: [Operands.WHERE_PUBLISHED(), Operands.WHERE_ID_IS(id)],
}
: Operands.WHERE_ID_IS(id),
})
return this.request<UnbodyGraphQlResponseGoogleDoc>(query)
@ -114,6 +164,7 @@ class UnbodyService extends UnbodyClient {
serachBlocks = async (
q: string = '',
tags: string[] = [],
published: boolean = true,
): Promise<
ApiResponse<SearchResultItem<UnbodyImageBlock | UnbodyTextBlock>[]>
> => {
@ -124,12 +175,14 @@ class UnbodyService extends UnbodyClient {
concepts: [q, ...tags],
certainty: 0.75,
},
...(published
? {
where: Operands.WHERE_PUBLISHED(),
}
: {}),
}
: {}),
})
console.log(query)
return this.request<UnbodyGraphQlResponseBlocks>(query)
.then(({ data }) => {
if (!data || !(data.Get.ImageBlock || data.Get.TextBlock))
@ -147,16 +200,20 @@ class UnbodyService extends UnbodyClient {
searchArticles = (
q: string = '',
tags: string[] = [],
published: boolean = true,
): Promise<ApiResponse<SearchResultItem<UnbodyGoogleDoc>[]>> => {
const query = getSearchArticlesQuery({
...(q.trim().length > 0
? {
nearText: {
concepts: [q],
},
}
: {}),
...((tags.length > 0 && {
let queryFilters = {}
if (q.trim().length > 0) {
queryFilters = {
nearText: {
concepts: [q],
},
}
}
if (tags.length > 0) {
queryFilters = {
...queryFilters,
where: {
operator: UnbodyGraphQl.Filters.WhereOperatorEnum.And,
operands: tags.map((tag) => ({
@ -165,9 +222,20 @@ class UnbodyService extends UnbodyClient {
valueString: tag,
})),
},
}) ||
{}),
})
}
}
if (published) {
queryFilters = {
...queryFilters,
where: {
...((queryFilters as any).where || {}),
...Operands.WHERE_PUBLISHED(),
},
}
}
const query = getSearchArticlesQuery(queryFilters)
return this.request<UnbodyGraphQlResponseGoogleDoc>(query)
.then(({ data }) => {