2021-10-19 15:56:52 +02:00
|
|
|
/* eslint-disable @typescript-eslint/camelcase */
|
|
|
|
import { h, VNode } from "preact";
|
|
|
|
import { useState } from "preact/hooks";
|
2021-10-22 06:31:46 +02:00
|
|
|
import { ReducerStateRecovery, ReducerStateBackup, UserAttributeSpec } from "anastasis-core/lib";
|
|
|
|
import { useAnastasisContext } from "../../context/anastasis";
|
2021-10-19 16:17:54 +02:00
|
|
|
import { AnastasisReducerApi } from "../../hooks/use-anastasis-reducer";
|
2021-10-19 15:56:52 +02:00
|
|
|
import { AnastasisClientFrame, withProcessLabel, LabeledInput } from "./index";
|
|
|
|
|
2021-10-22 06:31:46 +02:00
|
|
|
export function AttributeEntryScreen(): VNode {
|
|
|
|
const reducer = useAnastasisContext()
|
|
|
|
const state = reducer?.currentReducerState
|
|
|
|
const currentIdentityAttributes = state && "identity_attributes" in state ? (state.identity_attributes || {}) : {}
|
|
|
|
const [attrs, setAttrs] = useState<Record<string, string>>(currentIdentityAttributes);
|
|
|
|
|
|
|
|
if (!reducer) {
|
|
|
|
return <div>no reducer in context</div>
|
|
|
|
}
|
|
|
|
if (!reducer.currentReducerState || !("required_attributes" in reducer.currentReducerState)) {
|
|
|
|
return <div>invalid state</div>
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:56:52 +02:00
|
|
|
return (
|
|
|
|
<AnastasisClientFrame
|
|
|
|
title={withProcessLabel(reducer, "Select Country")}
|
|
|
|
onNext={() => reducer.transition("enter_user_attributes", {
|
|
|
|
identity_attributes: attrs,
|
|
|
|
})}
|
|
|
|
>
|
2021-10-22 06:31:46 +02:00
|
|
|
{reducer.currentReducerState.required_attributes?.map((x, i: number) => {
|
2021-10-19 15:56:52 +02:00
|
|
|
return (
|
|
|
|
<AttributeEntryField
|
|
|
|
key={i}
|
|
|
|
isFirst={i == 0}
|
|
|
|
setValue={(v: string) => setAttrs({ ...attrs, [x.name]: v })}
|
|
|
|
spec={x}
|
|
|
|
value={attrs[x.name]} />
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</AnastasisClientFrame>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AttributeEntryProps {
|
|
|
|
reducer: AnastasisReducerApi;
|
|
|
|
reducerState: ReducerStateRecovery | ReducerStateBackup;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AttributeEntryFieldProps {
|
|
|
|
isFirst: boolean;
|
|
|
|
value: string;
|
|
|
|
setValue: (newValue: string) => void;
|
2021-10-22 06:31:46 +02:00
|
|
|
spec: UserAttributeSpec;
|
2021-10-19 15:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function AttributeEntryField(props: AttributeEntryFieldProps): VNode {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<LabeledInput
|
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
|
|
|
bind={[props.value, props.setValue]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|