Add flags UI to Payment transaction.
This commit is contained in:
		
							
								
								
									
										33
									
								
								state/constants/flags.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								state/constants/flags.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
import { SelectOption } from '../transactions';
 | 
			
		||||
 | 
			
		||||
interface Flags {
 | 
			
		||||
    [key: string]: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const transactionFlags: { [key: /* TransactionType */ string]: Flags } = {
 | 
			
		||||
    Payment: {
 | 
			
		||||
        tfNoDirectRipple: "0x00010000",
 | 
			
		||||
        tfPartialPayment: "0x00020000",
 | 
			
		||||
        tfLimitQuality: "0x00040000",
 | 
			
		||||
    },
 | 
			
		||||
    // TODO Add more here
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export function combineFlags(flags?: string[]): string | undefined {
 | 
			
		||||
    if (!flags) return
 | 
			
		||||
 | 
			
		||||
    const num = flags.reduce((cumm, curr) =>  cumm | BigInt(curr), BigInt(0))
 | 
			
		||||
    return num.toString()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function extractFlags(transactionType: string, flags?: string | number,): SelectOption[] {
 | 
			
		||||
    const flagsObj = transactionFlags[transactionType]
 | 
			
		||||
    if (!flags || !flagsObj) return []
 | 
			
		||||
 | 
			
		||||
    const extracted = Object.entries(flagsObj).reduce((cumm, [label, value]) => {
 | 
			
		||||
        return (BigInt(flags) & BigInt(value)) ? cumm.concat({ label, value }) : cumm
 | 
			
		||||
    }, [] as SelectOption[])
 | 
			
		||||
 | 
			
		||||
    return extracted
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@ import transactionsData from '../content/transactions.json'
 | 
			
		||||
import state from '.'
 | 
			
		||||
import { showAlert } from '../state/actions/showAlert'
 | 
			
		||||
import { parseJSON } from '../utils/json'
 | 
			
		||||
import { extractFlags, transactionFlags } from './constants/flags'
 | 
			
		||||
 | 
			
		||||
export type SelectOption = {
 | 
			
		||||
  value: string
 | 
			
		||||
@@ -14,6 +15,7 @@ export interface TransactionState {
 | 
			
		||||
  selectedTransaction: SelectOption | null
 | 
			
		||||
  selectedAccount: SelectOption | null
 | 
			
		||||
  selectedDestAccount: SelectOption | null
 | 
			
		||||
  selectedFlags: SelectOption[] | null
 | 
			
		||||
  txIsLoading: boolean
 | 
			
		||||
  txIsDisabled: boolean
 | 
			
		||||
  txFields: TxFields
 | 
			
		||||
@@ -31,6 +33,7 @@ export const defaultTransaction: TransactionState = {
 | 
			
		||||
  selectedTransaction: null,
 | 
			
		||||
  selectedAccount: null,
 | 
			
		||||
  selectedDestAccount: null,
 | 
			
		||||
  selectedFlags: null,
 | 
			
		||||
  txIsLoading: false,
 | 
			
		||||
  txIsDisabled: false,
 | 
			
		||||
  txFields: {},
 | 
			
		||||
@@ -128,9 +131,8 @@ export const prepareTransaction = (data: any) => {
 | 
			
		||||
        try {
 | 
			
		||||
          options[field] = JSON.parse(_value.$value)
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
          const message = `Input error for json field '${field}': ${
 | 
			
		||||
            error instanceof Error ? error.message : ''
 | 
			
		||||
          }`
 | 
			
		||||
          const message = `Input error for json field '${field}': ${error instanceof Error ? error.message : ''
 | 
			
		||||
            }`
 | 
			
		||||
          console.error(message)
 | 
			
		||||
          options[field] = _value.$value
 | 
			
		||||
        }
 | 
			
		||||
@@ -205,6 +207,13 @@ export const prepareState = (value: string, transactionType?: string) => {
 | 
			
		||||
    rest.Destination = Destination
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (transactionFlags[TransactionType] && rest.Flags) {
 | 
			
		||||
    const flags = extractFlags(TransactionType, rest.Flags)
 | 
			
		||||
 | 
			
		||||
    rest.Flags = undefined
 | 
			
		||||
    tx.selectedFlags = flags
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Object.keys(rest).forEach(field => {
 | 
			
		||||
    const value = rest[field]
 | 
			
		||||
    const schemaVal = schema[field as keyof TxFields]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user