diff --git a/components/forms/CreateTxForm/index.tsx b/components/forms/CreateTxForm/index.tsx index 2ce1aaf..3a05b03 100644 --- a/components/forms/CreateTxForm/index.tsx +++ b/components/forms/CreateTxForm/index.tsx @@ -1,8 +1,58 @@ +import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import { Form } from "@/components/ui/form"; +import { useChains } from "@/context/ChainsContext"; +import { getField, getMsgSchema } from "@/lib/form"; import { getMsgRegistry } from "@/lib/msg"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useState } from "react"; +import { useForm } from "react-hook-form"; +import * as z from "zod"; + +type MsgType = Readonly<{ + url: string; + key: string; +}>; export default function CreateTxForm() { - console.log({ msgRegistry: getMsgRegistry() }); + const { chain } = useChains(); + const [msgTypes, setMsgTypes] = useState([]); + + const msgRegistry = getMsgRegistry(); + const categories = [...new Set(Object.values(msgRegistry).map((msg) => msg.category))]; + + const basicCreateTxSchema = z.object({ + memo: z.string().trim().min(1, "Required"), + msgs: z.object({}), + }); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const msgsSchema: Record> = {}; + + for (const msgType of msgTypes) { + msgsSchema[msgType.key] = getMsgSchema(msgRegistry[msgType.url].fields, { chain }); + } + + const createTxSchema = basicCreateTxSchema.extend({ msgs: z.object(msgsSchema) }); + + const createTxForm = useForm>({ + resolver: zodResolver(createTxSchema), + }); + + const addMsg = (typeUrl: string) => { + setMsgTypes((oldMsgTypes) => { + const newMsgTypeUrls: readonly MsgType[] = [ + ...oldMsgTypes, + { url: typeUrl, key: crypto.randomUUID() }, + ]; + // setGasLimit(gasOfTx(newMsgTypes)); + return newMsgTypeUrls; + }); + createTxForm.trigger(); + }; + + const submitCreateTx = (values: z.infer) => + console.log("created tx with values:", values); return ( @@ -10,7 +60,45 @@ export default function CreateTxForm() { Create a new transaction You can add several different messages. - Here will be the future improved form + + {categories.map((category) => ( +
+

{category}

+
+ {Object.values(msgRegistry) + .filter((msg) => msg.category === category) + .map((msg) => ( + + ))} +
+
+ ))} +
+ + {msgTypes.map((type) => { + const msg = msgRegistry[type.url]; + return ( +
+

{msg.name}

+ {msg.fields.map((fieldName: string) => { + const Field = getField(fieldName); + return ( + + ); + })} +
+ ); + })} + +
+ +
); }