;
}) {
const { i18n } = useTranslationContext();
const theForm = uiForms.forms(i18n).find((v) => v.id === formId);
if (!theForm) {
return form with id {formId} not found
;
}
const { config } = useExchangeApiContext();
const initial = {
when: AbsoluteTime.now(),
state: AmlExchangeBackend.AmlState.pending,
threshold: Amounts.zeroOfCurrency(config.currency),
};
return (
{}}
onSubmit={(formValue) => {
if (
formValue.state === undefined ||
formValue.threshold === undefined
) {
return;
}
const validatedForm = formValue as BaseForm;
const st = formValue.state;
const amount = formValue.threshold;
const justification: Justification = {
id: theForm.id,
label: theForm.label,
version: theForm.version,
value: validatedForm,
};
onSubmit(justification, st, amount);
}}
>
);
}
export type Justification = {
// form values
value: T;
} & Omit, "icon">, "impl">;
export function stringifyJustification(j: Justification): string {
return JSON.stringify(j);
}
type SimpleFormMetadata = {
version?: number;
id?: string;
};
export const codecForSimpleFormMetadata = (): Codec =>
buildCodecForObject()
.property("id", codecOptional(codecForString()))
.property("version", codecOptional(codecForNumber()))
.build("SimpleFormMetadata");
type ParseJustificationFail =
| "not-json"
| "id-not-found"
| "form-not-found"
| "version-not-found";
export function parseJustification(
s: string,
listOfAllKnownForms: FormMetadata[],
):
| OperationOk<{
justification: Justification;
metadata: FormMetadata;
}>
| OperationFail {
try {
const justification = JSON.parse(s);
const info = codecForSimpleFormMetadata().decode(justification);
if (!info.id) {
return {
type: "fail",
case: "id-not-found",
detail: {} as TalerErrorDetail,
};
}
if (!info.version) {
return {
type: "fail",
case: "version-not-found",
detail: {} as TalerErrorDetail,
};
}
const found = listOfAllKnownForms.find((f) => {
return f.id === info.id && f.version === info.version;
});
if (!found) {
return {
type: "fail",
case: "form-not-found",
detail: {} as TalerErrorDetail,
};
}
return {
type: "ok",
body: {
justification,
metadata: found,
},
};
} catch (e) {
return {
type: "fail",
case: "not-json",
detail: {} as TalerErrorDetail,
};
}
}