'use strict'; import React from 'react'; import classnames from 'classnames'; import Button from 'react-bootstrap/lib/Button'; import Form from './form'; import Property from './property'; import InputTextAreaToggable from './input_textarea_toggable'; import InputDate from './input_date'; import InputCheckbox from './input_checkbox'; import ContractAgreementListStore from '../../stores/contract_agreement_list_store'; import ContractAgreementListActions from '../../actions/contract_agreement_list_actions'; import AscribeSpinner from '../ascribe_spinner'; import { mergeOptions } from '../../utils/general_utils'; import { getLangText } from '../../utils/lang_utils'; import AclInformation from '../ascribe_buttons/acl_information'; let LoanForm = React.createClass({ propTypes: { loanHeading: React.PropTypes.string, email: React.PropTypes.string, gallery: React.PropTypes.string, startdate: React.PropTypes.object, enddate: React.PropTypes.object, showPersonalMessage: React.PropTypes.bool, showEndDate: React.PropTypes.bool, showStartDate: React.PropTypes.bool, showPassword: React.PropTypes.bool, url: React.PropTypes.string, id: React.PropTypes.object, message: React.PropTypes.string, createPublicContractAgreement: React.PropTypes.bool, handleSuccess: React.PropTypes.func }, getDefaultProps() { return { loanHeading: '', showPersonalMessage: true, showEndDate: true, showStartDate: true, showPassword: true, createPublicContractAgreement: true }; }, getInitialState() { return ContractAgreementListStore.getState(); }, componentDidMount() { ContractAgreementListStore.listen(this.onChange); this.getContractAgreementsOrCreatePublic(this.props.email); }, /** * This method needs to be in form_loan as some whitelabel pages (Cyland) load * the loanee's email async! * * SO LEAVE IT IN! */ componentWillReceiveProps(nextProps) { if(nextProps && nextProps.email && this.props.email !== nextProps.email) { this.getContractAgreementsOrCreatePublic(nextProps.email); } }, componentWillUnmount() { ContractAgreementListStore.unlisten(this.onChange); }, onChange(state) { this.setState(state); }, getContractAgreementsOrCreatePublic(email){ ContractAgreementListActions.flushContractAgreementList.defer(); if (email) { // fetch the available contractagreements (pending/accepted) ContractAgreementListActions.fetchAvailableContractAgreementList(email, true); } }, getFormData(){ return mergeOptions( this.props.id, this.getContractAgreementId() ); }, handleOnChange(event) { // event.target.value is the submitted email of the loanee if(event && event.target && event.target.value && event.target.value.match(/.*@.*\..*/)) { this.getContractAgreementsOrCreatePublic(event.target.value); } else { ContractAgreementListActions.flushContractAgreementList(); } }, getContractAgreementId() { if (this.state.contractAgreementList && this.state.contractAgreementList.length > 0) { return {'contract_agreement_id': this.state.contractAgreementList[0].id}; } return {}; }, getContractCheckbox() { if(this.state.contractAgreementList && this.state.contractAgreementList.length > 0) { // we need to define a key on the InputCheckboxes as otherwise // react is not rerendering them on a store switch and is keeping // the default value of the component (which is in that case true) let contractAgreement = this.state.contractAgreementList[0]; let contract = contractAgreement.contract; if(contractAgreement.datetime_accepted) { return ( ); } else { return ( {getLangText('I agree to the')}  {getLangText('terms of ')} {contract.issuer} ); } } else { return ( ); } }, getAppendix() { if(this.state.contractAgreementList && this.state.contractAgreementList.length > 0) { let appendix = this.state.contractAgreementList[0].appendix; if (appendix && appendix.default) { return (
{appendix.default}
); } } return null; }, getButtons() { if(this.props.loanHeading) { return ( ); } else { return (

); } }, render() { return (

}>

{this.props.loanHeading}

{this.getContractCheckbox()} {this.getAppendix()} {this.props.children} ); } }); export default LoanForm;