git » hoplite.git » commit 8b2bf36

improve login

author Alan Dipert
2021-06-12 14:41:17 UTC
committer Alan Dipert
2021-06-12 14:41:17 UTC
parent 64d27e73ebe04330644cb5529d2e57144b5e0dc0

improve login

el.mjs +1 -9
hntr2.mjs +20 -6
reactives.mjs +25 -1

diff --git a/el.mjs b/el.mjs
index d534127..399def1 100644
--- a/el.mjs
+++ b/el.mjs
@@ -231,12 +231,4 @@ function entitiesBy(db, sortAttr, makeNode) {
 
 let nbsp = "\u00A0";
 
-function cond(...pairs) {
-  return formula((...pairs) => {
-    for (let i = 0; i < pairs.length-1; i+=2) {
-      if (pairs[i]) return pairs[i+1];
-    }
-  })(...pairs);
-}
-
-export { $el, entities, nbsp, cond, entitiesBy };
+export { $el, entities, nbsp, entitiesBy };
diff --git a/hntr2.mjs b/hntr2.mjs
index a049f25..e4be3b2 100644
--- a/hntr2.mjs
+++ b/hntr2.mjs
@@ -1,13 +1,17 @@
-import { input, formula, database, transaction } from './reactives.mjs';
-import { $el, nbsp, cond, entities } from './el.mjs';
+import { input, formula, cond, not, database, transaction } from './reactives.mjs';
+import { $el, nbsp, entities } from './el.mjs';
 
-let user = input();
+let user = input(),
+    loaded = input(false);
 
-firebase.auth().onAuthStateChanged(result => user.set(result));
+firebase.auth().onAuthStateChanged(result => {
+  if (result) user.set(result)
+  loaded.set(true);
+});
 
 function authed() {
   return $el.p(
-    formula(s => `Signed in as ${s?.email}`)(user),
+    formula(u => `Signed in as ${u?.email}`)(user),
     nbsp,
     $el.a({href: "#", onclick: () => {
       firebase.auth().signOut().then(() => user.set());
@@ -43,7 +47,17 @@ function login() {
 function app() {
   return $el.div(
     $el.h1("hntr2"),
-    cond(user, authed(), true, login()),
+    cond(
+      not(loaded),
+      "Loading...",
+      true,
+      cond(
+        user,
+        authed(),
+        true,
+        login()
+      )
+    )
   );
 }
 
diff --git a/reactives.mjs b/reactives.mjs
index e9a444c..b5931fb 100644
--- a/reactives.mjs
+++ b/reactives.mjs
@@ -315,7 +315,31 @@ function watch(f) {
   return (source) => new Watch(f, source);
 }
 
-export { Input, Formula, transaction, captureCreated, input, formula, database, view, watch };
+function cond(...pairs) {
+  return formula((...pairs) => {
+    for (let i = 0; i < pairs.length-1; i+=2) {
+      if (pairs[i]) return pairs[i+1];
+    }
+    return null;
+  })(...pairs);
+}
+
+function not(x) {
+  return formula(v => !v)(x);
+}
+
+function and(...xs) {
+  return formula((...xs) => {
+    for (x of xs) {
+      if (x.value) {
+        return x.value;
+      }
+    }
+    return false;
+  })(...xs)
+}
+
+export { Input, Formula, transaction, captureCreated, input, formula, database, view, watch, cond, not };
 
 // let A = input(100),
 //     B = input(200),