Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. import React from "react";
  2. import { Formik } from "formik";
  3. import * as Yup from "yup";
  4. import styled, { keyframes } from "styled-components";
  5. import { Mutation, Query } from "react-apollo";
  6. import { Button, Flex, Card, Box } from "rebass";
  7. import gql from "graphql-tag";
  8. import Link from "./Link";
  9. import Text from "./Text";
  10. import Error from "./ErrorMessage";
  11. import User, { CURRENT_USER_QUERY } from "./User";
  12.  
  13.  
  14. const UPDATE_USER_MUTATION = gql`
  15. mutation UPDATE_USER_MUTATION(
  16. $id: ID!
  17. $email: String
  18. $firstName: String
  19. $lastName: String
  20. ) {
  21. updateUser(
  22. id: $id
  23. email: $email
  24. firstName: $firstName
  25. lastName: $lastName
  26. ) {
  27. id
  28. email
  29. firstName
  30. lastName
  31. }
  32. }
  33. `;
  34.  
  35. const StyledInput = styled(Box).attrs({
  36. as: "input",
  37. })`
  38. font-size: 1em;
  39. border-radius: 3px;
  40. width: 15em;
  41. border: 1px solid ${props => props.theme.colors.greys[1]};
  42. padding: 0.5em;
  43. `;
  44.  
  45. const UpdateSchema = Yup.object().shape({
  46. email: Yup.string()
  47. .email("Invalid email address")
  48. .required("You must enter an email address"),
  49. firstName: Yup.string()
  50. .min(2, "Must be longer than 2 characters")
  51. .required("Please enter your first name"),
  52. lastName: Yup.string()
  53. .min(2, "Must be longer than 2 characters")
  54. .required("Please enter your last name"),
  55. });
  56.  
  57. const Settings = props => (
  58. <Query query={CURRENT_USER_QUERY} variables={{ id: props.id }}>
  59. {({ data: { currentUser } }) => (
  60. <Mutation mutation={UPDATE_USER_MUTATION}>
  61. {(updateUser, { error, loading, called }) => (
  62. <Formik
  63. initialValues={{
  64. id: props.userId,
  65. email: currentUser.email,
  66. firstName: currentUser.firstName,
  67. lastName: currentUser.lastName,
  68. }}
  69. validationSchema={UpdateSchema}
  70. onSubmit={values => {
  71. updateUser({
  72. variables: {
  73. id: currentUser.id,
  74. email: values.email,
  75. firstName: values.firstName,
  76. lastName: values.lastName,
  77. },
  78. });
  79. }}
  80. render={({
  81. values,
  82. errors,
  83. touched,
  84. handleChange,
  85. handleBlur,
  86. handleSubmit,
  87. isSubmitting,
  88. }) => (
  89. <Flex
  90. flexDirection={["column"]}
  91. mb={[2, 0]}
  92. width={1}
  93. alignItems="center"
  94. height={1}
  95. >
  96. <form onSubmit={handleSubmit}>
  97. <fieldset disabled={loading} aria-busy={loading}>
  98. <Box>
  99. <Error error={error} />
  100. <Box mb="30px">
  101. <label htmlFor="email">
  102. <Text my={2}>Email</Text>
  103. <StyledInput
  104. placeholder="Enter your email address"
  105. type="email"
  106. name="email"
  107. onChange={handleChange}
  108. onBlur={handleBlur}
  109. value={values.email}
  110. />
  111.  
  112. {!error && !loading && called && (
  113. <Text fontSize={1} color="green" pt={1}>
  114. Email successfully updated!
  115. </Text>
  116. )}
  117.  
  118. {touched.email && errors && errors.email && (
  119. <Text fontSize={1} color="red" pt={1}>
  120. {errors.email}
  121. </Text>
  122. )}
  123. </label>
  124. </Box>
  125.  
  126. <Box mb="30px">
  127. <label htmlFor="First Name">
  128. <Text my={2}>First Name</Text>
  129. <StyledInput
  130. placeholder="Please enter your first name"
  131. type="text"
  132. name="firstName"
  133. onChange={handleChange}
  134. onBlur={handleBlur}
  135. value={values.firstName}
  136. />
  137.  
  138. {!error && !loading && called && (
  139. <Text fontSize={1} color="green" pt={1}>
  140. First name updated!
  141. </Text>
  142. )}
  143.  
  144. {touched.firstName && errors && errors.firstName && (
  145. <Text fontSize={1} color="red" pt={1}>
  146. {errors.firstName}
  147. </Text>
  148. )}
  149. </label>
  150. </Box>
  151.  
  152. <Box mb="30px">
  153. <label htmlFor="Last Name">
  154. <Text my={2}>Last Name</Text>
  155. <StyledInput
  156. placeholder="Please enter your last name"
  157. type="text"
  158. name="lastName"
  159. onChange={handleChange}
  160. onBlur={handleBlur}
  161. value={values.lastName}
  162. />
  163.  
  164. {!error && !loading && called && (
  165. <Text fontSize={1} color="green" pt={1}>
  166. Last name updated!
  167. </Text>
  168. )}
  169.  
  170. {touched.lastName && errors && errors.lastName && (
  171. <Text fontSize={1} color="red" pt={1}>
  172. {errors.lastName}
  173. </Text>
  174. )}
  175. </label>
  176. </Box>
  177. <Box>
  178. <Button
  179. type="submit"
  180. disabled={
  181. isSubmitting ||
  182. !!(errors.email && touched.email) ||
  183. !!(errors.firstName && touched.firstName) ||
  184. !!(errors.lastName && touched.lastName)
  185. }
  186. width={1}
  187. primary
  188. >
  189. Update
  190. </Button>
  191. </Box>
  192. </Box>
  193. </fieldset>
  194. </form>
  195. </Flex>
  196. )}
  197. />
  198. )}
  199. </Mutation>
  200. )}
  201. </Query>
  202. );
  203.  
  204. export default Settings;
  205. export { UPDATE_USER_MUTATION };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement