From faa733cbf4c9ebef2abc630d0e5a191d354dfd47 Mon Sep 17 00:00:00 2001 From: Vihang Patil Date: Wed, 16 Aug 2023 13:57:19 +0200 Subject: [PATCH] feat(research): added author card in search result --- apps/research/src/api/authors.ts | 33 +++++++++++++++++++ apps/research/src/api/index.ts | 1 + .../src/components/search/SearchHits.tsx | 4 --- apps/research/src/pages/articles/index.tsx | 33 +++++++++++++++++-- apps/research/src/types/contentful/article.ts | 4 +-- apps/research/src/types/contentful/author.ts | 8 ++++- 6 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 apps/research/src/api/authors.ts diff --git a/apps/research/src/api/authors.ts b/apps/research/src/api/authors.ts new file mode 100644 index 00000000..5ad2722d --- /dev/null +++ b/apps/research/src/api/authors.ts @@ -0,0 +1,33 @@ +import { gql } from 'graphql-request'; +import { contentful } from './client'; +import { AssetFragment } from './fragments'; +import { Author, AuthorCollection } from '@/types'; + +const GetAuthorByName = gql` + query GetAuthorByName($name: String!) { + authorCollection(where: { name: $name}, limit: 1) { + items { + name + title + description + profilePicture { + ...asset + } + } + } + } + ${AssetFragment} +`; + +export const getAuthorByName = async ( + name: string, +) => { + const { authorCollection } = + await contentful.request>( + GetAuthorByName, + { + name, + }, + ); + return authorCollection.items[0]; +}; diff --git a/apps/research/src/api/index.ts b/apps/research/src/api/index.ts index 162ee0bf..a857244c 100644 --- a/apps/research/src/api/index.ts +++ b/apps/research/src/api/index.ts @@ -1,4 +1,5 @@ export * from './articles'; +export * from './authors'; export * from './client'; export * from './products'; export * from './home'; diff --git a/apps/research/src/components/search/SearchHits.tsx b/apps/research/src/components/search/SearchHits.tsx index c2e0cf8e..b086b95b 100644 --- a/apps/research/src/components/search/SearchHits.tsx +++ b/apps/research/src/components/search/SearchHits.tsx @@ -21,10 +21,6 @@ interface SearchHit extends BaseHit { const SearchHits: React.FC = () => { const { results } = useHits(); - const { indexUiState, setIndexUiState } = useInstantSearch(); - - console.log(indexUiState); - if (results!.__isArtificial && results!.nbHits === 0) { return (
diff --git a/apps/research/src/pages/articles/index.tsx b/apps/research/src/pages/articles/index.tsx index 44224380..cc97f9c5 100644 --- a/apps/research/src/pages/articles/index.tsx +++ b/apps/research/src/pages/articles/index.tsx @@ -1,14 +1,19 @@ -import { Col, Row, Layout, theme } from 'antd'; +import { Col, Layout, Row, theme } from 'antd'; import { NextPageWithLayout } from 'platform-js'; import algoliasearch from 'algoliasearch/lite'; import { history } from 'instantsearch.js/es/lib/routers'; -import { InstantSearch, Configure } from 'react-instantsearch-hooks-web'; -import { SearchHits, SearchText } from '@/components'; +import { InstantSearch } from 'react-instantsearch-hooks-web'; +import { AuthorCard, SearchHits, SearchText } from '@/components'; import { NextSeo } from 'next-seo'; import { siteUsername } from '@/utils'; import styles from './styles.module.scss'; import { UiState } from 'instantsearch.js'; import { RouterProps } from 'instantsearch.js/es/middlewares'; +import { Author } from '@/types'; +import React, { useState } from 'react'; +import { useRouter } from 'next/router'; +import { getAuthorByName } from '@/api'; +import { useAsyncEffect } from 'ahooks'; const { useToken } = theme; @@ -67,6 +72,22 @@ const Articles: NextPageWithLayout = () => { }, }, }; + const [author, setAuthor] = useState(null) + const router = useRouter() + useAsyncEffect(async () => { + const authors = router.query["authors"] + if (authors) { + if (authors && typeof authors == 'string') { + console.log("authors as string", authors); + setAuthor(await getAuthorByName(authors)); + } else if (typeof authors == 'object') { + console.log("authors as array", authors); + if (authors.length == 1) { + setAuthor(await getAuthorByName(authors[0])); + } + } + } + }, [router]); return ( <> { paddingTop: 40, }} > + {author && + + + + + } diff --git a/apps/research/src/types/contentful/article.ts b/apps/research/src/types/contentful/article.ts index d32674a1..93b249cf 100644 --- a/apps/research/src/types/contentful/article.ts +++ b/apps/research/src/types/contentful/article.ts @@ -4,7 +4,7 @@ import { ISectionFields, ITagFields, } from '../generated/contentful'; -import { AuthorCollection, AuthorCompact } from './author'; +import { AuthorCompact, AuthorsCollection } from './author'; import { Asset, RichTextDocument } from './global'; import { SeoData } from './Seo'; import { RelatedArticleSummaryLinked } from './indexes'; @@ -15,7 +15,7 @@ export interface ArticleSlug extends Pick {} export interface Article extends Pick, - AuthorCollection, + AuthorsCollection, TagCollection, RecommendedArticle, RelatedArticles { diff --git a/apps/research/src/types/contentful/author.ts b/apps/research/src/types/contentful/author.ts index b5fbcc5d..ad5100d3 100644 --- a/apps/research/src/types/contentful/author.ts +++ b/apps/research/src/types/contentful/author.ts @@ -1,12 +1,18 @@ import type { IAuthorFields } from '../generated/contentful'; import { Asset } from './global'; -export interface AuthorCollection { +export interface AuthorsCollection { authorsCollection: { items: ReadonlyArray; }; } +export interface AuthorCollection { + authorCollection: { + items: ReadonlyArray; + }; +} + export interface Author extends Omit { profilePicture: Asset; }