DecreaseWorkingGroupLeadStakeForm.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React, { useState, useEffect } from 'react';
  2. import { getFormErrorLabelsProps } from './errorHandling';
  3. import * as Yup from 'yup';
  4. import {
  5. withProposalFormData,
  6. ProposalFormExportProps,
  7. ProposalFormContainerProps,
  8. ProposalFormInnerProps,
  9. genericFormDefaultOptions
  10. } from './GenericProposalForm';
  11. import {
  12. GenericWorkingGroupProposalForm,
  13. FormValues as WGFormValues,
  14. defaultValues as wgFromDefaultValues
  15. } from './GenericWorkingGroupProposalForm';
  16. import { InputFormField } from './FormFields';
  17. import { withFormContainer } from './FormContainer';
  18. import { Grid } from 'semantic-ui-react';
  19. import { formatBalance } from '@polkadot/util';
  20. import _ from 'lodash';
  21. import Validation from '../validationSchema';
  22. import { WorkerData } from '@polkadot/joy-utils/types/workingGroups';
  23. export type FormValues = WGFormValues & {
  24. amount: string;
  25. };
  26. const defaultValues: FormValues = {
  27. ...wgFromDefaultValues,
  28. amount: ''
  29. };
  30. type FormAdditionalProps = {}; // Aditional props coming all the way from export component into the inner form.
  31. type ExportComponentProps = ProposalFormExportProps<FormAdditionalProps, FormValues>;
  32. type FormContainerProps = ProposalFormContainerProps<ExportComponentProps>;
  33. type FormInnerProps = ProposalFormInnerProps<FormContainerProps, FormValues>;
  34. const DecreaseWorkingGroupLeadStakeForm: React.FunctionComponent<FormInnerProps> = props => {
  35. const { handleChange, errors, touched, values, myMemberId, setFieldError } = props;
  36. const errorLabelsProps = getFormErrorLabelsProps<FormValues>(errors, touched);
  37. const [lead, setLead] = useState<WorkerData | null>(null);
  38. // Here we validate if stake <= current lead stake.
  39. // Because it depends on selected working group,
  40. // there's no easy way to do it using validationSchema
  41. useEffect(() => {
  42. if (lead && parseInt(values.amount) > (lead.stake || 0) && !errors.amount) {
  43. setFieldError('amount', `The stake cannot exceed current leader's stake (${formatBalance(lead.stake)})`);
  44. }
  45. });
  46. return (
  47. <GenericWorkingGroupProposalForm
  48. {...props}
  49. txMethod="createDecreaseWorkingGroupLeaderStakeProposal"
  50. proposalType="DecreaseWorkingGroupLeaderStake"
  51. leadRequired={true}
  52. leadStakeRequired={true}
  53. onLeadChange={(lead: WorkerData | null) => setLead(lead)}
  54. submitParams={[
  55. myMemberId,
  56. values.title,
  57. values.rationale,
  58. '{STAKE}',
  59. lead?.workerId,
  60. values.amount,
  61. values.workingGroup
  62. ]}
  63. >
  64. { (lead && lead.stake) && (
  65. <Grid columns="4" doubling stackable verticalAlign="bottom">
  66. <Grid.Column>
  67. <InputFormField
  68. label="Amount to decrease"
  69. onChange={handleChange}
  70. name="amount"
  71. error={errorLabelsProps.amount}
  72. value={values.amount}
  73. unit={formatBalance.getDefaults().unit}
  74. />
  75. </Grid.Column>
  76. </Grid>
  77. ) }
  78. </GenericWorkingGroupProposalForm>
  79. );
  80. };
  81. const FormContainer = withFormContainer<FormContainerProps, FormValues>({
  82. mapPropsToValues: (props: FormContainerProps) => ({
  83. ...defaultValues,
  84. ...(props.initialData || {})
  85. }),
  86. validationSchema: Yup.object().shape({
  87. ...genericFormDefaultOptions.validationSchema,
  88. ...Validation.DecreaseWorkingGroupLeaderStake()
  89. }),
  90. handleSubmit: genericFormDefaultOptions.handleSubmit,
  91. displayName: 'DecreaseWorkingGroupLeadStakeForm'
  92. })(DecreaseWorkingGroupLeadStakeForm);
  93. export default withProposalFormData<FormContainerProps, ExportComponentProps>(FormContainer);