Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import React from "react";
  2. import {
  3. RegularRegistrationInfo,
  4. ValidateEmail,
  5. validateEmail,
  6. ValidateName,
  7. validateName,
  8. ValidatePassword,
  9. validatePassword
  10. } from "running_app_core";
  11. import { Input } from "../Input";
  12. interface Validators {
  13. validateName: ValidateName;
  14. validateEmail: ValidateEmail;
  15. validatePassword: ValidatePassword;
  16. }
  17. export interface RegistrationProps {
  18. readonly name: string;
  19. readonly email: string;
  20. readonly password: string;
  21. readonly passwordCopy: string;
  22. readonly changeName: (name: string) => void;
  23. readonly changeEmail: (email: string) => void;
  24. readonly changePassword: (password: string) => void;
  25. readonly changePasswordConfirmation: (password: string) => void;
  26. readonly register: (userInfo: RegularRegistrationInfo) => void;
  27. }
  28. export const RegistrationFactory = (props: RegistrationProps & Validators) => {
  29. const userInfo = {
  30. name: props.name,
  31. email: props.email,
  32. password: props.password,
  33. passwordConfirmation: props.passwordCopy
  34. };
  35. const isLoginValid = props.validateName(userInfo.name);
  36. const isEmailValid = props.validateEmail(userInfo.email);
  37. const isPasswordValid = props.validatePassword(userInfo.password);
  38. const isPasswordCopyValid = props.passwordCopy === userInfo.password;
  39. const userInfoValid =
  40. isLoginValid && isEmailValid && isPasswordValid && isPasswordCopyValid;
  41. return (
  42. <div>
  43. <Input
  44. id="name"
  45. label="Name"
  46. errorMessage={
  47. isLoginValid ? undefined : "name should be in range from 2 to 128"
  48. }
  49. onChange={props.changeName}
  50. />
  51. <Input
  52. id="email"
  53. label="Email"
  54. errorMessage={isEmailValid ? undefined : "email is invalid"}
  55. onChange={props.changeEmail}
  56. />
  57. <Input
  58. id="password"
  59. label="Password"
  60. errorMessage={
  61. isPasswordValid
  62. ? undefined
  63. : "password should be in range from 5 to 128"
  64. }
  65. onChange={props.changePassword}
  66. />
  67. <Input
  68. id="passwordCopy"
  69. label="Repeat your password"
  70. errorMessage={
  71. isPasswordCopyValid ? undefined : "passwords do not match"
  72. }
  73. onChange={props.changePasswordConfirmation}
  74. />
  75. <button
  76. onClick={
  77. userInfoValid ? props.register.bind(null, userInfo) : undefined
  78. }
  79. >
  80. Register
  81. </button>
  82. </div>
  83. );
  84. };
  85. export const Registration = (props: RegistrationProps) => (
  86. <RegistrationFactory
  87. {...props}
  88. validateName={validateName}
  89. validateEmail={validateEmail}
  90. validatePassword={validatePassword}
  91. />
  92. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement