diff options
author | Sebastian <sebasjm@gmail.com> | 2021-03-16 10:39:46 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-03-16 10:40:22 -0300 |
commit | b38595a7114174038a195cbf70449123a07b9cda (patch) | |
tree | a8eae9386012957aeeb95f9ca75b03d2f77fcf11 /packages/frontend/src/paths/admin/create/CreatePage.tsx | |
parent | b63efaaed60b46d911ec64df31485b913af0a7d1 (diff) | |
download | merchant-backoffice-b38595a7114174038a195cbf70449123a07b9cda.tar.gz merchant-backoffice-b38595a7114174038a195cbf70449123a07b9cda.tar.bz2 merchant-backoffice-b38595a7114174038a195cbf70449123a07b9cda.zip |
build single html with all assets
Diffstat (limited to 'packages/frontend/src/paths/admin/create/CreatePage.tsx')
-rw-r--r-- | packages/frontend/src/paths/admin/create/CreatePage.tsx | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/packages/frontend/src/paths/admin/create/CreatePage.tsx b/packages/frontend/src/paths/admin/create/CreatePage.tsx new file mode 100644 index 0000000..ae76796 --- /dev/null +++ b/packages/frontend/src/paths/admin/create/CreatePage.tsx @@ -0,0 +1,127 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** +* +* @author Sebastian Javier Marchano (sebasjm) +*/ + +import { h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { MerchantBackend } from "../../../declaration"; +import * as yup from 'yup'; +import { FormErrors, FormProvider } from "../../../components/form/Field" +import { InstanceCreateSchema as schema } from '../../../schemas' +import { Message } from "preact-messages"; +import { Input } from "../../../components/form/Input"; +import { InputSecured } from "../../../components/form/InputSecured"; +import { InputWithAddon } from "../../../components/form/InputWithAddon"; +import { InputGroup } from "../../../components/form/InputGroup"; +import { useConfigContext, useBackendContext } from "../../../context/backend"; +import { InputDuration } from "../../../components/form/InputDuration"; +import { InputCurrency } from "../../../components/form/InputCurrency"; +import { InputPayto } from "../../../components/form/InputPayto"; + +type Entity = MerchantBackend.Instances.InstanceConfigurationMessage & {auth_token?: string} + +interface Props { + onCreate: (d: Entity) => void; + isLoading: boolean; + onBack: () => void; +} + +interface KeyValue { + [key: string]: string; +} + +function with_defaults(): Partial<Entity> { + return { + default_pay_delay: { d_ms: 1000 }, + default_wire_fee_amortization: 10, + default_wire_transfer_delay: { d_ms: 2000 }, + }; +} + +export function CreatePage({ onCreate, isLoading, onBack }: Props): VNode { + const [value, valueHandler] = useState(with_defaults()) + const [errors, setErrors] = useState<FormErrors<Entity>>({}) + + const submit = (): void => { + try { + // use conversion instead of this + const newToken = value.auth_token; + value.auth_token = undefined; + value.auth = newToken === null || newToken === undefined ? { method: "external" } : { method: "token", token: `secret-token:${newToken}` }; + // remove above use conversion + schema.validateSync(value, { abortEarly: false }) + onCreate(schema.cast(value) as Entity); + } catch (err) { + const errors = err.inner as yup.ValidationError[] + const pathMessages = errors.reduce((prev, cur) => !cur.path ? prev : ({ ...prev, [cur.path]: { type: cur.type, params: cur.params, message: cur.message } }), {}) + setErrors(pathMessages) + } + } + const backend = useBackendContext() + const config = useConfigContext() + + return <div> + + <section class="section is-main-section"> + <div class="columns"> + <div class="column" /> + <div class="column is-two-thirds"> + <FormProvider<Entity> errors={errors} object={value} valueHandler={valueHandler} > + + <InputWithAddon<Entity> name="id" addonBefore={`${backend.url}/private/instances/`} /> + + <Input<Entity> name="name" /> + + <InputSecured<Entity> name="auth_token" /> + + <InputPayto<Entity> name="payto_uris" /> + + <InputCurrency<Entity> name="default_max_deposit_fee" currency={config.currency} /> + + <InputCurrency<Entity> name="default_max_wire_fee" currency={config.currency} /> + + <Input<Entity> name="default_wire_fee_amortization" /> + + <InputGroup name="address"> + <Input<Entity> name="name" /> + </InputGroup> + + <InputGroup name="jurisdiction"> + <Input<Entity> name="name" /> + </InputGroup> + + <InputDuration<Entity> name="default_pay_delay" /> + + <InputDuration<Entity> name="default_wire_transfer_delay" /> + + </FormProvider> + + <div class="buttons is-right mt-5"> + <button class="button" onClick={onBack} ><Message id="Cancel" /></button> + <button class="button is-success" onClick={submit} ><Message id="Confirm" /></button> + </div> + + </div> + <div class="column" /> + </div> + </section> + + </div> +}
\ No newline at end of file |