From 98dd62e28793bad9c121a9f16b11a10222a41c50 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Tue, 11 Jan 2022 11:41:58 +0100
Subject: chore(widgets): pass title to thematics list as parameter

If the current page is a thematic, I also choose to not display the
current thematic in the list.
---
 src/components/Widget/ThematicsList/ThematicsList.tsx | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

(limited to 'src/components/Widget/ThematicsList/ThematicsList.tsx')

diff --git a/src/components/Widget/ThematicsList/ThematicsList.tsx b/src/components/Widget/ThematicsList/ThematicsList.tsx
index 232515d..0dc0916 100644
--- a/src/components/Widget/ThematicsList/ThematicsList.tsx
+++ b/src/components/Widget/ThematicsList/ThematicsList.tsx
@@ -1,11 +1,17 @@
 import { t } from '@lingui/macro';
 import { getAllThematics } from '@services/graphql/queries';
-import { ThematicPreview } from '@ts/types/taxonomies';
 import Link from 'next/link';
+import { useRouter } from 'next/router';
 import useSWR from 'swr';
 import styles from './ThematicsList.module.scss';
 
-const ThematicsList = () => {
+const ThematicsList = ({ title }: { title: string }) => {
+  const router = useRouter();
+  const isThematic = () => router.asPath.includes('/thematique/');
+  const currentThematicSlug = isThematic()
+    ? router.asPath.replace('/thematique/', '')
+    : '';
+
   const { data, error } = useSWR('/api/thematics', getAllThematics);
 
   if (error) return <div>{t`Failed to load.`}</div>;
@@ -16,18 +22,20 @@ const ThematicsList = () => {
   );
 
   const thematics = sortedThematics.map((thematic) => {
-    return (
+    return currentThematicSlug !== thematic.slug ? (
       <li key={thematic.databaseId}>
         <Link href={`/thematique/${thematic.slug}`}>
           <a>{thematic.title}</a>
         </Link>
       </li>
+    ) : (
+      ''
     );
   });
 
   return (
     <div>
-      <h2 className={styles.title}>{t`Thematics`}</h2>
+      <h2 className={styles.title}>{title}</h2>
       <ul className={styles.list}>{thematics}</ul>
     </div>
   );
-- 
cgit v1.2.3

