From a054b740ee69c6ebbc3ac706b6ee6eb2117bf4c6 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:27:24 +0200 Subject: [PATCH 1/3] Add FieldCommission --- .../CreateTxForm/Fields/FieldCommission.tsx | 94 +++++++++++++++++++ components/forms/CreateTxForm/Fields/index.ts | 1 + 2 files changed, 95 insertions(+) create mode 100644 components/forms/CreateTxForm/Fields/FieldCommission.tsx diff --git a/components/forms/CreateTxForm/Fields/FieldCommission.tsx b/components/forms/CreateTxForm/Fields/FieldCommission.tsx new file mode 100644 index 0000000..ff39b42 --- /dev/null +++ b/components/forms/CreateTxForm/Fields/FieldCommission.tsx @@ -0,0 +1,94 @@ +import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { prettyFieldName } from "@/lib/form"; +import * as z from "zod"; +import type { FieldProps } from "./types"; + +const isFieldCommission = (fieldName: string) => fieldName === "inputs" || fieldName === "outputs"; + +export const getFieldCommission = (fieldName: string) => + isFieldCommission(fieldName) ? FieldCommission : null; + +export const getFieldCommissionSchema = (fieldName: string) => + isFieldCommission(fieldName) + ? z.object({ + rate: z.coerce + .number({ invalid_type_error: "Must be a number", required_error: "Required" }) + .positive("Must be positive") + .transform((value) => { + try { + return String(value); + } catch (error) { + return value; + } + }), + maxRate: z.coerce + .number({ invalid_type_error: "Must be a number", required_error: "Required" }) + .positive("Must be positive") + .transform((value) => { + try { + return String(value); + } catch (error) { + return value; + } + }), + maxChangeRate: z.coerce + .number({ invalid_type_error: "Must be a number", required_error: "Required" }) + .positive("Must be positive") + .transform((value) => { + try { + return String(value); + } catch (error) { + return value; + } + }), + }) + : null; + +export default function FieldCommission({ form, fieldFormName }: FieldProps) { + const prettyLabel = prettyFieldName(fieldFormName); + + return ( + <> + ( + + {`${prettyLabel} Rate`} + + + + + + )} + /> + ( + + {`${prettyLabel} Max Rate`} + + + + + + )} + /> + ( + + {`${prettyLabel} Max Change Rate`} + + + + + + )} + /> + + ); +} diff --git a/components/forms/CreateTxForm/Fields/index.ts b/components/forms/CreateTxForm/Fields/index.ts index 62413a5..193c24b 100644 --- a/components/forms/CreateTxForm/Fields/index.ts +++ b/components/forms/CreateTxForm/Fields/index.ts @@ -1,6 +1,7 @@ export * from "./FieldAddress"; export * from "./FieldAmount"; export * from "./FieldBoolean"; +export * from "./FieldCommission"; export * from "./FieldDescription"; export * from "./FieldNumber"; export * from "./FieldString"; From 31c6fd6c5cd81950154287458d13ec6ce5fb350d Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:27:34 +0200 Subject: [PATCH 2/3] Add FieldCommission to form helpers --- lib/form.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/form.ts b/lib/form.ts index 53b2e5e..ac6a7b5 100644 --- a/lib/form.ts +++ b/lib/form.ts @@ -5,6 +5,8 @@ import { getFieldAmountSchema, getFieldBoolean, getFieldBooleanSchema, + getFieldCommission, + getFieldCommissionSchema, getFieldDescription, getFieldDescriptionSchema, getFieldNumber, @@ -33,6 +35,7 @@ export const getField = (fieldName: string) => getFieldBoolean(fieldName) || getFieldTimeoutHeight(fieldName) || getFieldDescription(fieldName) || + getFieldCommission(fieldName) || null; const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) => @@ -43,6 +46,7 @@ const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) => getFieldBooleanSchema(fieldName) || getFieldTimeoutHeightSchema(fieldName) || getFieldDescriptionSchema(fieldName) || + getFieldCommissionSchema(fieldName) || null; export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => { From c230ec9d3a8d5c815fcd87f70942e7b4ae89dcb0 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:57:44 +0200 Subject: [PATCH 3/3] Fix msg field for commission --- components/forms/CreateTxForm/Fields/FieldCommission.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/forms/CreateTxForm/Fields/FieldCommission.tsx b/components/forms/CreateTxForm/Fields/FieldCommission.tsx index ff39b42..e15a6f0 100644 --- a/components/forms/CreateTxForm/Fields/FieldCommission.tsx +++ b/components/forms/CreateTxForm/Fields/FieldCommission.tsx @@ -4,7 +4,7 @@ import { prettyFieldName } from "@/lib/form"; import * as z from "zod"; import type { FieldProps } from "./types"; -const isFieldCommission = (fieldName: string) => fieldName === "inputs" || fieldName === "outputs"; +const isFieldCommission = (fieldName: string) => fieldName === "commission"; export const getFieldCommission = (fieldName: string) => isFieldCommission(fieldName) ? FieldCommission : null;