SetPasswordForm.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { useState } from 'react';
  2. import Container from 'components/Container';
  3. import Card from 'react-bootstrap/Card';
  4. import Form from 'react-bootstrap/Form';
  5. import constants from 'utils/strings/constants';
  6. import { Formik, FormikHelpers } from 'formik';
  7. import * as Yup from 'yup';
  8. import Button from 'react-bootstrap/Button';
  9. import SubmitButton from './SubmitButton';
  10. interface Props {
  11. callback: (passphrase: any, setFieldError: any) => Promise<void>;
  12. buttonText: string;
  13. back: () => void;
  14. }
  15. interface formValues {
  16. passphrase: string;
  17. confirm: string;
  18. }
  19. function SetPasswordForm(props: Props) {
  20. const [loading, setLoading] = useState(false);
  21. const onSubmit = async (
  22. values: formValues,
  23. { setFieldError }: FormikHelpers<formValues>,
  24. ) => {
  25. setLoading(true);
  26. try {
  27. const { passphrase, confirm } = values;
  28. if (passphrase === confirm) {
  29. await props.callback(passphrase, setFieldError);
  30. } else {
  31. setFieldError('confirm', constants.PASSPHRASE_MATCH_ERROR);
  32. }
  33. } catch (e) {
  34. setFieldError(
  35. 'passphrase',
  36. `${constants.UNKNOWN_ERROR} ${e.message}`,
  37. );
  38. } finally {
  39. setLoading(false);
  40. }
  41. };
  42. return (
  43. <Container>
  44. <Card style={{ maxWidth: '540px', padding: '20px' }}>
  45. <Card.Body>
  46. <div
  47. className="text-center"
  48. style={{ marginBottom: '40px' }}
  49. >
  50. <p>{constants.ENTER_ENC_PASSPHRASE}</p>
  51. {constants.PASSPHRASE_DISCLAIMER()}
  52. </div>
  53. <Formik<formValues>
  54. initialValues={{ passphrase: '', confirm: '' }}
  55. validationSchema={Yup.object().shape({
  56. passphrase: Yup.string().required(
  57. constants.REQUIRED,
  58. ),
  59. confirm: Yup.string().required(constants.REQUIRED),
  60. })}
  61. validateOnChange={false}
  62. validateOnBlur={false}
  63. onSubmit={onSubmit}
  64. >
  65. {({
  66. values,
  67. touched,
  68. errors,
  69. handleChange,
  70. handleSubmit,
  71. }) => (
  72. <Form noValidate onSubmit={handleSubmit}>
  73. <Form.Group>
  74. <Form.Control
  75. type="password"
  76. placeholder={constants.PASSPHRASE_HINT}
  77. value={values.passphrase}
  78. onChange={handleChange('passphrase')}
  79. isInvalid={Boolean(
  80. touched.passphrase &&
  81. errors.passphrase,
  82. )}
  83. autoFocus
  84. disabled={loading}
  85. />
  86. <Form.Control.Feedback type="invalid">
  87. {errors.passphrase}
  88. </Form.Control.Feedback>
  89. </Form.Group>
  90. <Form.Group>
  91. <Form.Control
  92. type="password"
  93. placeholder={
  94. constants.RE_ENTER_PASSPHRASE
  95. }
  96. value={values.confirm}
  97. onChange={handleChange('confirm')}
  98. isInvalid={Boolean(
  99. touched.confirm && errors.confirm,
  100. )}
  101. disabled={loading}
  102. />
  103. <Form.Control.Feedback type="invalid">
  104. {errors.confirm}
  105. </Form.Control.Feedback>
  106. </Form.Group>
  107. <SubmitButton
  108. buttonText={props.buttonText}
  109. loading={loading}
  110. />
  111. </Form>
  112. )}
  113. </Formik>
  114. {props.back && (
  115. <div
  116. className="text-center"
  117. style={{ marginTop: '20px' }}
  118. >
  119. <Button variant="link" onClick={props.back}>
  120. {constants.GO_BACK}
  121. </Button>
  122. </div>
  123. )}
  124. </Card.Body>
  125. </Card>
  126. </Container>
  127. );
  128. }
  129. export default SetPasswordForm;