SetMaxValidatorCountForm.tsx 2.7 KB

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