2021-10-19 15:56:52 +02:00
|
|
|
/* eslint-disable @typescript-eslint/camelcase */
|
2021-10-26 17:08:03 +02:00
|
|
|
import { UserAttributeSpec, validators } from "anastasis-core";
|
2021-11-01 20:10:49 +01:00
|
|
|
import { Fragment, h, VNode } from "preact";
|
2021-10-19 15:56:52 +02:00
|
|
|
import { useState } from "preact/hooks";
|
2021-10-22 06:31:46 +02:00
|
|
|
import { useAnastasisContext } from "../../context/anastasis";
|
2021-10-26 17:08:03 +02:00
|
|
|
import { AnastasisClientFrame, withProcessLabel } from "./index";
|
2021-10-27 20:13:35 +02:00
|
|
|
import { TextInput } from "../../components/fields/TextInput";
|
2021-10-26 17:08:03 +02:00
|
|
|
import { DateInput } from "../../components/fields/DateInput";
|
2021-10-27 20:13:35 +02:00
|
|
|
import { NumberInput } from "../../components/fields/NumberInput";
|
2021-10-19 15:56:52 +02:00
|
|
|
|
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-11-01 20:10:49 +01:00
|
|
|
const reqAttr = reducer.currentReducerState.required_attributes || []
|
|
|
|
let hasErrors = false;
|
2021-10-26 17:08:03 +02:00
|
|
|
|
2021-11-01 20:10:49 +01:00
|
|
|
const fieldList: VNode[] = reqAttr.map((spec, i: number) => {
|
|
|
|
const value = attrs[spec.name]
|
|
|
|
const error = checkIfValid(value, spec)
|
|
|
|
hasErrors = hasErrors || error !== undefined
|
|
|
|
return (
|
|
|
|
<AttributeEntryField
|
|
|
|
key={i}
|
|
|
|
isFirst={i == 0}
|
|
|
|
setValue={(v: string) => setAttrs({ ...attrs, [spec.name]: v })}
|
|
|
|
spec={spec}
|
|
|
|
errorMessage={error}
|
|
|
|
value={value} />
|
|
|
|
);
|
|
|
|
})
|
2021-10-26 17:08:03 +02:00
|
|
|
|
2021-10-19 15:56:52 +02:00
|
|
|
return (
|
|
|
|
<AnastasisClientFrame
|
2021-10-26 17:08:03 +02:00
|
|
|
title={withProcessLabel(reducer, "Who are you?")}
|
2021-11-01 20:10:49 +01:00
|
|
|
hideNext={hasErrors ? "Complete the form." : undefined}
|
2021-10-19 15:56:52 +02:00
|
|
|
onNext={() => reducer.transition("enter_user_attributes", {
|
|
|
|
identity_attributes: attrs,
|
|
|
|
})}
|
|
|
|
>
|
2021-10-26 17:08:03 +02:00
|
|
|
<div class="columns">
|
|
|
|
<div class="column is-half">
|
2021-11-01 20:10:49 +01:00
|
|
|
{fieldList}
|
2021-10-26 17:08:03 +02:00
|
|
|
</div>
|
|
|
|
<div class="column is-half" >
|
2021-11-01 20:10:49 +01:00
|
|
|
<p>This personal information will help to locate your secret.</p>
|
2021-10-26 17:08:03 +02:00
|
|
|
<h1><b>This stay private</b></h1>
|
|
|
|
<p>The information you have entered here:
|
|
|
|
</p>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<span class="icon is-right">
|
|
|
|
<i class="mdi mdi-circle-small" />
|
|
|
|
</span>
|
|
|
|
Will be hashed, and therefore unreadable
|
|
|
|
</li>
|
|
|
|
<li><span class="icon is-right">
|
|
|
|
<i class="mdi mdi-circle-small" />
|
|
|
|
</span>The non-hashed version is not shared</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-19 15:56:52 +02:00
|
|
|
</AnastasisClientFrame>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:08:03 +02:00
|
|
|
interface AttributeEntryFieldProps {
|
2021-10-19 15:56:52 +02:00
|
|
|
isFirst: boolean;
|
|
|
|
value: string;
|
|
|
|
setValue: (newValue: string) => void;
|
2021-10-22 06:31:46 +02:00
|
|
|
spec: UserAttributeSpec;
|
2021-11-01 20:10:49 +01:00
|
|
|
errorMessage: string | undefined;
|
2021-10-19 15:56:52 +02:00
|
|
|
}
|
2021-10-27 20:13:35 +02:00
|
|
|
const possibleBirthdayYear: Array<number> = []
|
2021-11-01 20:10:49 +01:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2021-10-27 20:13:35 +02:00
|
|
|
possibleBirthdayYear.push(2020 - i)
|
|
|
|
}
|
2021-10-26 17:08:03 +02:00
|
|
|
function AttributeEntryField(props: AttributeEntryFieldProps): VNode {
|
|
|
|
|
2021-10-19 15:56:52 +02:00
|
|
|
return (
|
|
|
|
<div>
|
2021-10-27 20:13:35 +02:00
|
|
|
{props.spec.type === 'date' &&
|
2021-10-26 17:08:03 +02:00
|
|
|
<DateInput
|
2021-10-27 20:13:35 +02:00
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
|
|
|
years={possibleBirthdayYear}
|
2021-11-01 20:10:49 +01:00
|
|
|
error={props.errorMessage}
|
2021-10-27 20:13:35 +02:00
|
|
|
bind={[props.value, props.setValue]}
|
|
|
|
/>}
|
|
|
|
{props.spec.type === 'number' &&
|
|
|
|
<NumberInput
|
2021-10-26 17:08:03 +02:00
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
2021-11-01 20:10:49 +01:00
|
|
|
error={props.errorMessage}
|
2021-10-26 17:08:03 +02:00
|
|
|
bind={[props.value, props.setValue]}
|
2021-10-27 20:13:35 +02:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
{props.spec.type === 'string' &&
|
|
|
|
<TextInput
|
2021-10-26 17:08:03 +02:00
|
|
|
grabFocus={props.isFirst}
|
|
|
|
label={props.spec.label}
|
2021-11-01 20:10:49 +01:00
|
|
|
error={props.errorMessage}
|
2021-10-26 17:08:03 +02:00
|
|
|
bind={[props.value, props.setValue]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
<span>
|
|
|
|
<span class="icon is-right">
|
|
|
|
<i class="mdi mdi-eye-off" />
|
|
|
|
</span>
|
|
|
|
This stay private
|
|
|
|
</span>
|
2021-10-19 15:56:52 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-11-01 20:10:49 +01:00
|
|
|
|
|
|
|
function checkIfValid(value: string, spec: UserAttributeSpec): string | undefined {
|
|
|
|
const pattern = spec['validation-regex']
|
|
|
|
if (pattern) {
|
|
|
|
const re = new RegExp(pattern)
|
|
|
|
if (!re.test(value)) return 'The value is invalid'
|
|
|
|
}
|
|
|
|
const logic = spec['validation-logic']
|
|
|
|
if (logic) {
|
|
|
|
const func = (validators as any)[logic];
|
|
|
|
if (func && typeof func === 'function' && !func(value)) return 'Please check the value'
|
|
|
|
}
|
|
|
|
const optional = spec.optional
|
|
|
|
if (!optional && !value) {
|
|
|
|
return 'This value is required'
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|