From e40a18132119a90d9b4bf035ecf741b930342688 Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Wed, 13 Mar 2024 16:03:13 +0000
Subject: mobile: Add support for manually adding bookmarks

---
 packages/mobile/app/dashboard/add-note.tsx | 53 ++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 packages/mobile/app/dashboard/add-note.tsx

(limited to 'packages/mobile/app/dashboard/add-note.tsx')

diff --git a/packages/mobile/app/dashboard/add-note.tsx b/packages/mobile/app/dashboard/add-note.tsx
new file mode 100644
index 00000000..cf775a15
--- /dev/null
+++ b/packages/mobile/app/dashboard/add-note.tsx
@@ -0,0 +1,53 @@
+import { useRouter } from "expo-router";
+import { useState } from "react";
+import { View, Text } from "react-native";
+
+import { Button } from "@/components/ui/Button";
+import { Input } from "@/components/ui/Input";
+import { api } from "@/lib/trpc";
+
+export default function AddNote() {
+  const [text, setText] = useState("");
+  const [error, setError] = useState<string | undefined>();
+  const router = useRouter();
+  const invalidateAllBookmarks =
+    api.useUtils().bookmarks.getBookmarks.invalidate;
+
+  const { mutate } = api.bookmarks.createBookmark.useMutation({
+    onSuccess: () => {
+      invalidateAllBookmarks();
+      if (router.canGoBack()) {
+        router.replace("../");
+      } else {
+        router.replace("dashboard");
+      }
+    },
+    onError: (e) => {
+      let message;
+      if (e.data?.code === "BAD_REQUEST") {
+        const error = JSON.parse(e.message)[0];
+        message = error.message;
+      } else {
+        message = `Something went wrong: ${e.message}`;
+      }
+      setError(message);
+    },
+  });
+
+  return (
+    <View className="flex gap-2 p-4">
+      {error && (
+        <Text className="w-full text-center text-red-500">{error}</Text>
+      )}
+      <Input
+        className="bg-white"
+        value={text}
+        onChangeText={setText}
+        multiline
+        placeholder="What's on your mind?"
+        autoFocus
+      />
+      <Button onPress={() => mutate({ type: "text", text })} label="Add Note" />
+    </View>
+  );
+}
-- 
cgit v1.2.3-70-g09d2

