Guest User

Untitled

a guest
Feb 7th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { withFormik } from 'formik';
  3. import PropTypes from 'prop-types';
  4. import * as Yup from 'yup';
  5.  
  6. import { createUser, updateUser } from './service';
  7. import { listGroups } from '../groups/service';
  8.  
  9. const AddEditUserHOCForm = WrappedComponent => {
  10. class ViewUser extends Component {
  11. static propTypes = {
  12. user: PropTypes.object,
  13. onCancel: PropTypes.func,
  14. onSave: PropTypes.func,
  15. status: PropTypes.string,
  16. values: PropTypes.object,
  17. errors: PropTypes.object,
  18. isSubmitting: PropTypes.bool,
  19. handleChange: PropTypes.func,
  20. handleSubmit: PropTypes.func,
  21. setStatus: PropTypes.func
  22. };
  23.  
  24. static defaultProps = {
  25. user: {},
  26. onCancel: () => { },
  27. onSave: () => { },
  28. status: '',
  29. values: {},
  30. errors: {},
  31. isSubmitting: false,
  32. handleChange: () => { },
  33. handleSubmit: () => { },
  34. setStatus: () => { }
  35. };
  36.  
  37. state = {
  38. groups: [],
  39. isRetrievingData: false
  40. };
  41.  
  42. componentDidMount() {
  43. this.setState({
  44. isRetrievingData: true
  45. });
  46. return new Promise([listGroups()])
  47. .then(values => values.map(res => res.data))
  48. .then(([groups]) => {
  49. this.setState({
  50. isRetrievingData: false,
  51. groups
  52. });
  53. })
  54. .catch(({ message = 'Could not retrieve data from server.' }) => {
  55. this.setState({
  56. isRetrievingData: false
  57. });
  58. this.props.setStatus(message);
  59. });
  60. }
  61.  
  62. handleCancel = () => {
  63. const { onCancel } = this.props;
  64.  
  65. if (onCancel) {
  66. onCancel();
  67. }
  68. };
  69.  
  70. render() {
  71. return (
  72. <WrappedComponent
  73. {...this.props}
  74. {...this.state}
  75. onSubmit={this.handleSubmit}
  76. onCancel={this.handleCanel}
  77. />
  78. );
  79. }
  80. }
  81.  
  82. const UserValidationSchema = Yup.object().shape({
  83. username: Yup.string('Provide a Username').required('Username is Required'),
  84. password: Yup.string().email('Provide a Valid email Address'),
  85. confirmPassword: Yup.string('Enter your password again')
  86. .required('Password Confirmation is Required')
  87. .oneOf([Yup.ref('password')], 'Passwords do not match')
  88. });
  89.  
  90. const NewUserFormHOC = withFormik({
  91. mapPropsToValues: ({ user }) => ({ ...user }),
  92. UserValidationSchema,
  93.  
  94. handleSubmit: (values, { props, setSubmitting, setStatus }) => {
  95. const saveUser = values.username ? updateUser : createUser;
  96. return saveUser(values)
  97. .then(() => {
  98. setSubmitting(false);
  99. setStatus('');
  100. props.onSave(values);
  101. props.onCancel();
  102. })
  103. .catch(({ message, response: { data } }) => {
  104. setSubmitting(false);
  105. setStatus(data || message);
  106. });
  107. },
  108.  
  109. displayName: 'ViewUser'
  110. })(ViewUser);
  111. return NewUserFormHOC;
  112. };
  113.  
  114. export default AddEditUserHOCForm;
  115.  
  116. import React, { Component, Fragment } from 'react';
  117. import { Formik, Form, Field } from 'formik';
  118. import PropTypes from 'prop-types';
  119. import AddEditUserHOCForm from './components/AddEditUserHOC'
  120.  
  121. import LensesSelect from './data/ReactSelectComponent';
  122. import formPropTypes from '../constants/formPropTypes';
  123.  
  124. import { listGroups } from '../groups/service';
  125.  
  126. class UserCreateForm extends Component {
  127. static propTypes = {
  128. ...formPropTypes,
  129. username: PropTypes.string,
  130. email: PropTypes.string,
  131. password: PropTypes.string,
  132. confirmPassword: PropTypes.string,
  133. groupSelect: PropTypes.func
  134. };
  135.  
  136. static defaultProps = {
  137. email: ''
  138. };
  139.  
  140. state = {
  141. type: 'password',
  142. groups: []
  143. };
  144.  
  145. componentDidMount() {
  146. this.fetchListGroups();
  147. }
  148.  
  149. fetchListGroups = () => {
  150. listGroups().then(({ data }) => {
  151. this.setState({ groups: data });
  152. });
  153. };
  154.  
  155. mapListGroupToSelect = () => {
  156. const { groups } = this.state;
  157. return groups.map(group => ({
  158. label: group.name,
  159. value: group.name
  160. }));
  161. };
  162.  
  163. togglePasswordMask = e => {
  164. const { type } = this.state;
  165. e.preventDefault();
  166. this.setState(prevState => ({
  167. passwordIsMasked: !prevState.passwordIsMasked,
  168. type: type === 'password' ? 'input' : 'password'
  169. }));
  170. };
  171.  
  172. selectOnChangeCallback = response => {
  173. // eslint-disable-next-line no-console
  174. console.log('selectOnChangeCallback', response);
  175. };
  176.  
  177. render() {
  178. const { type } = this.state;
  179. const selectData = this.mapListGroupToSelect();
  180. return (
  181. <Fragment>
  182. <Formik
  183. initialValues={{
  184. username: '',
  185. email: '',
  186. password: '',
  187. confirmPassword: ''
  188. }}
  189. // validationSchema={createUserValidationSchema}
  190. onSubmit={values => {
  191. // same shape as initial values
  192. console.log(values);
  193. }}
  194. >
  195. {({ errors, touched }) => (
  196. <Form>
  197. <div className="my-3">
  198. <label>
  199. Username <span className="text-danger">*</span>
  200. </label>
  201. <Field name="username" type="text" className="form-control rounded-0" />
  202. {errors.username && touched.username ? (
  203. <div className="text-danger">{errors.username}</div>
  204. ) : null}
  205. </div>
  206. <div className="my-3">
  207. <label>email</label>
  208. <Field name="email" type="email" className="form-control rounded-0" />
  209. {errors.email && touched.email ? (
  210. <div className="text-danger">{errors.email}</div>
  211. ) : null}
  212. </div>
  213. <div className="my-3">
  214. <label>
  215. Password <span className="text-danger">*</span>
  216. </label>
  217. <div className="d-flex align-items-center">
  218. <Field type={type} name="password" className="form-control rounded-0 mr-2" />
  219. <span
  220. className={type === 'password' ? 'fa fa-eye fa-lg' : 'fa fa-eye-slash fa-lg'}
  221. onClick={this.togglePasswordMask}
  222. />
  223. </div>
  224. {errors.password && touched.password ? (
  225. <div className="text-danger">{errors.password}</div>
  226. ) : null}
  227. </div>
  228. <div className="my-3">
  229. <label>
  230. Confirm Password <span className="text-danger">*</span>
  231. </label>
  232. <Field name="confirmPassword" type={type} className="form-control rounded-0" />
  233. {errors.confirmPassword && touched.confirmPassword ? (
  234. <div className="text-danger">{errors.confirmPassword}</div>
  235. ) : null}
  236. </div>
  237. <div className="my-3">
  238. <label>
  239. Select Group <span className="text-danger">*</span>
  240. </label>
  241. <ReactSelectComponent
  242. isMulti={false}
  243. options={selectData}
  244. onChangeCallback={this.selectOnChangeCallback}
  245. />
  246. </div>
  247. <button type="submit" className="btn btn-primary rounded-0 float-right my-5">
  248. <span className="mx-2">Create User</span>
  249. </button>
  250. </Form>
  251. )}
  252. </Formik>
  253. </Fragment>
  254. );
  255. }
  256. }
  257.  
  258. export default AddEditUserHOCForm(UserCreateForm);
Add Comment
Please, Sign In to add comment