SetPasswordForm.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. <p>{constants.ENTER_ENC_PASSPHRASE}</p>
  50. {constants.PASSPHRASE_DISCLAIMER()}
  51. </div>
  52. <Formik<formValues>
  53. initialValues={{ passphrase: '', confirm: '' }}
  54. validationSchema={Yup.object().shape({
  55. passphrase: Yup.string().required(
  56. constants.REQUIRED
  57. ),
  58. confirm: Yup.string().required(constants.REQUIRED),
  59. })}
  60. validateOnChange={false}
  61. validateOnBlur={false}
  62. onSubmit={onSubmit}>
  63. {({
  64. values,
  65. touched,
  66. errors,
  67. handleChange,
  68. handleSubmit,
  69. }) => (
  70. <Form noValidate onSubmit={handleSubmit}>
  71. <Form.Group>
  72. <Form.Control
  73. type="password"
  74. placeholder={constants.PASSPHRASE_HINT}
  75. value={values.passphrase}
  76. onChange={handleChange('passphrase')}
  77. isInvalid={Boolean(
  78. touched.passphrase &&
  79. errors.passphrase
  80. )}
  81. autoFocus
  82. disabled={loading}
  83. />
  84. <Form.Control.Feedback type="invalid">
  85. {errors.passphrase}
  86. </Form.Control.Feedback>
  87. </Form.Group>
  88. <Form.Group>
  89. <Form.Control
  90. type="password"
  91. placeholder={
  92. constants.RE_ENTER_PASSPHRASE
  93. }
  94. value={values.confirm}
  95. onChange={handleChange('confirm')}
  96. isInvalid={Boolean(
  97. touched.confirm && errors.confirm
  98. )}
  99. disabled={loading}
  100. />
  101. <Form.Control.Feedback type="invalid">
  102. {errors.confirm}
  103. </Form.Control.Feedback>
  104. </Form.Group>
  105. <SubmitButton
  106. buttonText={props.buttonText}
  107. loading={loading}
  108. />
  109. </Form>
  110. )}
  111. </Formik>
  112. {props.back && (
  113. <div
  114. className="text-center"
  115. style={{ marginTop: '20px' }}>
  116. <Button variant="link" onClick={props.back}>
  117. {constants.GO_BACK}
  118. </Button>
  119. </div>
  120. )}
  121. </Card.Body>
  122. </Card>
  123. </Container>
  124. );
  125. }
  126. export default SetPasswordForm;