SetMaxValidatorCountForm.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React, { useEffect } from 'react';
  2. import { getFormErrorLabelsProps } from './errorHandling';
  3. import * as Yup from 'yup';
  4. import {
  5. GenericProposalForm,
  6. GenericFormValues,
  7. genericFormDefaultOptions,
  8. genericFormDefaultValues,
  9. withProposalFormData,
  10. ProposalFormExportProps,
  11. ProposalFormContainerProps,
  12. ProposalFormInnerProps
  13. } from './GenericProposalForm';
  14. import Validation from '../validationSchema';
  15. import { InputFormField } from './FormFields';
  16. import { withFormContainer } from './FormContainer';
  17. import { useTransport, usePromise } from '@polkadot/joy-utils/react/hooks';
  18. import './forms.css';
  19. export type FormValues = GenericFormValues & {
  20. maxValidatorCount: string;
  21. };
  22. const defaultValues: FormValues = {
  23. ...genericFormDefaultValues,
  24. maxValidatorCount: ''
  25. };
  26. type FormAdditionalProps = {}; // Aditional props coming all the way from export comonent into the inner form.
  27. type ExportComponentProps = ProposalFormExportProps<FormAdditionalProps, FormValues>;
  28. type FormContainerProps = ProposalFormContainerProps<ExportComponentProps>;
  29. type FormInnerProps = ProposalFormInnerProps<FormContainerProps, FormValues>;
  30. const SetMaxValidatorCountForm: React.FunctionComponent<FormInnerProps> = props => {
  31. const transport = useTransport();
  32. const [validatorCount] = usePromise<number>(() => transport.validators.maxCount(), 20);
  33. const { handleChange, errors, touched, values, setFieldValue } = props;
  34. const errorLabelsProps = getFormErrorLabelsProps<FormValues>(errors, touched);
  35. useEffect(() => {
  36. if (validatorCount) {
  37. setFieldValue('maxValidatorCount', validatorCount);
  38. }
  39. }, [validatorCount]);
  40. return (
  41. <GenericProposalForm
  42. {...props}
  43. txMethod="createSetValidatorCountProposal"
  44. proposalType="SetValidatorCount"
  45. submitParams={[props.myMemberId, values.title, values.rationale, '{STAKE}', values.maxValidatorCount]}
  46. >
  47. <InputFormField
  48. error={errorLabelsProps.maxValidatorCount}
  49. label="Max Validator Count"
  50. help="The new value for maximum number of Validators that you propose"
  51. onChange={handleChange}
  52. name="maxValidatorCount"
  53. placeholder={validatorCount}
  54. value={values.maxValidatorCount}
  55. />
  56. </GenericProposalForm>
  57. );
  58. };
  59. const FormContainer = withFormContainer<FormContainerProps, FormValues>({
  60. mapPropsToValues: (props: FormContainerProps) => ({
  61. ...defaultValues,
  62. ...(props.initialData || {})
  63. }),
  64. validationSchema: Yup.object().shape({
  65. ...genericFormDefaultOptions.validationSchema,
  66. ...Validation.SetValidatorCount()
  67. }),
  68. handleSubmit: genericFormDefaultOptions.handleSubmit,
  69. displayName: 'SetMaxValidatorCountForm'
  70. })(SetMaxValidatorCountForm);
  71. export default withProposalFormData<FormContainerProps, ExportComponentProps>(FormContainer);