Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { PureComponent, ChangeEvent } from 'react';
  2. import { connect, MapDispatchToProps } from 'react-redux';
  3. import isEmpty from 'lodash/isEmpty';
  4. import InfoBox from 'components/Audience/InfoBox';
  5. import { updateCustomerMetadata as updateCustomerMetadataAction } from 'actions';
  6. import ConfirmModal from 'components/Modals/ConfirmModal';
  7. import { reduce, map } from 'lodash';
  8. import { CustomerDetails } from 'reducers/types/AudienceState';
  9.  
  10. interface DispatchProps {
  11.   updateCustomerMetadata: (id: number, metadata: any, fullName?: string) => any;
  12. }
  13.  
  14. interface OwnProps {
  15.   id: number;
  16.   customerDetails: CustomerDetails;
  17. }
  18.  
  19. type Props = DispatchProps & OwnProps;
  20.  
  21. interface State {
  22.   edit: boolean;
  23.   inputValues: {
  24.     [key: string]: string;
  25.   };
  26. }
  27.  
  28. class CustomerInfoBox extends PureComponent<Props, State> {
  29.   resetMetadataModal: ConfirmModal | null = null;
  30.  
  31.   constructor(props: Props) {
  32.     super(props);
  33.  
  34.     this.state = {
  35.       edit: false,
  36.       inputValues: {},
  37.     };
  38.   }
  39.  
  40.   componentDidUpdate(prevProps: Props) {
  41.     const { customerDetails } = this.props;
  42.     if (prevProps.customerDetails.isFetching && !customerDetails.isFetching) {
  43.       const inputValues = reduce(
  44.         customerDetails,
  45.         (result, value, key) => ({ ...result, [(key as any).toString()]: value }),
  46.         {}
  47.       );
  48.  
  49.       console.error(inputValues);
  50.  
  51.       this.setState({
  52.         inputValues,
  53.       });
  54.     }
  55.   }
  56.  
  57.   handleIconClick = () => {
  58.     this.setState({ edit: true });
  59.   };
  60.  
  61.   handleCancel = () => {
  62.     this.setState({ edit: false });
  63.   };
  64.  
  65.   handleCheck = () => {
  66.     const { updateCustomerMetadata, id } = this.props;
  67.     updateCustomerMetadata(id, this.getInputValues());
  68.     this.setState({
  69.       edit: false,
  70.     });
  71.   };
  72.  
  73.   handleReset = () => {
  74.     const { updateCustomerMetadata, id } = this.props;
  75.     updateCustomerMetadata(id, this.getSpanValues());
  76.     this.hideModal();
  77.   };
  78.  
  79.   getInputValues = () => {
  80.     const { inputValues } = this.state;
  81.     return inputValues;
  82.   };
  83.  
  84.   getSpanValues = () => {
  85.     const { customerDetails } = this.props;
  86.     return reduce(
  87.       customerDetails,
  88.       (result, _, key) => ({
  89.         ...result,
  90.         [(key as any).toString()]: '',
  91.       }),
  92.       {}
  93.     );
  94.   };
  95.  
  96.   showModal = () => {
  97.     if (this.resetMetadataModal) {
  98.       this.resetMetadataModal.show();
  99.     }
  100.   };
  101.  
  102.   hideModal = () => {
  103.     if (this.resetMetadataModal) {
  104.       this.resetMetadataModal.hide();
  105.     }
  106.   };
  107.  
  108.   onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
  109.     this.setState(({ inputValues }) => ({
  110.       inputValues: {
  111.         ...inputValues,
  112.         [e.currentTarget.name]: e.currentTarget.value,
  113.       },
  114.     }));
  115.   };
  116.  
  117.   renderInput = (value: string, key: string) => {
  118.     const { edit } = this.state;
  119.     return (
  120.       <div className="ovp-dialog info" key={key}>
  121.         <span>{key.toString()}:</span>
  122.         {edit ? (
  123.           <input onChange={this.onInputChange} type="text" name={key} value={value.toString()} />
  124.         ) : (
  125.           <span>{value.toString()}</span>
  126.         )}
  127.       </div>
  128.     );
  129.   };
  130.  
  131.   renderInputs = (inputValues: any) => {
  132.     console.error('RERENDERING');
  133.     return map(inputValues, this.renderInput);
  134.   };
  135.  
  136.   render() {
  137.     const { customerDetails } = this.props;
  138.     const { edit, inputValues } = this.state;
  139.  
  140.     return (
  141.       <div className="customer-box">
  142.         <div className="info-box">
  143.           {isEmpty(customerDetails) ? (
  144.             [
  145.               <h5 key="1">Info collected</h5>,
  146.               <span className="empty_msg" key="2">
  147.                 There are no collected information for this user.
  148.               </span>,
  149.             ]
  150.           ) : (
  151.             <InfoBox
  152.               key="1"
  153.               title="Info collected"
  154.               subtitle="Clear metadata"
  155.               renderContent={() => this.renderInputs(inputValues)}
  156.               handleIconClick={this.handleIconClick}
  157.               handleCheck={this.handleCheck}
  158.               handleCancel={this.handleCancel}
  159.               edit={edit}
  160.               showModal={this.showModal}
  161.             />
  162.           )}
  163.         </div>
  164.         <ConfirmModal
  165.           key="2"
  166.           ref={modal => {
  167.             this.resetMetadataModal = modal;
  168.           }}
  169.           item="metadata"
  170.           handleDelete={this.handleReset}
  171.           noSubheader
  172.         />
  173.       </div>
  174.     );
  175.   }
  176. }
  177.  
  178. export const PlainCustomerInfoBox = CustomerInfoBox;
  179.  
  180. const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
  181.   updateCustomerMetadata: updateCustomerMetadataAction,
  182. };
  183.  
  184. export default connect(
  185.   null,
  186.   mapDispatchToProps
  187. )(CustomerInfoBox);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement