From 3d35170404b280bb70913ef41b0478ca65e00d72 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Tue, 11 Jan 2022 13:04:50 +0100
Subject: chore(widget): pass title to TopicsList as parameter

I also choose to no display the current topic in the list if the
current page is a subject.
---
 src/components/Widget/TopicsList/TopicsList.tsx | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

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

diff --git a/src/components/Widget/TopicsList/TopicsList.tsx b/src/components/Widget/TopicsList/TopicsList.tsx
index 179d129..6ddb186 100644
--- a/src/components/Widget/TopicsList/TopicsList.tsx
+++ b/src/components/Widget/TopicsList/TopicsList.tsx
@@ -1,10 +1,17 @@
 import { t } from '@lingui/macro';
 import { getAllSubjects } from '@services/graphql/queries';
 import Link from 'next/link';
+import { useRouter } from 'next/router';
 import useSWR from 'swr';
 import styles from '../Widget.module.scss';
 
-const TopicsList = () => {
+const TopicsList = ({ title }: { title: string }) => {
+  const router = useRouter();
+  const isTopic = () => router.asPath.includes('/sujet/');
+  const currentTopicSlug = isTopic()
+    ? router.asPath.replace('/sujet/', '')
+    : '';
+
   const { data, error } = useSWR('/api/subjects', getAllSubjects);
 
   if (error) return <div>{t`Failed to load.`}</div>;
@@ -15,18 +22,20 @@ const TopicsList = () => {
   );
 
   const subjects = sortedSubjects.map((subject) => {
-    return (
+    return currentTopicSlug !== subject.slug ? (
       <li key={subject.databaseId}>
         <Link href={`/sujet/${subject.slug}`}>
           <a>{subject.title}</a>
         </Link>
       </li>
+    ) : (
+      ''
     );
   });
 
   return (
     <div>
-      <h2 className={styles.title}>{t`Topics`}</h2>
+      <h2 className={styles.title}>{title}</h2>
       <ul className={styles.list}>{subjects}</ul>
     </div>
   );
-- 
cgit v1.2.3

