SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3.  
  4. import {isEmpty} from "lodash";
  5. import {containsSubstringCaseInsensitive, IsoTimeToUnixTime} from "../utils/common";
  6.  
  7. export const withEnquiries = () => Component => {
  8.   return class WithEnquiries extends React.Component {
  9.     static propTypes = {
  10.       // connect
  11.       getEnquiries: PropTypes.func.isRequired,
  12.       getEnquiryComments: PropTypes.func.isRequired,
  13.       setOpenedCommentId: PropTypes.func.isRequired,
  14.       enquiries: PropTypes.shape({
  15.         loadingEnquiries: PropTypes.bool.isRequired,
  16.         loadingComments: PropTypes.bool.isRequired,
  17.         list: PropTypes.array,
  18.         users: PropTypes.array,
  19.         comments: PropTypes.array,
  20.         openedCommentId: PropTypes.number,
  21.         enquiriesError: PropTypes.bool.isRequired,
  22.         commentsError: PropTypes.bool.isRequired
  23.       }).isRequired,
  24.       user: PropTypes.object.isRequired,
  25.       // From PopupHolder.js (MainSection)
  26.       status: PropTypes.string,
  27.       unqualifiedStatus: PropTypes.string,
  28.       searchQuery: PropTypes.string
  29.     };
  30.  
  31.     // Get enquiries on load if there are no enquiries in store yet
  32.     componentDidMount() {
  33.       if (isEmpty(this.props.enquiries.list)) {
  34.         this.props.getEnquiries();
  35.       }
  36.     }
  37.  
  38.     // Get comments from clicked enquiry in dashboard
  39.     handleGetEnquiryComments = (e, id) => {
  40.       this.props.setOpenedCommentId(id);
  41.       // Check if comments of this enquiry are already in store
  42.       if (!this.props.enquiries.comments.some(comment => comment.id === id)) {
  43.         this.props.getEnquiryComments(id);
  44.       }
  45.     };
  46.     // Parse enquiries object from store
  47.     getListEnquiries = () => {
  48.       if (this.props.enquiries.loading)
  49.         return null;
  50.  
  51.       const {enquiries, status, unqualifiedStatus, searchQuery} = this.props;
  52.  
  53.       let lst = enquiries.list;
  54.  
  55.       lst = lst.sort((a, b) => IsoTimeToUnixTime(a["updated_at"]) - IsoTimeToUnixTime(b["updated_at"]));
  56.  
  57.       if (isEmpty(lst))
  58.         return (
  59.           <tr>
  60.             <td>No enquiries</td>
  61.           </tr>
  62.         );
  63.  
  64.       // Filter enquiries by status
  65.       const listEnquiries = lst.reduceRight((list, enquiry) => {
  66.         const enquiryUser = this.getEnquiryUser(enquiry.requester_id);
  67.         if (!enquiryUser)
  68.           return list;
  69.  
  70.         // Show only enquiries related to current user (agent)
  71.         // if (user.email === enquiry.fields[5].value) {
  72.  
  73.         switch (status) {
  74.           case "pending":
  75.             if (enquiry.status !== "pending" && enquiry.status !== "open") {
  76.               return list;
  77.             }
  78.             break;
  79.  
  80.           case "qualified":
  81.             // If there's no requsted tag in 'tags', then skip enquiry,
  82.             // add it to 'list' otherwise
  83.             if (!enquiry.tags.some(tag => tag === "leadstatus_qualified__qualified")) {
  84.               return list;
  85.             }
  86.             break;
  87.  
  88.           case "unqualified":
  89.             if (!enquiry.tags.some(tag => tag.indexOf("unqualified") !== -1)) {
  90.               return list;
  91.             } else {
  92.               // Check for which exactly unqualified status filter enquiries
  93.               switch (unqualifiedStatus) {
  94.                 case "notinterested":
  95.                   if (!enquiry.tags.some(tag => tag === "leadstatus_unqualified__not_interested")) {
  96.                     return list;
  97.                   }
  98.                   break;
  99.  
  100.                 case "unresponsive":
  101.                   if (!enquiry.tags.some(tag => tag === "leadstatus_unqualified__unresponsive")) {
  102.                     return list;
  103.                   }
  104.                   break;
  105.  
  106.                 case "inaccurate":
  107.                   if (!enquiry.tags.some(tag => tag === "leadstatus_unqualified__inaccurate_contact_information")) {
  108.                     return list;
  109.                   }
  110.                   break;
  111.  
  112.                 case "other":
  113.                   if (!enquiry.tags.some(tag => tag === "leadstatus_unqualified__other")) {
  114.                     return list;
  115.                   }
  116.                   break;
  117.  
  118.                 default:
  119.                   break;
  120.               }
  121.             }
  122.             break;
  123.  
  124.           case "referred":
  125.             if (!enquiry.tags.some(tag => tag === "leadstatus_qualified__referred_for_finance")) {
  126.               return list;
  127.             }
  128.             break;
  129.  
  130.           default:
  131.             break;
  132.         }
  133.         // Search for search field query
  134.         if (searchQuery) {
  135.           // In case property address is null
  136.           if (!enquiry.fields[10].value) {
  137.             // Check if name and email don't satisfy the query
  138.             if (!containsSubstringCaseInsensitive(enquiryUser.name, searchQuery)
  139.               && !containsSubstringCaseInsensitive(enquiryUser.email, searchQuery)) {
  140.               return list;
  141.             }
  142.           } else if (!containsSubstringCaseInsensitive(enquiryUser.name, searchQuery)
  143.             && !containsSubstringCaseInsensitive(enquiryUser.email, searchQuery)
  144.             && !containsSubstringCaseInsensitive(enquiry.fields[10].value, searchQuery)) {
  145.             return list;
  146.           }
  147.         }
  148.         // Accumulate enquiries
  149.         list.push(
  150.           <tr
  151.             key={enquiry.id}
  152.             onClick={e => this.handleGetEnquiryComments(e, enquiry.id)}
  153.           >
  154.             <td>{enquiry.id}</td>
  155.             <td>{enquiryUser.name}</td>
  156.             <td>{enquiryUser.email}</td>
  157.             <td>{enquiryUser.phone || "No number provided"}</td>
  158.             <td>{enquiry.fields[10].value || "No address"}</td>
  159.           </tr>
  160.         );
  161.  
  162.         return list;
  163.       }, []);
  164.       if (isEmpty(listEnquiries)) {
  165.         return null;
  166.       }
  167.       return listEnquiries;
  168.     };
  169.     // Get current zendesk user instance for matching with enquiries (tickets)
  170.     getEnquiryUser = id => {
  171.       const user = this.props.enquiries.users.filter(user => user.id === id);
  172.       return user[0];
  173.     };
  174.     // Build enquiries table if not empty
  175.     getTableBody = () => {
  176.       const listEnquiries = this.getListEnquiries();
  177.  
  178.       if (isEmpty(listEnquiries)) return <h4>There are no enquiries</h4>;
  179.       return (
  180.         <table>
  181.           <thead>
  182.           <tr>
  183.             <th>#</th>
  184.             <th>Name</th>
  185.             <th>Email</th>
  186.             <th>Phone</th>
  187.             <th>Property Address</th>
  188.           </tr>
  189.           </thead>
  190.           <tbody>{listEnquiries}</tbody>
  191.         </table>
  192.       );
  193.     };
  194.  
  195.     render() {
  196.       return <Component getTableBody={this.getTableBody} {...this.props} />;
  197.     }
  198.   };
  199. };
  200.  
  201. export default withEnquiries;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top