index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2017-2019 @polkadot/app-staking authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { I18nProps } from '@polkadot/react-components/types';
  5. import { ComponentProps } from '../types';
  6. import React from 'react';
  7. import { RouteComponentProps } from 'react-router';
  8. import { withRouter } from 'react-router-dom';
  9. import { Button, CardGrid } from '@polkadot/react-components';
  10. import { withMulti } from '@polkadot/react-api';
  11. import translate from '../translate';
  12. import Add from './Add';
  13. import Contract from './Contract';
  14. import Call from './Call';
  15. type Props = ComponentProps & I18nProps & RouteComponentProps;
  16. interface State {
  17. isAddOpen: boolean;
  18. isCallOpen: boolean;
  19. callAddress: string | null;
  20. callMethod: string | null;
  21. }
  22. class Contracts extends React.PureComponent<Props, State> {
  23. public state: State = {
  24. callAddress: null,
  25. callMethod: null,
  26. isAddOpen: false,
  27. isCallOpen: false
  28. };
  29. public render (): React.ReactNode {
  30. const { accounts, basePath, contracts, hasCode, showDeploy, t } = this.props;
  31. const { callAddress, callMethod, isAddOpen, isCallOpen } = this.state;
  32. return (
  33. <>
  34. <CardGrid
  35. emptyText={t('No contracts available')}
  36. buttons={
  37. <Button.Group>
  38. {hasCode && (
  39. <>
  40. <Button
  41. isPrimary
  42. label={t('Deploy a code hash')}
  43. labelIcon='cloud upload'
  44. onClick={showDeploy()}
  45. />
  46. <Button.Or />
  47. </>
  48. )}
  49. <Button
  50. label={t('Add an existing contract')}
  51. labelIcon='add'
  52. onClick={this.showAdd}
  53. />
  54. </Button.Group>
  55. }
  56. >
  57. {accounts && contracts && Object.keys(contracts).map((address): React.ReactNode => {
  58. return (
  59. <Contract
  60. basePath={basePath}
  61. address={address}
  62. key={address}
  63. onCall={this.showCall}
  64. />
  65. );
  66. })}
  67. </CardGrid>
  68. <Add
  69. basePath={basePath}
  70. isOpen={isAddOpen}
  71. onClose={this.hideAdd}
  72. />
  73. <Call
  74. address={callAddress}
  75. isOpen={isCallOpen}
  76. method={callMethod}
  77. onClose={this.hideCall}
  78. />
  79. </>
  80. );
  81. }
  82. private showAdd = (): void => {
  83. this.setState({
  84. isAddOpen: true
  85. });
  86. }
  87. private hideAdd = (): void => {
  88. this.setState({
  89. isAddOpen: false
  90. });
  91. }
  92. private showCall = (callAddress?: string, callMethod?: string): void => {
  93. this.setState({
  94. isCallOpen: true,
  95. callAddress: callAddress || null,
  96. callMethod: callMethod || null
  97. });
  98. }
  99. private hideCall = (): void => {
  100. this.setState({
  101. isCallOpen: false,
  102. callAddress: null,
  103. callMethod: null
  104. });
  105. }
  106. }
  107. export default withMulti(
  108. Contracts,
  109. translate,
  110. withRouter
  111. );