Fix json saving mismatch

This commit is contained in:
muzam1l
2022-05-31 00:53:58 +05:30
parent 377c963c7a
commit 6f636645f7
3 changed files with 13 additions and 11 deletions

View File

@@ -86,9 +86,10 @@ const Transaction: FC<TransactionProps> = ({
const submitTest = useCallback(async () => { const submitTest = useCallback(async () => {
let st: TransactionState | undefined; let st: TransactionState | undefined;
const tt = txState.selectedTransaction?.value;
if (viewType === "json") { if (viewType === "json") {
// save the editor state first // save the editor state first
const pst = prepareState(editorValue || "", txState); const pst = prepareState(editorValue || "", tt);
if (!pst) return; if (!pst) return;
st = setState(pst); st = setState(pst);

View File

@@ -42,7 +42,9 @@ export const TxJson: FC<JsonProps> = ({
const { editorValue = value, estimatedFee } = txState; const { editorValue = value, estimatedFee } = txState;
const { theme } = useTheme(); const { theme } = useTheme();
const [hasUnsaved, setHasUnsaved] = useState(false); const [hasUnsaved, setHasUnsaved] = useState(false);
const [currTxType, setCurrTxType] = useState<string>(); const [currTxType, setCurrTxType] = useState<string | undefined>(
txState.selectedTransaction?.value
);
useEffect(() => { useEffect(() => {
setState({ editorValue: value }); setState({ editorValue: value });
@@ -66,8 +68,8 @@ export const TxJson: FC<JsonProps> = ({
else setHasUnsaved(true); else setHasUnsaved(true);
}, [editorValue, value]); }, [editorValue, value]);
const saveState = (value: string, txState: TransactionState) => { const saveState = (value: string, transactionType?: string) => {
const tx = prepareState(value, txState); const tx = prepareState(value, transactionType);
if (tx) setState(tx); if (tx) setState(tx);
}; };
@@ -82,7 +84,7 @@ export const TxJson: FC<JsonProps> = ({
const onExit = (value: string) => { const onExit = (value: string) => {
const options = parseJSON(value); const options = parseJSON(value);
if (options) { if (options) {
saveState(value, txState); saveState(value, currTxType);
return; return;
} }
showAlert("Error!", { showAlert("Error!", {
@@ -178,7 +180,6 @@ export const TxJson: FC<JsonProps> = ({
useEffect(() => { useEffect(() => {
if (!monaco) return; if (!monaco) return;
getSchemas().then(schemas => { getSchemas().then(schemas => {
console.log("seeung schmea");
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true, validate: true,
schemas, schemas,
@@ -221,7 +222,7 @@ export const TxJson: FC<JsonProps> = ({
{hasUnsaved && ( {hasUnsaved && (
<Text muted small css={{ position: "absolute", bottom: 0, right: 0 }}> <Text muted small css={{ position: "absolute", bottom: 0, right: 0 }}>
This file has unsaved changes.{" "} This file has unsaved changes.{" "}
<Link onClick={() => saveState(editorValue, txState)}>save</Link>{" "} <Link onClick={() => saveState(editorValue, currTxType)}>save</Link>{" "}
<Link onClick={discardChanges}>discard</Link> <Link onClick={discardChanges}>discard</Link>
</Text> </Text>
)} )}

View File

@@ -141,7 +141,7 @@ export const prepareTransaction = (data: any) => {
} }
// editor value to state // editor value to state
export const prepareState = (value: string, txState: TransactionState) => { export const prepareState = (value: string, transactionType?: string) => {
const options = parseJSON(value); const options = parseJSON(value);
if (!options) { if (!options) {
showAlert("Error!", { showAlert("Error!", {
@@ -152,7 +152,7 @@ export const prepareState = (value: string, txState: TransactionState) => {
const { Account, TransactionType, Destination, ...rest } = options; const { Account, TransactionType, Destination, ...rest } = options;
let tx: Partial<TransactionState> = {}; let tx: Partial<TransactionState> = {};
const { txFields } = txState const txFields = getTxFields(transactionType)
if (Account) { if (Account) {
const acc = state.accounts.find(acc => acc.address === Account); const acc = state.accounts.find(acc => acc.address === Account);
@@ -207,7 +207,7 @@ export const prepareState = (value: string, txState: TransactionState) => {
if (isXrp) { if (isXrp) {
rest[field] = { rest[field] = {
$type: "xrp", $type: "xrp",
$value: +value / 1000000, // TODO maybe use bigint? $value: +value / 1000000, // ! maybe use bigint?
}; };
} else if (typeof value === "object") { } else if (typeof value === "object") {
rest[field] = { rest[field] = {
@@ -223,7 +223,7 @@ export const prepareState = (value: string, txState: TransactionState) => {
return tx return tx
} }
export const getTxFields = (tt: string) => { export const getTxFields = (tt?: string) => {
const txFields: TxFields | undefined = transactionsData.find( const txFields: TxFields | undefined = transactionsData.find(
tx => tx.TransactionType === tt tx => tx.TransactionType === tt
); );