Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import PropTypes from 'prop-types';
- import { compose, mapProps } from 'recompose';
- import BaseEnquiryForm from 'collections/EnquiryBase';
- import {
- withFlatModel,
- addModelRule,
- } from 'components/Utilities/Model';
- import {
- isRequiredWithMessage,
- hasCorrectName,
- hasCorrectContactPhone,
- hasCorrectContactEmail,
- hasCorrectPassengers,
- hasSubFieldValidationRule,
- isNumber,
- isLengthExact,
- } from 'components/Utilities/BasicValidationRules';
- import withRemoteSubmit from 'components/Utilities/RemoteSubmit';
- import gimpMapping from 'components/Utilities/Mapping/Gimp';
- import subscribeMapping from 'components/Utilities/Mapping/Subscribe';
- import { withEventListener } from 'components/Utilities/EventProp';
- let today = new Date();
- let initialReturn = new Date();
- initialReturn.setDate(today.getDate() + 2);
- /**
- * Checks the date supplied.
- *
- * This function checks the date then normalizes its value wherever
- * possible.
- * @param {Date|number|string} date The date to check.
- * @param {Date} fallback The fallback date, if the supplied date is not valid.
- *
- * @return {Date} This will return the date object.
- */
- function checkDate (date, fallback) {
- if (date) {
- if (date instanceof Date) {
- return date;
- } else if (typeof date === 'number') {
- return new Date(date);
- }
- } else {
- return fallback;
- }
- }
- // Enquiry Schema.
- const enquiryModel = (props) => {
- // Simple expoint check.
- const expoint = props.expoint && props.expoint.indexOf(',') === -1 ? props.expoint : '';
- return {
- expoint,
- destination: props.destination ? props.destination : '',
- enquiryType: props.enquiryType ? props.enquiryType : '',
- tripType: 'return',
- departDate: checkDate(props.departDate, today),
- returnDate: checkDate(props.returnDate, initialReturn),
- questions: props.freeText,
- passengers: {
- adults: 1,
- children: 0,
- infants: 0,
- },
- name: {
- title: '',
- firstName: '',
- lastName: '',
- },
- method: 'phone',
- contact: {
- phone: '',
- email: '',
- postcode: '',
- },
- subscribe: false,
- interestFree: false,
- };
- };
- // MAPPING.
- // This is a mapping from the model (above) to the required endpoint schema.
- // Using gimp mapping here as that is our target.
- const mapDataToSubmit = gimpMapping;
- const mapDataToSubscribe = subscribeMapping;
- // VALIDATION.
- // Model transformations. (VALIDATION).
- // If you turn off the passive updates, you need to enable clear errors to avoid
- // errors being extended by other elements updating.
- // clearErrors(),
- const modelTransforms = compose(
- addModelRule('expoint', isRequiredWithMessage('Please provide a location'), true),
- addModelRule('destination', isRequiredWithMessage('Please provide a location'), true),
- addModelRule('name', hasCorrectName, true),
- addModelRule('passengers', hasCorrectPassengers, true),
- addModelRule('contact', [
- hasSubFieldValidationRule('postcode', [
- isRequiredWithMessage('Invalid Postcode'),
- isNumber,
- isLengthExact(4, 'Invalid Postcode'),
- ]),
- hasCorrectContactPhone,
- hasCorrectContactEmail,
- ], true)
- );
- // Compose General Enquiry with Validation.
- export const GeneralEnquiryForm = compose(
- mapProps(props => Object.assign({}, props, props.conf, { mapDataToSubmit, mapDataToSubscribe })),
- withRemoteSubmit(props => props.endpoint ? props.endpoint : '//data.fclmedia.com/sendEnquiry', 'GET', 'submitEnquiry'),
- withRemoteSubmit(props => props.subscribeEndpoint ? props.subscribeEndpoint : '/api/external_endpoint/salesforce_subscribe', 'GET', 'subscribe'),
- withFlatModel(enquiryModel, modelTransforms),
- withEventListener('react-form-update.form.enquiry.general', (data, props) => {
- Object.keys(data).map(key => {
- let newValue = data[key];
- // We cannot directly map return date or depart date because it
- // may come through in an alternate format.
- if (key === 'returnDate' || key === 'departDate') {
- newValue = checkDate(newValue, today);
- }
- props.updateModelField(key, newValue);
- });
- })
- )(BaseEnquiryForm);
- // These are placed here to stop the Base enquiry form from
- // having an opinionation on backend (ie gimp or not).
- GeneralEnquiryForm.defaultProps = {
- expoint: '',
- destination: '',
- sku: '',
- productName: '',
- brand: 'FC',
- country: 'AU',
- enquiryCategory: 'General Enquiry',
- validationTemplate: 'aufc/drupal-validate-sf.ftl',
- consultantEmailTemplate: 'au/fc/static-forms/ct-general-enquiry.ftl',
- bounceBackEmailTemplate: 'au/fc/static-forms/bb-general-enquiry.ftl',
- failUrl: '/fail',
- forwardUrl: 'http://www.flightcentre.com.au/contact-us/enquiry-confirmation?type=gimp',
- submitHeading: 'Thankyou for your enquiry.',
- submitBody: 'We will get back to you shortly.',
- endpoint: '//data.fclmedia.com/sendEnquiry',
- subscribeEndpoint: '/api/external_endpoint/salesforce_subscribe',
- };
- GeneralEnquiryForm.propTypes = {
- expoint: PropTypes.string,
- destination: PropTypes.string,
- sku: PropTypes.string,
- productName: PropTypes.string,
- brand: PropTypes.string,
- country: PropTypes.string,
- enquiryCategory: PropTypes.string,
- validationTemplate: PropTypes.string,
- consultantEmailTemplate: PropTypes.string,
- bounceBackEmailTemplate: PropTypes.string,
- failUrl: PropTypes.string,
- forwardUrl: PropTypes.string,
- submitHeading: PropTypes.string,
- submitBody: PropTypes.string,
- endpoint: PropTypes.string,
- subscribeEndpoint: PropTypes.string,
- departDate: PropTypes.oneOfType([
- PropTypes.string,
- PropTypes.number,
- PropTypes.instanceOf(Date),
- ]),
- returnDate: PropTypes.oneOfType([
- PropTypes.string,
- PropTypes.number,
- PropTypes.instanceOf(Date),
- ]),
- };
- export default GeneralEnquiryForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement