show an error message when the input has more than 21 chars

This commit is contained in:
Sebastian 2022-04-15 13:07:03 -03:00
parent 5a4b6c7eb6
commit b3b1329acf
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
4 changed files with 46 additions and 11 deletions

View File

@ -17,14 +17,13 @@
/** /**
* Imports. * Imports.
*/ */
import { import { ChallengeInfo } from "@gnu-taler/anastasis-core";
ChallengeInfo,
} from "@gnu-taler/anastasis-core";
import { h, VNode } from "preact"; import { h, VNode } from "preact";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
import { AsyncButton } from "../../../components/AsyncButton"; import { AsyncButton } from "../../../components/AsyncButton";
import { TextInput } from "../../../components/fields/TextInput"; import { TextInput } from "../../../components/fields/TextInput";
import { useAnastasisContext } from "../../../context/anastasis"; import { useAnastasisContext } from "../../../context/anastasis";
import { useTranslator } from "../../../i18n";
import { AnastasisClientFrame } from "../index"; import { AnastasisClientFrame } from "../index";
import { SolveOverviewFeedbackDisplay } from "../SolveScreen"; import { SolveOverviewFeedbackDisplay } from "../SolveScreen";
import { shouldHideConfirm } from "./helpers"; import { shouldHideConfirm } from "./helpers";
@ -48,12 +47,13 @@ export function AuthMethodEmailSolve({ id }: AuthMethodSolveProps): VNode {
result += `-${unformatted.substring(8, 12)}`; result += `-${unformatted.substring(8, 12)}`;
} }
if (unformatted.length > 12) { if (unformatted.length > 12) {
result += `-${unformatted.substring(12, 16)}`; result += `-${unformatted.substring(12)}`;
} }
_setAnswer(result); _setAnswer(result);
} }
const [expanded, setExpanded] = useState(false); const [expanded, setExpanded] = useState(false);
const i18n = useTranslator();
const reducer = useAnastasisContext(); const reducer = useAnastasisContext();
if (!reducer) { if (!reducer) {
@ -66,7 +66,7 @@ export function AuthMethodEmailSolve({ id }: AuthMethodSolveProps): VNode {
if (reducer.currentReducerState?.reducer_type !== "recovery") { if (reducer.currentReducerState?.reducer_type !== "recovery") {
return ( return (
<AnastasisClientFrame hideNav title="Recovery problem"> <AnastasisClientFrame hideNav title="Recovery problem">
<div>invalid state</div> <div>invalid state, no recovery state</div>
</AnastasisClientFrame> </AnastasisClientFrame>
); );
} }
@ -84,7 +84,7 @@ export function AuthMethodEmailSolve({ id }: AuthMethodSolveProps): VNode {
if (!reducer.currentReducerState.selected_challenge_uuid) { if (!reducer.currentReducerState.selected_challenge_uuid) {
return ( return (
<AnastasisClientFrame hideNav title="Recovery problem"> <AnastasisClientFrame hideNav title="Recovery problem">
<div>invalid state</div> <div>invalid state, no challenge id</div>
<div <div
style={{ style={{
marginTop: "2em", marginTop: "2em",
@ -122,6 +122,11 @@ export function AuthMethodEmailSolve({ id }: AuthMethodSolveProps): VNode {
reducer?.back(); reducer?.back();
} }
const error =
answer.length > 21
? i18n`The answer should not be greater than 21 characters.`
: undefined;
return ( return (
<AnastasisClientFrame hideNav title="Email challenge"> <AnastasisClientFrame hideNav title="Email challenge">
<SolveOverviewFeedbackDisplay feedback={feedback} /> <SolveOverviewFeedbackDisplay feedback={feedback} />
@ -160,6 +165,7 @@ export function AuthMethodEmailSolve({ id }: AuthMethodSolveProps): VNode {
grabFocus grabFocus
onConfirm={onNext} onConfirm={onNext}
bind={[answer, setAnswer]} bind={[answer, setAnswer]}
error={error}
placeholder="A-12345-678-1234-5678" placeholder="A-12345-678-1234-5678"
/> />
@ -174,7 +180,11 @@ export function AuthMethodEmailSolve({ id }: AuthMethodSolveProps): VNode {
Cancel Cancel
</button> </button>
{!shouldHideConfirm(feedback) && ( {!shouldHideConfirm(feedback) && (
<AsyncButton class="button is-info" onClick={onNext}> <AsyncButton
class="button is-info"
onClick={onNext}
disabled={!!error}
>
Confirm Confirm
</AsyncButton> </AsyncButton>
)} )}

View File

@ -7,6 +7,7 @@ import { useState } from "preact/hooks";
import { AsyncButton } from "../../../components/AsyncButton"; import { AsyncButton } from "../../../components/AsyncButton";
import { TextInput } from "../../../components/fields/TextInput"; import { TextInput } from "../../../components/fields/TextInput";
import { useAnastasisContext } from "../../../context/anastasis"; import { useAnastasisContext } from "../../../context/anastasis";
import { useTranslator } from "../../../i18n";
import { AnastasisClientFrame } from "../index"; import { AnastasisClientFrame } from "../index";
import { SolveOverviewFeedbackDisplay } from "../SolveScreen"; import { SolveOverviewFeedbackDisplay } from "../SolveScreen";
import { shouldHideConfirm } from "./helpers"; import { shouldHideConfirm } from "./helpers";
@ -30,11 +31,12 @@ export function AuthMethodPostSolve({ id }: AuthMethodSolveProps): VNode {
result += `-${unformatted.substring(8, 12)}`; result += `-${unformatted.substring(8, 12)}`;
} }
if (unformatted.length > 12) { if (unformatted.length > 12) {
result += `-${unformatted.substring(12, 16)}`; result += `-${unformatted.substring(12)}`;
} }
_setAnswer(result); _setAnswer(result);
} }
const i18n = useTranslator();
const reducer = useAnastasisContext(); const reducer = useAnastasisContext();
if (!reducer) { if (!reducer) {
@ -103,6 +105,11 @@ export function AuthMethodPostSolve({ id }: AuthMethodSolveProps): VNode {
reducer?.back(); reducer?.back();
} }
const error =
answer.length > 21
? i18n`The answer should not be greater than 21 characters.`
: undefined;
return ( return (
<AnastasisClientFrame hideNav title="Postal Challenge"> <AnastasisClientFrame hideNav title="Postal Challenge">
<SolveOverviewFeedbackDisplay feedback={feedback} /> <SolveOverviewFeedbackDisplay feedback={feedback} />
@ -112,6 +119,7 @@ export function AuthMethodPostSolve({ id }: AuthMethodSolveProps): VNode {
label="Answer" label="Answer"
grabFocus grabFocus
placeholder="A-12345-678-1234-5678" placeholder="A-12345-678-1234-5678"
error={error}
bind={[answer, setAnswer]} bind={[answer, setAnswer]}
/> />
@ -126,7 +134,11 @@ export function AuthMethodPostSolve({ id }: AuthMethodSolveProps): VNode {
Cancel Cancel
</button> </button>
{!shouldHideConfirm(feedback) && ( {!shouldHideConfirm(feedback) && (
<AsyncButton class="button is-info" onClick={onNext}> <AsyncButton
class="button is-info"
onClick={onNext}
disabled={!!error}
>
Confirm Confirm
</AsyncButton> </AsyncButton>
)} )}

View File

@ -7,6 +7,7 @@ import { useState } from "preact/hooks";
import { AsyncButton } from "../../../components/AsyncButton"; import { AsyncButton } from "../../../components/AsyncButton";
import { TextInput } from "../../../components/fields/TextInput"; import { TextInput } from "../../../components/fields/TextInput";
import { useAnastasisContext } from "../../../context/anastasis"; import { useAnastasisContext } from "../../../context/anastasis";
import { useTranslator } from "../../../i18n";
import { AnastasisClientFrame } from "../index"; import { AnastasisClientFrame } from "../index";
import { SolveOverviewFeedbackDisplay } from "../SolveScreen"; import { SolveOverviewFeedbackDisplay } from "../SolveScreen";
import { shouldHideConfirm } from "./helpers"; import { shouldHideConfirm } from "./helpers";
@ -30,11 +31,12 @@ export function AuthMethodSmsSolve({ id }: AuthMethodSolveProps): VNode {
result += `-${unformatted.substring(8, 12)}`; result += `-${unformatted.substring(8, 12)}`;
} }
if (unformatted.length > 12) { if (unformatted.length > 12) {
result += `-${unformatted.substring(12, 16)}`; result += `-${unformatted.substring(12)}`;
} }
_setAnswer(result); _setAnswer(result);
} }
const i18n = useTranslator();
const [expanded, setExpanded] = useState(false); const [expanded, setExpanded] = useState(false);
const reducer = useAnastasisContext(); const reducer = useAnastasisContext();
@ -104,6 +106,11 @@ export function AuthMethodSmsSolve({ id }: AuthMethodSolveProps): VNode {
reducer?.back(); reducer?.back();
} }
const error =
answer.length > 21
? i18n`The answer should not be greater than 21 characters.`
: undefined;
return ( return (
<AnastasisClientFrame hideNav title="SMS Challenge"> <AnastasisClientFrame hideNav title="SMS Challenge">
<SolveOverviewFeedbackDisplay feedback={feedback} /> <SolveOverviewFeedbackDisplay feedback={feedback} />
@ -142,6 +149,7 @@ export function AuthMethodSmsSolve({ id }: AuthMethodSolveProps): VNode {
grabFocus grabFocus
onConfirm={onNext} onConfirm={onNext}
bind={[answer, setAnswer]} bind={[answer, setAnswer]}
error={error}
placeholder="A-12345-678-1234-5678" placeholder="A-12345-678-1234-5678"
/> />
@ -156,7 +164,11 @@ export function AuthMethodSmsSolve({ id }: AuthMethodSolveProps): VNode {
Cancel Cancel
</button> </button>
{!shouldHideConfirm(feedback) && ( {!shouldHideConfirm(feedback) && (
<AsyncButton class="button is-info" onClick={onNext}> <AsyncButton
class="button is-info"
onClick={onNext}
disabled={!!error}
>
Confirm Confirm
</AsyncButton> </AsyncButton>
)} )}

View File

@ -222,6 +222,7 @@ export const reducerStatesExample = {
} as ReducerState, } as ReducerState,
challengeSolving: { challengeSolving: {
...base, ...base,
reducer_type: "recovery",
recovery_state: RecoveryStates.ChallengeSolving, recovery_state: RecoveryStates.ChallengeSolving,
} as ReducerStateRecovery, } as ReducerStateRecovery,
challengePaying: { challengePaying: {