// Copyright 2017-2020 @polkadot/app-contracts authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { I18nProps } from '@polkadot/react-components/types'; import BN from 'bn.js'; import React from 'react'; import { Abi } from '@polkadot/api-contract'; import { Input, InputAddress, InputNumber, Modal, TxComponent } from '@polkadot/react-components'; import ABI from './ABI'; export interface ContractModalProps extends I18nProps { basePath: string; isNew?: boolean; isOpen: boolean; onClose?: () => void; } export interface ContractModalState { abi?: string | null; accountId?: string | null; contractAbi?: Abi | null; gasLimit: BN; isAbiSupplied: boolean; isAbiValid: boolean; isBusy: boolean; isNameValid: boolean; name?: string | null; tags: string[]; } class ContractModal

extends TxComponent { // horrible :( protected defaultState: S = { accountId: null, gasLimit: new BN(0), isAbiSupplied: false, isAbiValid: false, isBusy: false, isNameValid: false, name: null, tags: [] as string[] } as S; public state: S = this.defaultState; protected isContract?: boolean; public render (): React.ReactNode { const { isOpen, t } = this.props; return ( {this.renderContent()} {this.renderButtons()} ); } protected headerText = ''; protected renderContent: () => React.ReactNode | null = (): React.ReactNode => null; protected renderButtons: () => React.ReactNode | null = (): React.ReactNode => null; protected renderInputAbi (): React.ReactNode { const { t } = this.props; const { isBusy } = this.state; return ( ); } protected renderInputAccount (): React.ReactNode { const { t } = this.props; const { accountId, isBusy } = this.state; return ( ); } protected renderInputName (): React.ReactNode { const { isNew, t } = this.props; const { isBusy, isNameValid, name } = this.state; return ( ); } protected renderInputGas (): React.ReactNode { const { t } = this.props; const { gasLimit, isBusy } = this.state; const isGasValid = !gasLimit.isZero(); return ( ); } protected reset = (): void => { this.setState( this.defaultState ); } protected toggleBusy = (isBusy?: boolean): () => void => (): void => { this.setState((state: S): S => { return { isBusy: isBusy === undefined ? !state.isBusy : isBusy } as unknown as S; }); } protected onClose = (): void => { const { onClose } = this.props; const { isBusy } = this.state; onClose && onClose(); if (!isBusy) { this.reset(); } } protected onAddAbi = (abi: string | null | undefined, contractAbi: Abi | null = null, isAbiSupplied = false): void => { this.setState({ abi, contractAbi, isAbiSupplied, isAbiValid: !!abi }); } protected onChangeAccount = (accountId: string | null): void => { this.setState({ accountId }); } protected onChangeName = (name: string): void => { this.setState({ name, isNameValid: name.length !== 0 }); } protected onChangeGas = (gasLimit: BN | undefined): void => { this.setState({ gasLimit: gasLimit || new BN(0) }); } } export default ContractModal;