From b405ddb76f217ccf7828b422ec4c0f0e15b7dee4 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 7 Jan 2022 12:33:35 +0100
Subject: chore: add a widget to list all blog topics

---
 src/components/Widget/TopicsList/TopicsList.tsx | 35 +++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 src/components/Widget/TopicsList/TopicsList.tsx

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

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;
-- 
cgit v1.2.3

