Browse Source

[feat] Add category filtering

Sergio Mattei 1 year ago
parent
commit
e1afcd9516
1 changed files with 14 additions and 2 deletions
  1. 14
    2
      pages/api/getTopicList.ts

+ 14
- 2
pages/api/getTopicList.ts View File

@@ -7,7 +7,7 @@ import { DatabaseType, Error, Topic } from "../../types";
7 7
 function getTopicsInDatabase(notionResp: any): Topic[] {
8 8
   return notionResp.results.map((block: any) => ({
9 9
     blockId: block.id,
10
-    name: block.properties.Name.title[0].plain_text,
10
+    name: block.properties.Name?.title?.[0]?.plain_text ?? "Untitled",
11 11
   }));
12 12
 }
13 13
 
@@ -22,12 +22,22 @@ export default async function handler(
22 22
   });
23 23
 
24 24
   const {
25
-    query: { type },
25
+    query: { type, category },
26 26
   } = req;
27 27
   const blockId = getDatabaseId(type as DatabaseType);
28 28
   if (!blockId) {
29 29
     res.status(404).json({ status: "Not found." });
30 30
   } else {
31
+    const categoryFilter = category
32
+      ? {
33
+          filter: {
34
+            property: "Category",
35
+            select: {
36
+              equals: category,
37
+            },
38
+          },
39
+        }
40
+      : {};
31 41
     const resp = await notion.databases.query({
32 42
       database_id: blockId,
33 43
       sorts: [
@@ -36,7 +46,9 @@ export default async function handler(
36 46
           direction: "ascending",
37 47
         },
38 48
       ],
49
+      ...categoryFilter,
39 50
     });
51
+    console.log(resp);
40 52
     res.status(200).json(getTopicsInDatabase(resp));
41 53
   }
42 54
 }