SpendingProposalForm.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React from 'react';
  2. import { getFormErrorLabelsProps } from './errorHandling';
  3. import * as Yup from 'yup';
  4. import { Label } from 'semantic-ui-react';
  5. import {
  6. GenericProposalForm,
  7. GenericFormValues,
  8. genericFormDefaultOptions,
  9. genericFormDefaultValues,
  10. withProposalFormData,
  11. ProposalFormExportProps,
  12. ProposalFormContainerProps,
  13. ProposalFormInnerProps
  14. } from './GenericProposalForm';
  15. import Validation from '../validationSchema';
  16. import { InputFormField, FormField } from './FormFields';
  17. import { withFormContainer } from './FormContainer';
  18. import { InputAddress } from '@polkadot/react-components/index';
  19. import { formatBalance } from '@polkadot/util';
  20. import './forms.css';
  21. export type FormValues = GenericFormValues & {
  22. destinationAccount: any;
  23. tokens: string;
  24. };
  25. const defaultValues: FormValues = {
  26. ...genericFormDefaultValues,
  27. destinationAccount: '',
  28. tokens: ''
  29. };
  30. type FormAdditionalProps = {}; // Aditional props coming all the way from export comonent into the inner form.
  31. type ExportComponentProps = ProposalFormExportProps<FormAdditionalProps, FormValues>;
  32. type FormContainerProps = ProposalFormContainerProps<ExportComponentProps>;
  33. type FormInnerProps = ProposalFormInnerProps<FormContainerProps, FormValues>;
  34. const SpendingProposalForm: React.FunctionComponent<FormInnerProps> = props => {
  35. const { handleChange, errors, touched, values, setFieldValue } = props;
  36. const errorLabelsProps = getFormErrorLabelsProps<FormValues>(errors, touched);
  37. return (
  38. <GenericProposalForm
  39. {...props}
  40. txMethod="createSpendingProposal"
  41. proposalType="Spending"
  42. submitParams={[
  43. props.myMemberId,
  44. values.title,
  45. values.rationale,
  46. '{STAKE}',
  47. values.tokens,
  48. values.destinationAccount
  49. ]}
  50. >
  51. <InputFormField
  52. label="Amount of tokens"
  53. help="The amount of tokens you propose to spend"
  54. onChange={handleChange}
  55. name="tokens"
  56. placeholder="100"
  57. error={errorLabelsProps.tokens}
  58. unit={ formatBalance.getDefaults().unit }
  59. value={values.tokens}
  60. />
  61. <FormField
  62. error={errorLabelsProps.destinationAccount}
  63. label="Destination account"
  64. help="The account you propose to send the tokens into"
  65. >
  66. <InputAddress
  67. onChange={address => setFieldValue('destinationAccount', address)}
  68. type="all"
  69. placeholder="Select Destination Account"
  70. value={values.destinationAccount}
  71. />
  72. {errorLabelsProps.destinationAccount && <Label {...errorLabelsProps.destinationAccount} prompt />}
  73. </FormField>
  74. </GenericProposalForm>
  75. );
  76. };
  77. const FormContainer = withFormContainer<FormContainerProps, FormValues>({
  78. mapPropsToValues: (props: FormContainerProps) => ({
  79. ...defaultValues,
  80. ...(props.initialData || {})
  81. }),
  82. validationSchema: Yup.object().shape({
  83. ...genericFormDefaultOptions.validationSchema,
  84. ...Validation.Spending()
  85. }),
  86. handleSubmit: genericFormDefaultOptions.handleSubmit,
  87. displayName: 'SpendingProposalsForm'
  88. })(SpendingProposalForm);
  89. export default withProposalFormData<FormContainerProps, ExportComponentProps>(FormContainer);