summaryrefslogtreecommitdiff
path: root/packages/anastasis-webui/src/routes/home/index.tsx
blob: ee3399503f213a05c1f7781f370c4bd213315f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { FunctionalComponent, h } from "preact";
import { useState } from "preact/hooks";
import {
  AnastasisReducerApi,
  useAnastasisReducer,
} from "../../hooks/use-anastasis-reducer";
import style from "./style.css";

const Home: FunctionalComponent = () => {
  const reducer = useAnastasisReducer();
  if (!reducer.currentReducerState) {
    return (
      <div class={style.home}>
        <h1>Home</h1>
        <p>
          <button onClick={() => reducer.startBackup()}>Backup</button>
          <button>Recover</button>
        </p>
      </div>
    );
  }
  console.log("state", reducer.currentReducerState);
  if (reducer.currentReducerState.backup_state === "CONTINENT_SELECTING") {
    return (
      <div class={style.home}>
        <h1>Backup: Select Continent</h1>
        <ErrorBanner reducer={reducer} />
        <div>
          {reducer.currentReducerState.continents.map((x: any) => {
            const sel = (x: string) =>
              reducer.transition("select_continent", { continent: x });
            return (
              <button onClick={() => sel(x.name)} key={x.name}>
                {x.name}
              </button>
            );
          })}
        </div>
        <div>
          <button onClick={() => reducer.back()}>Back</button>
        </div>
      </div>
    );
  }
  if (reducer.currentReducerState.backup_state === "COUNTRY_SELECTING") {
    return (
      <div class={style.home}>
        <h1>Backup: Select Continent</h1>
        <ErrorBanner reducer={reducer} />
        <div>
          {reducer.currentReducerState.countries.map((x: any) => {
            const sel = (x: any) =>
              reducer.transition("select_country", {
                country_code: x.code,
                currencies: [x.currency],
              });
            return (
              <button onClick={() => sel(x)} key={x.name}>
                {x.name} ({x.currency})
              </button>
            );
          })}
        </div>
        <div>
          <button onClick={() => reducer.back()}>Back</button>
        </div>
      </div>
    );
  }
  if (
    reducer.currentReducerState.backup_state === "USER_ATTRIBUTES_COLLECTING"
  ) {
    return <AttributeEntry reducer={reducer} />;
  }

  if (reducer.currentReducerState.backup_state === "AUTHENTICATIONS_EDITING") {
    return <AuthenticationEditor reducer={reducer} />;
  }

  console.log("unknown state", reducer.currentReducerState);
  return (
    <div class={style.home}>
      <h1>Home</h1>
      <p>Bug: Unknown state.</p>
    </div>
  );
};

export interface AuthenticationEditorProps {
  reducer: AnastasisReducerApi;
}

function AuthenticationEditor(props: AuthenticationEditorProps) {
  const { reducer } = props;
  const providers = reducer.currentReducerState.authentication_providers;
  const authAvailable = new Set<string>();
  for (const provKey of Object.keys(providers)) {
    const p = providers[provKey];
    for (const meth of p.methods) {
      authAvailable.add(meth.type);
    }
  }
  return (
    <div class={style.home}>
      <h1>Backup: Configure Authentication Methods</h1>
      <p>Auths available: {JSON.stringify(Array.from(authAvailable))}</p>
      <button>Next</button>
      <div>
        <button onClick={() => reducer.back()}>Back</button>
      </div>
    </div>
  );
}

export interface AttributeEntryProps {
  reducer: AnastasisReducerApi;
}

function AttributeEntry(props: AttributeEntryProps) {
  const reducer = props.reducer;
  const [attrs, setAttrs] = useState<Record<string, string>>({});
  return (
    <div class={style.home}>
      <h1>Backup: Enter Basic User Attributes</h1>
      <ErrorBanner reducer={reducer} />
      <div>
        {reducer.currentReducerState.required_attributes.map((x: any) => {
          return (
            <AttributeEntryField
              setValue={(v: string) => setAttrs({ ...attrs, [x.name]: v })}
              spec={x}
              value={attrs[x.name]}
            />
          );
        })}
      </div>
      <button
        onClick={() =>
          reducer.transition("enter_user_attributes", {
            identity_attributes: attrs,
          })
        }
      >
        Next
      </button>
      <div>
        <button onClick={() => reducer.back()}>Back</button>
      </div>
    </div>
  );
}

export interface AttributeEntryFieldProps {
  value: string;
  setValue: (newValue: string) => void;
  spec: any;
}

function AttributeEntryField(props: AttributeEntryFieldProps) {
  return (
    <div>
      <label>{props.spec.label}</label>
      <input
        type="text"
        value={props.value}
        onChange={(e) => props.setValue((e as any).target.value)}
      />
    </div>
  );
}

interface ErrorBannerProps {
  reducer: AnastasisReducerApi;
}

/**
 * Show a dismissable error banner if there is a current error.
 */
function ErrorBanner(props: ErrorBannerProps) {
  const currentError = props.reducer.currentError;
  if (currentError) {
    return <div>Error: {JSON.stringify(currentError)}</div>;
  }
  return null;
}

export default Home;