From 543ed2a106f3bab28bc0f4b97ade00afb524b4ee Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Fri, 5 Nov 2021 11:17:42 -0300
Subject: selecting provider

---
 .../src/pages/home/SecretSelectionScreen.tsx       | 180 ++++++++++++---------
 1 file changed, 102 insertions(+), 78 deletions(-)

(limited to 'packages/anastasis-webui/src/pages/home/SecretSelectionScreen.tsx')

diff --git a/packages/anastasis-webui/src/pages/home/SecretSelectionScreen.tsx b/packages/anastasis-webui/src/pages/home/SecretSelectionScreen.tsx
index e8af75229..8aa5ed2f7 100644
--- a/packages/anastasis-webui/src/pages/home/SecretSelectionScreen.tsx
+++ b/packages/anastasis-webui/src/pages/home/SecretSelectionScreen.tsx
@@ -1,5 +1,3 @@
-/* eslint-disable @typescript-eslint/camelcase */
-import { RecoveryInternalData } from "anastasis-core";
 import { h, VNode } from "preact";
 import { useState } from "preact/hooks";
 import { AsyncButton } from "../../components/AsyncButton";
@@ -9,15 +7,12 @@ import { AnastasisClientFrame } from "./index";
 
 export function SecretSelectionScreen(): VNode {
   const [selectingVersion, setSelectingVersion] = useState<boolean>(false);
-  const [otherProvider, setOtherProvider] = useState<string>("");
   const reducer = useAnastasisContext()
 
   const currentVersion = (reducer?.currentReducerState
     && ("recovery_document" in reducer.currentReducerState)
     && reducer.currentReducerState.recovery_document?.version) || 0;
 
-  const [otherVersion, setOtherVersion] = useState("");
-
   if (!reducer) {
     return <div>no reducer in context</div>
   }
@@ -25,7 +20,7 @@ export function SecretSelectionScreen(): VNode {
     return <div>invalid state</div>
   }
 
-  async function selectVersion(p: string, n: number): Promise<void> {
+  async function doSelectVersion(p: string, n: number): Promise<void> {
     if (!reducer) return Promise.resolve();
     return reducer.runTransaction(async (tx) => {
       await tx.transition("change_version", {
@@ -38,74 +33,20 @@ export function SecretSelectionScreen(): VNode {
 
   const providerList = Object.keys(reducer.currentReducerState.authentication_providers ?? {})
   const recoveryDocument = reducer.currentReducerState.recovery_document
-  if (!recoveryDocument) {
-    return (
-      <AnastasisClientFrame hideNext="Recovery document not found" title="Recovery: Problem">
-        <p>No recovery document found, try with another provider</p>
-        <table class="table">
-          <tr>
-            <td><b>Provider</b></td>
-            <td>
-              <select onChange={(e) => setOtherProvider((e.target as any).value)}>
-                <option key="none" disabled selected > Choose another provider </option>
-                {providerList.map(prov => (
-                  <option key={prov} value={prov}>
-                    {prov}
-                  </option>
-                ))}
-              </select>
-            </td>
-          </tr>
-        </table>
-      </AnastasisClientFrame>
-    )
-  }
-  if (selectingVersion) {
-    return (
-      <AnastasisClientFrame hideNav title="Recovery: Select secret">
-        <div class="columns">
-          <div class="column">
-            <div class="box">
-              <h1 class="subtitle">Provider {recoveryDocument.provider_url}</h1>
-              <div class="block">
-                {currentVersion === 0 ? <p>
-                  Set to recover the latest version
-                </p> : <p>
-                  Set to recover the version number {currentVersion}
-                </p>}
-                <p>Specify other version below or use the latest</p>
-              </div>
-              <div class="container">
-                <NumberInput
-                  label="Version"
-                  placeholder="version number to recover"
-                  grabFocus
-                  bind={[otherVersion, setOtherVersion]} />
-              </div>
-            </div>
-            <div style={{ marginTop: '2em', display: 'flex', justifyContent: 'space-between' }}>
-              <button class="button" onClick={() => setSelectingVersion(false)}>Cancel</button>
-              <div class="buttons">
-                <button class="button " onClick={() => selectVersion(otherProvider, 0)}>Use latest</button>
-                <AsyncButton onClick={() => selectVersion(otherProvider, parseInt(otherVersion, 10))}>Confirm</AsyncButton>
-              </div>
-            </div>
-          </div>
-          <div class="column">
-            .
-          </div>
-        </div>
 
-      </AnastasisClientFrame>
-    );
+  if (!recoveryDocument) {
+    return <ChooseAnotherProviderScreen
+      providers={providerList} selected=""
+      onChange={(newProv) => doSelectVersion(newProv, 0)}
+    />
   }
 
-  function ProviderCard({ provider, selected }: { selected?: boolean; provider: string }): VNode {
-    return <button key={provider} class="button block is-fullwidth" style={{ border: selected ? '2px solid green' : undefined }}
-      onClick={(e) => selectVersion(provider, currentVersion)}
-    >
-      {provider}
-    </button>
+  if (selectingVersion) {
+    return <SelectOtherVersionProviderScreen providers={providerList}
+      provider={recoveryDocument.provider_url} version={recoveryDocument.version}
+      onCancel={() => setSelectingVersion(false)}
+      onConfirm={doSelectVersion}
+    />
   }
 
   return (
@@ -125,13 +66,6 @@ export function SecretSelectionScreen(): VNode {
               <button class="button" onClick={(e) => setSelectingVersion(true)}>Change secret's version</button>
             </div>
           </div>
-          <p class="block">
-            Or you can use another provider
-          </p>
-          {providerList.map(prov => {
-            if (recoveryDocument.provider_url === prov) return;
-            return <ProviderCard key={prov} provider={prov} />
-          })}
         </div>
         <div class="column">
           <p>Secret found, you can select another version or continue to the challenges solving</p>
@@ -140,3 +74,93 @@ export function SecretSelectionScreen(): VNode {
     </AnastasisClientFrame>
   );
 }
+
+
+function ChooseAnotherProviderScreen({ providers, selected, onChange }: { selected: string; providers: string[]; onChange: (prov: string) => void }): VNode {
+  return (
+    <AnastasisClientFrame hideNext="Recovery document not found" title="Recovery: Problem">
+      <p>No recovery document found, try with another provider</p>
+      <div class="field">
+        <label class="label">Provider</label>
+        <div class="control is-expanded has-icons-left">
+          <div class="select is-fullwidth">
+            <select onChange={(e) => onChange(e.currentTarget.value)} value={selected}>
+              <option key="none" disabled selected value=""> Choose a provider </option>
+              {providers.map(prov => (
+                <option key={prov} value={prov}>
+                  {prov}
+                </option>
+              ))}
+            </select>
+            <div class="icon is-small is-left">
+              <i class="mdi mdi-earth" />
+            </div>
+          </div>
+        </div>
+      </div>
+    </AnastasisClientFrame>
+  );
+}
+
+function SelectOtherVersionProviderScreen({ providers, provider, version, onConfirm, onCancel }: { onCancel: () => void; provider: string; version: number; providers: string[]; onConfirm: (prov: string, v: number) => Promise<void>; }): VNode {
+  const [otherProvider, setOtherProvider] = useState<string>(provider);
+  const [otherVersion, setOtherVersion] = useState(`${version}`);
+
+  return (
+    <AnastasisClientFrame hideNav title="Recovery: Select secret">
+      <div class="columns">
+        <div class="column">
+          <div class="box">
+            <h1 class="subtitle">Provider {otherProvider}</h1>
+            <div class="block">
+              {version === 0 ? <p>
+                Set to recover the latest version
+              </p> : <p>
+                Set to recover the version number {version}
+              </p>}
+              <p>Specify other version below or use the latest</p>
+            </div>
+
+            <div class="field">
+              <label class="label">Provider</label>
+              <div class="control is-expanded has-icons-left">
+                <div class="select is-fullwidth">
+                  <select onChange={(e) => setOtherProvider(e.currentTarget.value)} value={otherProvider}>
+                    <option key="none" disabled selected value=""> Choose a provider </option>
+                    {providers.map(prov => (
+                      <option key={prov} value={prov}>
+                        {prov}
+                      </option>
+                    ))}
+                  </select>
+                  <div class="icon is-small is-left">
+                    <i class="mdi mdi-earth" />
+                  </div>
+                </div>
+              </div>
+            </div>
+            <div class="container">
+              <NumberInput
+                label="Version"
+                placeholder="version number to recover"
+                grabFocus
+                bind={[otherVersion, setOtherVersion]} />
+            </div>
+          </div>
+          <div style={{ marginTop: '2em', display: 'flex', justifyContent: 'space-between' }}>
+            <button class="button" onClick={onCancel}>Cancel</button>
+            <div class="buttons">
+              <AsyncButton class="button" onClick={() => onConfirm(otherProvider, 0)}>Use latest</AsyncButton>
+              <AsyncButton class="button is-info" onClick={() => onConfirm(otherProvider, parseInt(otherVersion, 10))}>Confirm</AsyncButton>
+            </div>
+          </div>
+        </div>
+        <div class="column">
+          .
+        </div>
+      </div>
+
+    </AnastasisClientFrame>
+  );
+
+}
-- 
cgit v1.2.3

