Advertisement
danine1

Mutations: (basic.jsx)

Mar 21st, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint camelcase:0 */
  2. import React from "react";
  3. import PropTypes from "prop-types";
  4. import { connect } from "react-redux";
  5. import { graphql, createRefetchContainer, commitMutation } from "react-relay";
  6. import { environment } from "@graphql";
  7. import { toggleNotification as toggleNotificationAction } from "@redux/ui/actionsNotification"; /* eslint-disable-line max-len */
  8.  
  9. import BasicFormEdit from "../edit";
  10.  
  11. const mutation = graphql`
  12.   mutation BasicMutation($input: UpdatePersonInput!) {
  13.     updatePerson(input: $input) {
  14.       clientMutationId
  15.     }
  16.   }
  17. `;
  18. @connect(null, {
  19.   toggleNotification: toggleNotificationAction,
  20. })
  21. class BasicFormContainer extends React.PureComponent {
  22.   static propTypes = {
  23.     basicInfo: PropTypes.instanceOf(Object).isRequired,
  24.     relay: PropTypes.instanceOf(Object).isRequired,
  25.     toggleNotification: PropTypes.func.isRequired,
  26.   };
  27.  
  28.   updatePerson = (key, value) => {
  29.     const { basicInfo } = this.props;
  30.     commitMutation(environment, {
  31.       mutation,
  32.       variables: {
  33.         input: { globalId: basicInfo.id, [key]: value },
  34.       },
  35.       onCompleted: () => {
  36.         this.props.relay.refetch();
  37.       },
  38.       onError: (err) => {
  39.         // __TODO Handle error
  40.         this.props.toggleNotification(err, "success", 2000);
  41.       },
  42.     });
  43.   };
  44.  
  45.   flattenData = (basicInfo) => {
  46.     const {
  47.       first_name,
  48.       gender: { value: gender },
  49.       information: { date_of_birth, place_of_birth },
  50.       last_name,
  51.       middle_name,
  52.       maiden_name,
  53.     } = basicInfo;
  54.     return {
  55.       first_name,
  56.       gender: gender || "male",
  57.       date_of_birth,
  58.       place_of_birth,
  59.       last_name,
  60.       middle_name,
  61.       maiden_name,
  62.     };
  63.   };
  64.  
  65.   render() {
  66.     const { basicInfo, ...rest } = this.props;
  67.     const flatBasicInfo = this.flattenData(this.props.basicInfo);
  68.     return (
  69.       <BasicFormEdit
  70.         updatePerson={this.updatePerson}
  71.         basicInfo={flatBasicInfo}
  72.         {...rest}
  73.       />
  74.     );
  75.   }
  76. }
  77.  
  78. export default createRefetchContainer(
  79.   BasicFormContainer,
  80.   graphql`
  81.     fragment BasicInformation_basicInfo on Person {
  82.       id
  83.       first_name
  84.       last_name
  85.       middle_name
  86.       maiden_name
  87.       gender {
  88.         value
  89.       }
  90.       information {
  91.         date_of_birth
  92.         place_of_birth
  93.       }
  94.     }
  95.   `,
  96.   // this.props.relay.refetch() inherits variables from initial root query
  97.   graphql`
  98.     query BasicInformationRefetchQuery($globalId: String!) {
  99.       getEmployee(globalId: $globalId) {
  100.         person {
  101.           ...BasicInformation_basicInfo
  102.         }
  103.       }
  104.     }
  105.   `,
  106. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement