Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import React, {useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled/macro';
  3. import * as yup from 'yup';
  4. import { FontSize } from '../constants/base';
  5. import MessageBox from './MessageBox';
  6. import { authenticate } from '../service/user';
  7.  
  8. const Form = styled.form`
  9. font-family: Noto Sans JP;
  10. width: 25%;
  11. `;
  12.  
  13. const Field = styled.input`
  14. display: block;
  15. width: 100%;
  16. height: 40px;
  17. font-size: ${FontSize.Regular};
  18. padding: 0 10px;
  19. margin-bottom: 10px;
  20. border: 1px solid #ccc;
  21. border-radius: 5px;
  22. box-sizing: border-box;
  23. `;
  24.  
  25. const SignInButton = styled.button`
  26. background: #333;
  27. color: #fff;
  28. font-size: ${FontSize.Regular};
  29. width: 100%;
  30. height: 40px;
  31. border-radius: 5px;
  32. font-weight: bold;
  33. `;
  34.  
  35. const SignInForm: React.FC = () => {
  36. const [email, setEmail] = useState<string>('');
  37. const [password, setPassword] = useState<string>('');
  38. const [messages, setMessages] = useState<string[]|null>(null);
  39. const [canSubmit, setCanSubmit] = useState<boolean>(false);
  40. const [hasError, setHasError] = useState<boolean>(false);
  41.  
  42. const onChangeField = useCallback(
  43. (event: React.ChangeEvent<HTMLInputElement>) => {
  44. switch (event.target.name) {
  45. case 'email':
  46. setEmail(event.target.value);
  47. break;
  48. case 'password':
  49. setPassword(event.target.value);
  50. break;
  51. }
  52. },
  53. [],
  54. );
  55.  
  56. const validate = async (values: {[key: string]: string}): Promise<void> => {
  57. const signInScheme = yup.object().shape({
  58. email: yup
  59. .string()
  60. .email('Email must be a valid.')
  61. .required('Email must not be a empty.'),
  62. password: yup
  63. .string()
  64. .min(8, 'Password must be at least 8 characters.')
  65. .matches(/^[A-Za-z0-9●!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]+$/, 'Password must be a valid.')
  66. .required('Password must not be a empty.')
  67. });
  68.  
  69. try {
  70. await signInScheme.validate(values, { abortEarly: false });
  71. } catch (error) {
  72. throw error;
  73. }
  74. };
  75.  
  76. const onSubmit = useCallback(
  77. async (event: React.FormEvent<HTMLFormElement>): Promise<void> => {
  78. event.preventDefault();
  79.  
  80. const formValues = {
  81. email,
  82. password,
  83. };
  84.  
  85. try {
  86. await validate(formValues);
  87. setCanSubmit(true);
  88. setHasError(false);
  89. } catch (error) {
  90. setMessages(error.errors);
  91. setCanSubmit(false);
  92. setHasError(true);
  93. }
  94.  
  95. if (canSubmit) {
  96. setMessages(null);
  97. try {
  98. await authenticate(email, password);
  99. setEmail('');
  100. setPassword('');
  101. setMessages(['Sign In Successfully.']);
  102. setCanSubmit(false);
  103. setHasError(false);
  104. } catch (error) {
  105. setMessages([error.message]);
  106. setCanSubmit(false);
  107. setHasError(true);
  108. }
  109. }
  110. },
  111. [email, password, canSubmit],
  112. );
  113.  
  114. return (
  115. <Form
  116. onSubmit={onSubmit}
  117. >
  118. <Field
  119. name='email'
  120. placeholder='Email'
  121. type='email'
  122. value={email}
  123. onChange={onChangeField}
  124. />
  125. <Field
  126. name='password'
  127. placeholder='Password'
  128. type='password'
  129. value={password}
  130. onChange={onChangeField}
  131. />
  132. {messages &&
  133. <MessageBox
  134. messages={messages}
  135. status={hasError ? 'failure' : 'success'}
  136. />}
  137. <SignInButton>
  138. SIGN IN
  139. </SignInButton>
  140. </Form>
  141. )
  142. };
  143.  
  144. export default SignInForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement