Skip to content

Commit

Permalink
Avoid sending form with JSON errors
Browse files Browse the repository at this point in the history
  • Loading branch information
abefernan committed Jul 27, 2023
1 parent d49baa2 commit 62be9d6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
14 changes: 10 additions & 4 deletions components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MsgExecuteContractEncodeObject } from "@cosmjs/cosmwasm-stargate";
import { toUtf8 } from "@cosmjs/encoding";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { MsgGetter } from "..";
import { useChains } from "../../../../context/ChainsContext";
import { ChainInfo } from "../../../../context/ChainsContext/types";
Expand Down Expand Up @@ -45,6 +45,7 @@ const MsgExecuteContractForm = ({
const [customDenom, setCustomDenom] = useState("");
const [amount, setAmount] = useState("0");

const jsonError = useRef(false);
const [contractAddressError, setContractAddressError] = useState("");
const [customDenomError, setCustomDenomError] = useState("");
const [amountError, setAmountError] = useState("");
Expand All @@ -55,6 +56,10 @@ const MsgExecuteContractForm = ({
setAmountError("");

const isMsgValid = (): boolean => {
if (jsonError.current) {
return false;
}

const addressErrorMsg = checkAddress(contractAddress, chain.addressPrefix);
if (addressErrorMsg) {
setContractAddressError(`Invalid address for network ${chain.chainId}: ${addressErrorMsg}`);
Expand Down Expand Up @@ -142,9 +147,10 @@ const MsgExecuteContractForm = ({
<JsonEditor
label="Msg JSON"
content={{ text: msgContent }}
onChange={(newMsgContent) =>
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}")
}
onChange={(newMsgContent, _, { contentErrors }) => {
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}");
jsonError.current = !!contentErrors;
}}
/>
</div>
<div className="form-item form-select">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MsgInstantiateContract2EncodeObject } from "@cosmjs/cosmwasm-stargate";
import { fromHex, toUtf8 } from "@cosmjs/encoding";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { MsgGetter } from "..";
import { useChains } from "../../../../context/ChainsContext";
import { ChainInfo } from "../../../../context/ChainsContext/types";
Expand Down Expand Up @@ -48,6 +48,7 @@ const MsgInstantiateContract2Form = ({
const [customDenom, setCustomDenom] = useState("");
const [amount, setAmount] = useState("0");

const jsonError = useRef(false);
const [codeIdError, setCodeIdError] = useState("");
const [labelError, setLabelError] = useState("");
const [adminAddressError, setAdminAddressError] = useState("");
Expand All @@ -64,6 +65,10 @@ const MsgInstantiateContract2Form = ({
setAmountError("");

const isMsgValid = (): boolean => {
if (jsonError.current) {
return false;
}

if (!codeId || !Number.isSafeInteger(Number(codeId)) || Number(codeId) <= 0) {
setCodeIdError("Code ID must be a positive integer");
return false;
Expand Down Expand Up @@ -218,9 +223,10 @@ const MsgInstantiateContract2Form = ({
<JsonEditor
label="Msg JSON"
content={{ text: msgContent }}
onChange={(newMsgContent) =>
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}")
}
onChange={(newMsgContent, _, { contentErrors }) => {
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}");
jsonError.current = !!contentErrors;
}}
/>
</div>
<div className="form-item form-select">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MsgInstantiateContractEncodeObject } from "@cosmjs/cosmwasm-stargate";
import { toUtf8 } from "@cosmjs/encoding";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { MsgGetter } from "..";
import { useChains } from "../../../../context/ChainsContext";
import { ChainInfo } from "../../../../context/ChainsContext/types";
Expand Down Expand Up @@ -47,6 +47,7 @@ const MsgInstantiateContractForm = ({
const [customDenom, setCustomDenom] = useState("");
const [amount, setAmount] = useState("0");

const jsonError = useRef(false);
const [codeIdError, setCodeIdError] = useState("");
const [labelError, setLabelError] = useState("");
const [adminAddressError, setAdminAddressError] = useState("");
Expand All @@ -61,6 +62,10 @@ const MsgInstantiateContractForm = ({
setAmountError("");

const isMsgValid = (): boolean => {
if (jsonError.current) {
return false;
}

if (!codeId || !Number.isSafeInteger(Number(codeId)) || Number(codeId) <= 0) {
setCodeIdError("Code ID must be a positive integer");
return false;
Expand Down Expand Up @@ -183,9 +188,10 @@ const MsgInstantiateContractForm = ({
<JsonEditor
label="Msg JSON"
content={{ text: msgContent }}
onChange={(newMsgContent) =>
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}")
}
onChange={(newMsgContent, _, { contentErrors }) => {
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}");
jsonError.current = !!contentErrors;
}}
/>
</div>
<div className="form-item form-select">
Expand Down
14 changes: 10 additions & 4 deletions components/forms/CreateTxForm/MsgForm/MsgMigrateContractForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MsgMigrateContractEncodeObject } from "@cosmjs/cosmwasm-stargate";
import { toUtf8 } from "@cosmjs/encoding";
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { MsgGetter } from "..";
import { useChains } from "../../../../context/ChainsContext";
import { checkAddress, exampleAddress } from "../../../../lib/displayHelpers";
Expand All @@ -28,6 +28,7 @@ const MsgMigrateContractForm = ({
const [codeId, setCodeId] = useState("");
const [msgContent, setMsgContent] = useState("{}");

const jsonError = useRef(false);
const [contractAddressError, setContractAddressError] = useState("");
const [codeIdError, setCodeIdError] = useState("");

Expand All @@ -36,6 +37,10 @@ const MsgMigrateContractForm = ({
setContractAddressError("");

const isMsgValid = (): boolean => {
if (jsonError.current) {
return false;
}

const addressErrorMsg = checkAddress(contractAddress, chain.addressPrefix);
if (addressErrorMsg) {
setContractAddressError(`Invalid address for network ${chain.chainId}: ${addressErrorMsg}`);
Expand Down Expand Up @@ -108,9 +113,10 @@ const MsgMigrateContractForm = ({
<JsonEditor
label="Msg JSON"
content={{ text: msgContent }}
onChange={(newMsgContent) =>
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}")
}
onChange={(newMsgContent, _, { contentErrors }) => {
setMsgContent("text" in newMsgContent ? newMsgContent.text ?? "{}" : "{}");
jsonError.current = !!contentErrors;
}}
/>
</div>
<style jsx>{`
Expand Down

0 comments on commit 62be9d6

Please sign in to comment.