ratchapong

Untitled

Nov 27th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Node modules
  3.  */
  4. import * as yup from "yup";
  5. import PropTypes from "prop-types";
  6. import React from "react";
  7. import { connect } from "react-redux";
  8. import { FormattedMessage } from "react-intl";
  9. import { withFormik } from "formik";
  10.  
  11. /**
  12.  * Components
  13.  */
  14. import Form from "../../../../../components/AntDesign/Form";
  15. import InjectTranslation from "../../../../../components/InjectTranslation";
  16. import Input from "../../../../../components/AntDesign/Input";
  17. import Message from "../../../../../components/AntDesign/Message";
  18. import Modal from "../../../../../components/AntDesign/Modal";
  19.  
  20.  
  21. /**
  22.  * Redux actions
  23.  */
  24. import { actionCreators as userActionCreators } from "../../../../../redux/user/actions";
  25.  
  26. /**
  27.  * Redux selectors
  28.  */
  29. import {
  30.   usersSelector,
  31.   isUpdatingUserByIdSelector,
  32.   updateUserByIdRequestSelector,
  33. } from "../../../../../redux/user/selectors";
  34. import Button from "../../../../../components/AntDesign/Button";
  35.  
  36. class UpdateUserForm extends React.PureComponent {
  37.   state = {
  38.     isModalOpened: false,
  39.   };
  40.  
  41.   static propTypes = {
  42.     dispatchGetUsers: PropTypes.func.isRequired,
  43.     handleBlur: PropTypes.func.isRequired,
  44.     handleChange: PropTypes.func.isRequired,
  45.     handleSubmit: PropTypes.func.isRequired,
  46.     isSubmitting: PropTypes.bool.isRequired,
  47.     isUpdatingUser: PropTypes.bool.isRequired,
  48.     isValid: PropTypes.bool.isRequired,
  49.     resetForm: PropTypes.func.isRequired,
  50.     setSubmitting: PropTypes.func.isRequired,
  51.     submitForm: PropTypes.func.isRequired,
  52.     translate: PropTypes.shape({
  53.       defaultFormats: PropTypes.object,
  54.       defaultLocale: PropTypes.string,
  55.       formatDate: PropTypes.func,
  56.       formatHTMLMessage: PropTypes.func,
  57.       formatMessage: PropTypes.func,
  58.       formatNumber: PropTypes.func,
  59.       formatPlural: PropTypes.func,
  60.       formatRelative: PropTypes.func,
  61.       formatTime: PropTypes.func,
  62.       formats: PropTypes.object,
  63.       formatters: PropTypes.object,
  64.       locale: PropTypes.string,
  65.       messages: PropTypes.object,
  66.       now: PropTypes.func,
  67.       textComponent: PropTypes.string,
  68.     }).isRequired,
  69.     updateUserRequest: PropTypes.shape({ draw: PropTypes.number, response: PropTypes.string }).isRequired,
  70.     usersHeaders: PropTypes.shape({
  71.       limit: PropTypes.string,
  72.       page: PropTypes.string,
  73.       sort: PropTypes.string,
  74.       total: PropTypes.string,
  75.     }).isRequired,
  76.     values: PropTypes.shape({
  77.       objectId: PropTypes.string,
  78.       username: PropTypes.string,
  79.     }).isRequired,
  80.   };
  81.  
  82.   componentDidUpdate (previousProps) {
  83.     const {
  84.       dispatchGetUsers,
  85.       updateUserRequest,
  86.       resetForm,
  87.       setSubmitting,
  88.       translate,
  89.       usersHeaders,
  90.     } = this.props;
  91.     if (updateUserRequest.draw > previousProps.updateUserRequest.draw) {
  92.       setSubmitting(false);
  93.       resetForm();
  94.       this.closeModal();
  95.       Message.success(translate.formatMessage({ id: updateUserRequest.response }));
  96.       dispatchGetUsers({
  97.         ...usersHeaders,
  98.       });
  99.     }
  100.     if (updateUserRequest.draw < previousProps.updateUserRequest.draw) {
  101.       setSubmitting(false);
  102.       Message.error(translate.formatMessage({ id: updateUserRequest.response }));
  103.     }
  104.   }
  105.  
  106.   closeModal = () => {
  107.     console.log("This is called successfully!")
  108.     this.setState({
  109.       isModalOpened: false,
  110.     });
  111.   };
  112.  
  113.   handleOpenModal = () => {
  114.     this.setState({
  115.       isModalOpened: true,
  116.     });
  117.   };
  118.  
  119.   handleOk = () => {
  120.     const { submitForm } = this.props;
  121.     submitForm();
  122.   };
  123.  
  124.   handleCancel = () => {
  125.     this.closeModal();
  126.   };
  127.  
  128.   render () {
  129.     const {
  130.       handleBlur,
  131.       handleChange,
  132.       handleSubmit,
  133.       isUpdatingUser,
  134.       isSubmitting,
  135.       isValid,
  136.       values,
  137.     } = this.props;
  138.     const {
  139.       isModalOpened,
  140.     } = this.state;
  141.     console.log('isModalOpened', isModalOpened)
  142.     const isSubmittingForm = isSubmitting || isUpdatingUser;
  143.     return (
  144.       <Button
  145.         htmlType="button"
  146.         onClick={this.handleOpenModal}
  147.       >
  148.         <FormattedMessage id="a" />
  149.         <Modal
  150.           okButtonProps={
  151.             {
  152.               disabled: !isValid,
  153.               loading: isSubmittingForm,
  154.             }
  155.           }
  156.           okText={<FormattedMessage id="form.update.submit.label.user" />}
  157.           onCancel={this.handleCancel}
  158.           onOk={this.handleOk}
  159.           title={<FormattedMessage id="form.update.title.user" />}
  160.           visible={isModalOpened}
  161.         >
  162.           <Form onSubmit={handleSubmit}>
  163.             <Form.Item
  164.               label={<FormattedMessage id="form.update.field.label.user.username" />}
  165.             >
  166.               <Input
  167.                 autoComplete="off"
  168.                 name="username"
  169.                 onBlur={handleBlur}
  170.                 onChange={handleChange}
  171.                 value={values.username}
  172.               />
  173.             </Form.Item>
  174.           </Form>
  175.         </Modal>
  176.       </Button>
  177.     );
  178.   }
  179. }
  180.  
  181. const mapStateToProps = (state, props) => ({
  182.   isUpdatingUser: isUpdatingUserByIdSelector(state)(props.objectId),
  183.   updateUserRequest: updateUserByIdRequestSelector(state)(props.objectId),
  184.   usersHeaders: usersSelector(state).headers,
  185. });
  186.  
  187. const mapDispatchToProps = {
  188.   dispatchGetUsers: userActionCreators.getUsers,
  189.   dispatchUpdateUserById: userActionCreators.updateUserById,
  190. };
  191.  
  192. export default connect(mapStateToProps, mapDispatchToProps)(
  193.   InjectTranslation(
  194.     withFormik(
  195.       {
  196.         handleSubmit: (values, { props }) => {
  197.           const { dispatchUpdateUserById } = props;
  198.           const {
  199.             username,
  200.             objectId,
  201.           } = values;
  202.           dispatchUpdateUserById({
  203.             objectId,
  204.             username,
  205.           });
  206.         },
  207.         mapPropsToValues: props => ({
  208.           objectId: props.objectId,
  209.           username: props.username,
  210.         }),
  211.         validationSchema: yup.object().shape({
  212.           objectId: yup.string().required(),
  213.           username: yup.string().required(),
  214.         }),
  215.       },
  216.     )(UpdateUserForm),
  217.   ),
  218. );
Advertisement
Add Comment
Please, Sign In to add comment