1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-24 02:16:10 +01:00
onion/js/components/ascribe_forms/form_loan.js

153 lines
5.0 KiB
JavaScript
Raw Normal View History

'use strict';
2015-05-29 16:53:30 +02:00
import React from 'react';
import ApiUrls from '../../constants/api_urls';
import FormMixin from '../../mixins/form_mixin';
import InputText from './input_text';
2015-06-01 13:02:53 +02:00
import InputHidden from './input_hidden';
import InputCheckbox from './input_checkbox';
2015-06-01 14:16:06 +02:00
import InputDate from './input_date';
2015-05-29 16:53:30 +02:00
import InputTextArea from './input_textarea';
2015-06-05 11:40:49 +02:00
import OwnershipFetcher from '../../fetchers/ownership_fetcher';
2015-06-05 14:22:02 +02:00
import ButtonSubmitOrClose from '../ascribe_buttons/button_submit_close';
2015-05-29 16:53:30 +02:00
2015-07-03 19:08:56 +02:00
import { getLangText } from '../../utils/lang_utils.js'
2015-06-01 13:02:53 +02:00
let LoanForm = React.createClass({
getInitialState() {
this.setState({
contract_key: null,
contract_url: null,
loaneeHasContract: false
});
},
2015-05-29 16:53:30 +02:00
mixins: [FormMixin],
url() {
return ApiUrls.ownership_loans;
2015-05-29 16:53:30 +02:00
},
2015-05-29 16:53:30 +02:00
getFormData() {
return {
2015-06-02 14:00:05 +02:00
bitcoin_id: this.getBitcoinIds().join(),
2015-06-01 13:02:53 +02:00
loanee: this.refs.loanee.state.value,
gallery_name: this.refs.gallery_name.state.value,
2015-06-02 18:43:37 +02:00
startdate: this.refs.startdate.state.value_formatted,
enddate: this.refs.enddate.state.value_formatted,
2015-06-01 13:02:53 +02:00
loan_message: this.refs.loan_message.state.value,
password: this.refs.password.state.value,
terms: this.refs.terms.state.value
};
2015-05-29 16:53:30 +02:00
},
handleLoanEmailBlur(){
2015-06-01 13:02:53 +02:00
OwnershipFetcher.fetchLoanContract(this.refs.loanee.state.value)
.then((res) => {
if (res && res.length > 0) {
this.setState({
contract_key: res[0].s3Key,
contract_url: res[0].s3Url,
loaneeHasContract: true
});
2015-06-01 13:02:53 +02:00
}
else {
2015-06-01 13:02:53 +02:00
this.resetLoanContract();
}
})
.catch((err) => {
console.log(err);
this.resetLoanContract();
});
},
2015-06-01 13:02:53 +02:00
resetLoanContract(){
this.setState({
contract_key: null,
contract_url: null,
loaneeHasContract: false
});
2015-06-01 13:02:53 +02:00
},
2015-05-29 16:53:30 +02:00
renderForm() {
let title = this.getTitlesString().join('');
2015-05-29 16:53:30 +02:00
let username = this.props.currentUser.username;
let message =
2015-07-03 19:08:56 +02:00
`${getLangText('Hi')},
2015-05-29 16:53:30 +02:00
2015-07-03 19:08:56 +02:00
${getLangText('I loan')} :
${title}${getLangText('to you')}.
2015-05-29 16:53:30 +02:00
2015-07-03 19:08:56 +02:00
${getLangText('Truly yours')},
2015-05-29 16:53:30 +02:00
${username}`;
2015-06-02 14:00:05 +02:00
2015-06-01 13:02:53 +02:00
let contract = <InputHidden ref="terms" value="True"/>;
if (this.state.loaneeHasContract){
let label = (<div>
2015-07-03 19:08:56 +02:00
{getLangText('I agree to the')}&nbsp;
2015-06-01 13:02:53 +02:00
<a href={this.state.contract_url} target="_blank">
2015-07-03 19:08:56 +02:00
{getLangText('terms of')} {this.refs.loanee.state.value}
2015-06-01 13:02:53 +02:00
</a>
</div>);
contract = (<InputCheckbox
2015-06-01 13:02:53 +02:00
ref="terms"
required="required"
label={label}
/>);
2015-06-01 13:02:53 +02:00
}
2015-05-29 16:53:30 +02:00
return (
2015-06-01 13:02:53 +02:00
<form id="loan_modal_content" role="form" onSubmit={this.submit}>
<input className="invisible" type="email" name="fake_loanee"/>
2015-05-29 16:53:30 +02:00
<input className="invisible" type="password" name="fake_password"/>
<InputText
2015-06-01 13:02:53 +02:00
ref="loanee"
2015-07-03 19:08:56 +02:00
placeHolder={getLangText('Loanee email')}
2015-05-29 16:53:30 +02:00
required="required"
type="email"
2015-06-01 13:02:53 +02:00
submitted={this.state.submitted}
onBlur={this.handleLoanEmailBlur}/>
<InputText
ref="gallery_name"
2015-07-03 19:08:56 +02:00
placeHolder={getLangText('Gallery/exhibition (optional)')}
2015-06-01 13:02:53 +02:00
required=""
type="text"
2015-05-29 16:53:30 +02:00
submitted={this.state.submitted}/>
2015-06-01 13:02:53 +02:00
<div className="row">
2015-06-02 18:43:37 +02:00
<div className="col-xs-6">
2015-06-01 14:49:13 +02:00
<InputDate
ref="startdate"
2015-07-03 19:08:56 +02:00
placeholderText={getLangText('Loan start date')} />
2015-06-01 13:02:53 +02:00
</div>
2015-06-02 18:43:37 +02:00
<div className="col-xs-6 form-group">
2015-06-02 15:38:47 +02:00
<InputDate
2015-06-01 13:02:53 +02:00
ref="enddate"
2015-07-03 19:08:56 +02:00
placeholderText={getLangText('Loan end date')} />
2015-06-01 13:02:53 +02:00
</div>
</div>
2015-05-29 16:53:30 +02:00
<InputTextArea
2015-06-01 13:02:53 +02:00
ref="loan_message"
2015-05-29 16:53:30 +02:00
defaultValue={message}
required=""
/>
<InputText
ref="password"
2015-07-03 19:08:56 +02:00
placeHolder={getLangText('Password')}
2015-05-29 16:53:30 +02:00
required="required"
type="password"
submitted={this.state.submitted}/>
2015-06-01 13:02:53 +02:00
{contract}
<ButtonSubmitOrClose
2015-07-03 19:08:56 +02:00
text={getLangText('LOAN')}
2015-05-29 16:53:30 +02:00
onClose={this.props.onRequestHide}
submitted={this.state.submitted} />
</form>
);
}
});
2015-06-01 14:49:13 +02:00
export default LoanForm;