Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import PropTypes from 'prop-types';
  2. import { compose, mapProps } from 'recompose';
  3. import BaseEnquiryForm from 'collections/EnquiryBase';
  4. import {
  5.   withFlatModel,
  6.   addModelRule,
  7. } from 'components/Utilities/Model';
  8. import {
  9.   isRequiredWithMessage,
  10.   hasCorrectName,
  11.   hasCorrectContactPhone,
  12.   hasCorrectContactEmail,
  13.   hasCorrectPassengers,
  14.   hasSubFieldValidationRule,
  15.   isNumber,
  16.   isLengthExact,
  17. } from 'components/Utilities/BasicValidationRules';
  18. import withRemoteSubmit from 'components/Utilities/RemoteSubmit';
  19. import gimpMapping from 'components/Utilities/Mapping/Gimp';
  20. import subscribeMapping from 'components/Utilities/Mapping/Subscribe';
  21. import { withEventListener } from 'components/Utilities/EventProp';
  22.  
  23. let today = new Date();
  24. let initialReturn = new Date();
  25. initialReturn.setDate(today.getDate() + 2);
  26.  
  27. /**
  28.  * Checks the date supplied.
  29.  *
  30.  * This function checks the date then normalizes its value wherever
  31.  * possible.
  32.  * @param {Date|number|string} date The date to check.
  33.  * @param {Date} fallback The fallback date, if the supplied date is not valid.
  34.  *
  35.  * @return {Date} This will return the date object.
  36.  */
  37. function checkDate (date, fallback) {
  38.   if (date) {
  39.     if (date instanceof Date) {
  40.       return date;
  41.     } else if (typeof date === 'number') {
  42.       return new Date(date);
  43.     }
  44.   } else {
  45.     return fallback;
  46.   }
  47. }
  48.  
  49. // Enquiry Schema.
  50. const enquiryModel = (props) => {
  51.   // Simple expoint check.
  52.   const expoint = props.expoint && props.expoint.indexOf(',') === -1 ? props.expoint : '';
  53.   return {
  54.     expoint,
  55.     destination: props.destination ? props.destination : '',
  56.     enquiryType: props.enquiryType ? props.enquiryType : '',
  57.     tripType: 'return',
  58.     departDate: checkDate(props.departDate, today),
  59.     returnDate: checkDate(props.returnDate, initialReturn),
  60.     questions: props.freeText,
  61.     passengers: {
  62.       adults: 1,
  63.       children: 0,
  64.       infants: 0,
  65.     },
  66.     name: {
  67.       title: '',
  68.       firstName: '',
  69.       lastName: '',
  70.     },
  71.     method: 'phone',
  72.     contact: {
  73.       phone: '',
  74.       email: '',
  75.       postcode: '',
  76.     },
  77.     subscribe: false,
  78.     interestFree: false,
  79.   };
  80. };
  81.  
  82. // MAPPING.
  83. // This is a mapping from the model (above) to the required endpoint schema.
  84. // Using gimp mapping here as that is our target.
  85. const mapDataToSubmit = gimpMapping;
  86. const mapDataToSubscribe = subscribeMapping;
  87.  
  88. // VALIDATION.
  89. // Model transformations. (VALIDATION).
  90. // If you turn off the passive updates, you need to enable clear errors to avoid
  91. // errors being extended by other elements updating.
  92. // clearErrors(),
  93. const modelTransforms = compose(
  94.   addModelRule('expoint', isRequiredWithMessage('Please provide a location'), true),
  95.   addModelRule('destination', isRequiredWithMessage('Please provide a location'), true),
  96.   addModelRule('name', hasCorrectName, true),
  97.   addModelRule('passengers', hasCorrectPassengers, true),
  98.   addModelRule('contact', [
  99.     hasSubFieldValidationRule('postcode', [
  100.       isRequiredWithMessage('Invalid Postcode'),
  101.       isNumber,
  102.       isLengthExact(4, 'Invalid Postcode'),
  103.     ]),
  104.     hasCorrectContactPhone,
  105.     hasCorrectContactEmail,
  106.   ], true)
  107. );
  108.  
  109. // Compose General Enquiry with Validation.
  110. export const GeneralEnquiryForm = compose(
  111.   mapProps(props => Object.assign({}, props, props.conf, { mapDataToSubmit, mapDataToSubscribe })),
  112.   withRemoteSubmit(props => props.endpoint ? props.endpoint : '//data.fclmedia.com/sendEnquiry', 'GET', 'submitEnquiry'),
  113.   withRemoteSubmit(props => props.subscribeEndpoint ? props.subscribeEndpoint : '/api/external_endpoint/salesforce_subscribe', 'GET', 'subscribe'),
  114.   withFlatModel(enquiryModel, modelTransforms),
  115.   withEventListener('react-form-update.form.enquiry.general', (data, props) => {
  116.     Object.keys(data).map(key => {
  117.       let newValue = data[key];
  118.       // We cannot directly map return date or depart date because it
  119.       // may come through in an alternate format.
  120.       if (key === 'returnDate' || key === 'departDate') {
  121.         newValue = checkDate(newValue, today);
  122.       }
  123.       props.updateModelField(key, newValue);
  124.     });
  125.   })
  126. )(BaseEnquiryForm);
  127.  
  128. // These are placed here to stop the Base enquiry form from
  129. // having an opinionation on backend (ie gimp or not).
  130. GeneralEnquiryForm.defaultProps = {
  131.   expoint: '',
  132.   destination: '',
  133.   sku: '',
  134.   productName: '',
  135.   brand: 'FC',
  136.   country: 'AU',
  137.   enquiryCategory: 'General Enquiry',
  138.   validationTemplate: 'aufc/drupal-validate-sf.ftl',
  139.   consultantEmailTemplate: 'au/fc/static-forms/ct-general-enquiry.ftl',
  140.   bounceBackEmailTemplate: 'au/fc/static-forms/bb-general-enquiry.ftl',
  141.   failUrl: '/fail',
  142.   forwardUrl: 'http://www.flightcentre.com.au/contact-us/enquiry-confirmation?type=gimp',
  143.   submitHeading: 'Thankyou for your enquiry.',
  144.   submitBody: 'We will get back to you shortly.',
  145.   endpoint: '//data.fclmedia.com/sendEnquiry',
  146.   subscribeEndpoint: '/api/external_endpoint/salesforce_subscribe',
  147. };
  148.  
  149. GeneralEnquiryForm.propTypes = {
  150.   expoint: PropTypes.string,
  151.   destination: PropTypes.string,
  152.   sku: PropTypes.string,
  153.   productName: PropTypes.string,
  154.   brand: PropTypes.string,
  155.   country: PropTypes.string,
  156.   enquiryCategory: PropTypes.string,
  157.   validationTemplate: PropTypes.string,
  158.   consultantEmailTemplate: PropTypes.string,
  159.   bounceBackEmailTemplate: PropTypes.string,
  160.   failUrl: PropTypes.string,
  161.   forwardUrl: PropTypes.string,
  162.   submitHeading: PropTypes.string,
  163.   submitBody: PropTypes.string,
  164.   endpoint: PropTypes.string,
  165.   subscribeEndpoint: PropTypes.string,
  166.   departDate: PropTypes.oneOfType([
  167.     PropTypes.string,
  168.     PropTypes.number,
  169.     PropTypes.instanceOf(Date),
  170.   ]),
  171.   returnDate: PropTypes.oneOfType([
  172.     PropTypes.string,
  173.     PropTypes.number,
  174.     PropTypes.instanceOf(Date),
  175.   ]),
  176. };
  177.  
  178. export default GeneralEnquiryForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement