Guest User

Untitled

a guest
Nov 12th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Formik, Field, Form } from "formik";
  3. import { Mutation } from "react-apollo";
  4. import gql from "graphql-tag";
  5. import * as Yup from "yup";
  6. import Menu from "../Common/Menu/Menu";
  7. import Footer from "../Common/Footer/Footer";
  8. import styles from "./Register.module.scss";
  9.  
  10. const REGISTER_USER = gql`
  11. mutation register($name: String!, $password: String!, $email: String!) {
  12. register(name: $name, password: $password, email: $email) {
  13. status
  14. message
  15. }
  16. }
  17. `;
  18.  
  19. const registerMutation = values => {
  20. <Mutation mutation={REGISTER_USER}>
  21. {({ loading, error, data }) => {
  22. if (loading) console.log(loading);
  23. if (error) console.log(error);
  24. if (data) console.log(data);
  25. }}
  26. </Mutation>;
  27. };
  28.  
  29. const SignupSchema = Yup.object().shape({
  30. name: Yup.string()
  31. .min(2, "Too Short!")
  32. .max(15, "Too Long!")
  33. .required("This field is required"),
  34. password: Yup.string()
  35. .min(2, "Too Short!")
  36. .max(15, "Too Long!")
  37. .required("This field is required"),
  38. email: Yup.string()
  39. .email("Invalid email")
  40. .required("This field is required")
  41. });
  42.  
  43. class Register extends Component {
  44. render() {
  45. return (
  46. <div>
  47. <Menu />
  48. <div className={styles.Content}>
  49. <h1>Register</h1>
  50. <Formik
  51. initialValues={{ email: "", name: "", password: "" }}
  52. validationSchema={SignupSchema}
  53. onSubmit={(values, actions) => {
  54. setTimeout(() => {
  55. alert(JSON.stringify(values, null, 2));
  56. registerMutation(values);
  57. actions.setSubmitting(false);
  58. }, 1000);
  59. }}
  60. render={({ errors, touched }) => (
  61. <Form>
  62. <Field type="text" name="name" placeholder="Name" />
  63. {errors.name && touched.name ? <div>{errors.name}</div> : null}
  64. <Field
  65. type="email"
  66. name="email"
  67. placeholder="Enter a valid email "
  68. />
  69. {errors.email && touched.email ? (
  70. <div>{errors.email}</div>
  71. ) : null}
  72. <Field
  73. type="password"
  74. name="password"
  75. placeholder="Enter a password"
  76. />
  77. {errors.password && touched.password ? (
  78. <div>{errors.password}</div>
  79. ) : null}
  80. <button type="submit">Submit</button>
  81. </Form>
  82. )}
  83. />
  84. </div>
  85. <Footer />
  86. </div>
  87. );
  88. }
  89. }
  90.  
  91. export default Register;
Add Comment
Please, Sign In to add comment