diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-01-07 12:33:35 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-01-07 12:34:34 +0100 |
| commit | b405ddb76f217ccf7828b422ec4c0f0e15b7dee4 (patch) | |
| tree | b6313b139f018f09d9bb88dedb7c5b3e4d84ef76 /src | |
| parent | b4e12398fbeb642c12649dcccdf4d5cdefa471e3 (diff) | |
chore: add a widget to list all blog topics
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/Widget/TopicsList/TopicsList.module.scss | 7 | ||||
| -rw-r--r-- | src/components/Widget/TopicsList/TopicsList.tsx | 35 | ||||
| -rw-r--r-- | src/components/Widget/index.tsx | 3 | ||||
| -rw-r--r-- | src/pages/blog/index.tsx | 3 | ||||
| -rw-r--r-- | src/services/graphql/queries.ts | 19 | ||||
| -rw-r--r-- | src/ts/types/app.ts | 2 | ||||
| -rw-r--r-- | src/ts/types/taxonomies.ts | 14 |
7 files changed, 77 insertions, 6 deletions
diff --git a/src/components/Widget/TopicsList/TopicsList.module.scss b/src/components/Widget/TopicsList/TopicsList.module.scss new file mode 100644 index 0000000..5f37c7a --- /dev/null +++ b/src/components/Widget/TopicsList/TopicsList.module.scss @@ -0,0 +1,7 @@ +.title { + margin: 0; +} + +.list { + margin: var(--spacing-sm) 0; +} diff --git a/src/components/Widget/TopicsList/TopicsList.tsx b/src/components/Widget/TopicsList/TopicsList.tsx new file mode 100644 index 0000000..fabd40d --- /dev/null +++ b/src/components/Widget/TopicsList/TopicsList.tsx @@ -0,0 +1,35 @@ +import { t } from '@lingui/macro'; +import { getAllSubjects } from '@services/graphql/queries'; +import Link from 'next/link'; +import useSWR from 'swr'; +import styles from './TopicsList.module.scss'; + +const TopicsList = () => { + const { data, error } = useSWR('/api/subjects', getAllSubjects); + + if (error) return <div>{t`Failed to load.`}</div>; + if (!data) return <div>{t`Loading...`}</div>; + + const sortedSubjects = [...data].sort((a, b) => + a.title.localeCompare(b.title) + ); + + const subjects = sortedSubjects.map((subject) => { + return ( + <li key={subject.databaseId}> + <Link href={`/sujet/${subject.slug}`}> + <a>{subject.title}</a> + </Link> + </li> + ); + }); + + return ( + <div> + <h2 className={styles.title}>{t`Topics`}</h2> + <ul className={styles.list}>{subjects}</ul> + </div> + ); +}; + +export default TopicsList; diff --git a/src/components/Widget/index.tsx b/src/components/Widget/index.tsx index 025b302..e4e346f 100644 --- a/src/components/Widget/index.tsx +++ b/src/components/Widget/index.tsx @@ -1,4 +1,5 @@ import RecentPosts from './RecentPosts/RecentPosts'; import ThematicsList from './ThematicsList/ThematicsList'; +import TopicsList from './TopicsList/TopicsList'; -export { RecentPosts, ThematicsList }; +export { RecentPosts, ThematicsList, TopicsList }; diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx index f6aceb6..855e6ce 100644 --- a/src/pages/blog/index.tsx +++ b/src/pages/blog/index.tsx @@ -13,7 +13,7 @@ import { Button } from '@components/Buttons'; import { getPublishedPosts } from '@services/graphql/queries'; import PostHeader from '@components/PostHeader/PostHeader'; import styles from '@styles/pages/Listing.module.scss'; -import { ThematicsList } from '@components/Widget'; +import { ThematicsList, TopicsList } from '@components/Widget'; const Blog: NextPageWithLayout<BlogPageProps> = ({ fallback }) => { const getKey = (pageIndex: number, previousData: PostsListData) => { @@ -63,6 +63,7 @@ const Blog: NextPageWithLayout<BlogPageProps> = ({ fallback }) => { </div> <aside className={styles.aside}> <ThematicsList /> + <TopicsList /> </aside> </article> </> diff --git a/src/services/graphql/queries.ts b/src/services/graphql/queries.ts index 26a7869..f5387e5 100644 --- a/src/services/graphql/queries.ts +++ b/src/services/graphql/queries.ts @@ -2,11 +2,13 @@ import { Slug } from '@ts/types/app'; import { Article, PostBy } from '@ts/types/articles'; import { AllPostsSlug, PostsList, RawPostsList } from '@ts/types/blog'; import { + AllSubjects, AllSubjectsSlug, AllThematics, AllThematicsSlug, Subject, SubjectBy, + SubjectPreview, Thematic, ThematicBy, ThematicPreview, @@ -344,6 +346,23 @@ export const getAllSubjectsSlug = async (): Promise<Slug[]> => { return response.subjects.nodes; }; +export const getAllSubjects = async (): Promise<SubjectPreview[]> => { + const query = gql` + query AllSubjects { + subjects { + nodes { + databaseId + slug + title + } + } + } + `; + + const response = await fetchApi<AllSubjects>(query, null); + return response.subjects.nodes; +}; + //============================================================================== // Thematic query //============================================================================== diff --git a/src/ts/types/app.ts b/src/ts/types/app.ts index 7220ddd..d9bd041 100644 --- a/src/ts/types/app.ts +++ b/src/ts/types/app.ts @@ -6,6 +6,7 @@ import { AllPostsSlug, RawPostsList } from './blog'; import { CommentData, CreateComment } from './comments'; import { ContactData, SendEmail } from './contact'; import { + AllSubjects, AllSubjectsSlug, AllThematics, AllThematicsSlug, @@ -41,6 +42,7 @@ export type VariablesType<T> = T extends PostBy | SubjectBy | ThematicBy export type RequestType = | AllPostsSlug + | AllSubjects | AllSubjectsSlug | AllThematics | AllThematicsSlug diff --git a/src/ts/types/taxonomies.ts b/src/ts/types/taxonomies.ts index 32e1f15..0b2d696 100644 --- a/src/ts/types/taxonomies.ts +++ b/src/ts/types/taxonomies.ts @@ -37,6 +37,12 @@ export type SubjectPreview = TaxonomyPreview & { featuredImage: Cover; }; +export type AllSubjects = { + subjects: { + nodes: SubjectPreview[]; + }; +}; + export type RawSubject = SubjectPreview & { acfSubjects: { officialWebsite: string; @@ -58,6 +64,10 @@ export type AllSubjectsSlug = { }; }; +export type SubjectProps = { + subject: Subject; +}; + //============================================================================== // Thematics //============================================================================== @@ -91,10 +101,6 @@ export type AllThematicsSlug = { }; }; -export type SubjectProps = { - subject: Subject; -}; - export type ThematicProps = { thematic: Thematic; }; |
