Guest User

Untitled

a guest
Nov 19th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. import * as React from 'react';
  2. import { Link } from '@reach/router';
  3. import { Formik, Form, Field, ErrorMessage } from 'formik';
  4. import * as yup from 'yup';
  5. import * as R from 'ramda';
  6.  
  7. import {
  8. Box,
  9. Button,
  10. Text,
  11. TextInput,
  12. Flex,
  13. InlineNotice,
  14. Notice,
  15. } from '../alchemy';
  16. import { IdentityPage } from './IdentityPage';
  17. import { TokenConsumer } from '../TokenProvider';
  18. import { IBoxProps } from '../../typings/alchemy';
  19.  
  20. export interface ISignInFormValues {
  21. password: string;
  22. username: string;
  23. }
  24.  
  25. const validationSchema = yup.object().shape({
  26. password: yup.string().required('password.required'),
  27. username: yup
  28. .string()
  29. .required('username.required')
  30. .email('username.invalid'),
  31. });
  32.  
  33. const TextInputField = (props: any) => {
  34. const { field, ...otherProps } = props;
  35.  
  36. return <TextInput {...field} {...otherProps} />;
  37. };
  38.  
  39. const FormLabel = (props: any) => {
  40. return <Text as="label" fontWeight="bold" mb={2} {...props} />;
  41. };
  42. function ErrorNotice(props: { message: string } & IBoxProps) {
  43. const { message, ...otherProps } = props;
  44.  
  45. return (
  46. <InlineNotice type="danger" {...otherProps}>
  47. {message}
  48. </InlineNotice>
  49. );
  50. }
  51.  
  52. export function GlobalErrorNotice(
  53. props: { message: string | null } & IBoxProps,
  54. ) {
  55. const { message, ...otherProps } = props;
  56.  
  57. if (R.isNil(message)) {
  58. return null;
  59. }
  60.  
  61. return <Notice title={props.message} type="danger" {...otherProps} />;
  62. }
  63.  
  64. export function SignInPage() {
  65. return (
  66. <IdentityPage heading="Sign in to your account" title="Sign in to Insight">
  67. <TokenConsumer>
  68. {({ signIn, globalErrorMessage }) => {
  69. return (
  70. <Formik<ISignInFormValues>
  71. onSubmit={signIn}
  72. initialValues={{
  73. password: '',
  74. username: '',
  75. }}
  76. validateOnBlur={false}
  77. validateOnChange={false}
  78. validationSchema={validationSchema}
  79. >
  80. {props => {
  81. const buttonCopy = props.isSubmitting
  82. ? 'Signing in...'
  83. : 'Sign in';
  84.  
  85. return (
  86. <>
  87. <GlobalErrorNotice
  88. data-testid="error-sign-in-global"
  89. message={globalErrorMessage}
  90. mb={3}
  91. />
  92. <Box as={Form} data-testid="form-sign-in" width={1}>
  93. <Flex flexDirection="column" mb={3}>
  94. <FormLabel htmlFor="username" title="Email" mb={2}>
  95. Email address
  96. </FormLabel>
  97. <Field
  98. component={TextInputField}
  99. data-testid="input-username"
  100. name="username"
  101. id="username"
  102. placeholder="Email address"
  103. width={1}
  104. />
  105. <ErrorMessage name="username">
  106. {message => (
  107. <ErrorNotice
  108. data-testid="error-sign-in-username"
  109. message={message}
  110. mt={2}
  111. />
  112. )}
  113. </ErrorMessage>
  114. </Flex>
  115. <Flex flexDirection="column" mb={3}>
  116. <FormLabel htmlFor="password" title="Password" mb={2}>
  117. Password
  118. </FormLabel>
  119. <Field
  120. component={TextInputField}
  121. data-testid="input-password"
  122. type="password"
  123. name="password"
  124. id="password"
  125. placeholder="Password"
  126. width={1}
  127. />
  128. <ErrorMessage name="password">
  129. {message => (
  130. <ErrorNotice
  131. data-testid="error-sign-in-password"
  132. message={message}
  133. mt={2}
  134. />
  135. )}
  136. </ErrorMessage>
  137. </Flex>
  138. <Button
  139. data-testid="button-submit"
  140. type="submit"
  141. variant="primary"
  142. disabled={props.isSubmitting}
  143. width={1}
  144. >
  145. {buttonCopy}
  146. </Button>
  147. </Box>
  148. </>
  149. );
  150. }}
  151. </Formik>
  152. );
  153. }}
  154. </TokenConsumer>
  155. <Box mt={3}>
  156. <Link to="/begin_reset_password">Forgot your password?</Link>
  157. </Box>
  158. </IdentityPage>
  159. );
  160. }
Add Comment
Please, Sign In to add comment