From 3f5f1850b17eb0f5c4cd0970c22421f85d5a2bd6 Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Fri, 1 Mar 2024 23:55:10 +0000
Subject: feature: Default server address for the bookmark and a spinner while
 its saving

---
 packages/browser-extension/src/OptionsPage.tsx | 38 ++++++++++++++++++--------
 1 file changed, 27 insertions(+), 11 deletions(-)

(limited to 'packages/browser-extension/src/OptionsPage.tsx')

diff --git a/packages/browser-extension/src/OptionsPage.tsx b/packages/browser-extension/src/OptionsPage.tsx
index e3a34bd9..11a1a76d 100644
--- a/packages/browser-extension/src/OptionsPage.tsx
+++ b/packages/browser-extension/src/OptionsPage.tsx
@@ -1,4 +1,4 @@
-import { useRef } from "react";
+import { useRef, useState } from "react";
 import usePluginSettings from "./settings";
 
 export default function OptionsPage() {
@@ -7,35 +7,51 @@ export default function OptionsPage() {
   const apiKeyRef = useRef<HTMLInputElement>(null);
   const addressRef = useRef<HTMLInputElement>(null);
 
+  const [isSaved, setIsSaved] = useState(false);
+  const [error, setError] = useState<string | null>(null);
+
   const onSave = () => {
+    if (apiKeyRef.current?.value == "") {
+      setError("API Key can't be empty");
+      return;
+    }
+    if (addressRef.current?.value == "") {
+      setError("Server addres can't be empty");
+      return;
+    }
     setSettings({
       apiKey: apiKeyRef.current?.value || "",
-      address: addressRef.current?.value || "",
+      address: addressRef.current?.value || "https://demo.hoarder.app",
     });
+    setTimeout(() => {
+      setIsSaved(false);
+    }, 2000);
+    setIsSaved(true);
   };
 
   return (
     <div className="flex flex-col space-y-2">
       <span className="text-lg">Settings</span>
       <hr />
-      <div className="flex space-x-2 pt-2">
-        <label className="m-auto h-full">API Key</label>
+      <p className="text-red-500">{error}</p>
+      <div className="flex space-x-2">
+        <label className="m-auto h-full">Server Address</label>
         <input
-          ref={apiKeyRef}
-          defaultValue={settings.apiKey}
+          ref={addressRef}
+          defaultValue={settings.address || "https://demo.hoarder.app"}
           className="h-8 flex-1 rounded-lg border border-gray-300 p-2"
         />
       </div>
-      <div className="flex space-x-2">
-        <label className="m-auto h-full">Server Address</label>
+      <div className="flex space-x-2 pt-2">
+        <label className="m-auto h-full">API Key</label>
         <input
-          ref={addressRef}
-          defaultValue={settings.address}
+          ref={apiKeyRef}
+          defaultValue={settings.apiKey}
           className="h-8 flex-1 rounded-lg border border-gray-300 p-2"
         />
       </div>
       <button className="rounded-lg border border-gray-200" onClick={onSave}>
-        Save
+        {isSaved ? "Saved!" : "Save"}
       </button>
     </div>
   );
-- 
cgit v1.2.3-70-g09d2

