From 23eb3a7fd9cc5f0322b19e091f3913f3ef7caf0f Mon Sep 17 00:00:00 2001 From: diminator Date: Thu, 10 Sep 2015 20:20:42 +0200 Subject: [PATCH] Fetch contractagreements, order them and filter by accepted or pending Merge remote-tracking branch 'remotes/origin/AD-885-convert-roles-to-acls' into AD-924-apply-contractagreements-to-loan- Conflicts: ownership/serializers.py --- js/actions/contract_agreement_list_actions.js | 40 +++++++++++++++++++ js/components/ascribe_forms/form_loan.js | 32 ++++++++++----- js/fetchers/ownership_fetcher.js | 12 ++++++ js/stores/contract_agreement_list_store.js | 22 ++++++++++ 4 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 js/actions/contract_agreement_list_actions.js create mode 100644 js/stores/contract_agreement_list_store.js diff --git a/js/actions/contract_agreement_list_actions.js b/js/actions/contract_agreement_list_actions.js new file mode 100644 index 00000000..2c34be65 --- /dev/null +++ b/js/actions/contract_agreement_list_actions.js @@ -0,0 +1,40 @@ +'use strict'; + +import alt from '../alt'; +import Q from 'q'; + +import OwnershipFetcher from '../fetchers/ownership_fetcher'; + + +class ContractAgreementListActions { + constructor() { + this.generateActions( + 'updateContractAgreementList', + 'flushContractAgreementList' + ); + } + + fetchContractAgreementList(issuer, accepted, pending) { + return Q.Promise((resolve, reject) => { + OwnershipFetcher.fetchContractAgreementList(issuer, accepted, pending) + .then((contractAgreementList) => { + if (contractAgreementList.count > 0) { + this.actions.updateContractAgreementList(contractAgreementList.results); + } + else { + this.actions.updateContractAgreementList(null); + } + resolve(); + }) + .catch((err) => { + console.logGlobal(err); + this.actions.updateContractAgreementList(null); + reject(err); + }); + } + ); + } + +} + +export default alt.createActions(ContractAgreementListActions); diff --git a/js/components/ascribe_forms/form_loan.js b/js/components/ascribe_forms/form_loan.js index 44a24ad2..a307acae 100644 --- a/js/components/ascribe_forms/form_loan.js +++ b/js/components/ascribe_forms/form_loan.js @@ -12,8 +12,8 @@ import InputTextAreaToggable from './input_textarea_toggable'; import InputDate from './input_date'; import InputCheckbox from './input_checkbox'; -import ContractStore from '../../stores/contract_store'; -import ContractActions from '../../actions/contract_actions'; +import ContractAgreementListStore from '../../stores/contract_agreement_list_store'; +import ContractAgreementListActions from '../../actions/contract_agreement_list_actions'; import AppConstants from '../../constants/application_constants'; @@ -48,16 +48,24 @@ let LoanForm = React.createClass({ }, getInitialState() { - return ContractStore.getState(); + return ContractAgreementListStore.getState(); }, componentDidMount() { - ContractStore.listen(this.onChange); - ContractActions.flushContract.defer(); + ContractAgreementListStore.listen(this.onChange); + if (this.props.email){ + ContractAgreementListActions.fetchContractAgreementList( + this.props.email, 'True', null); + } + + /* @Tim: + throws Uncaught TypeError: Cannot read property 'defer' of undefined + We might not need this + ContractAgreementListActions.flushContractAgreementList().defer();*/ }, componentWillUnmount() { - ContractStore.unlisten(this.onChange); + ContractAgreementListStore.unlisten(this.onChange); }, onChange(state) { @@ -71,17 +79,19 @@ let LoanForm = React.createClass({ handleOnChange(event) { // event.target.value is the submitted email of the loanee if(event && event.target && event.target.value && event.target.value.match(/.*@.*/)) { - ContractActions.fetchContract(event.target.value); + ContractAgreementListActions.fetchContractAgreementList(event.target.value, 'True', null); } else { - ContractActions.flushContract(); + ContractAgreementListActions.flushContractAgreementList(); } }, getContractCheckbox() { - if(this.state.contractKey && this.state.contractUrl) { + 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 contract = this.state.contractAgreementList[0].contract; + return ( {getLangText('I agree to the')}  - - {getLangText('terms of')} {this.state.contractEmail} + + {getLangText('terms of')} {contract.issuer} diff --git a/js/fetchers/ownership_fetcher.js b/js/fetchers/ownership_fetcher.js index c0d32d71..6738a59f 100644 --- a/js/fetchers/ownership_fetcher.js +++ b/js/fetchers/ownership_fetcher.js @@ -19,6 +19,18 @@ let OwnershipFetcher = { return requests.get(ApiUrls.ownership_contract_list, isActive); }, + /** + * Fetch the contractagreement between the logged-in user and the email from the API. + */ + fetchContractAgreementList(issuer, accepted, pending) { + let queryParams = { + issuer, + accepted, + pending + }; + return requests.get(ApiUrls.ownership_contract_agreements, queryParams); + }, + fetchLoanPieceRequestList(){ return requests.get(ApiUrls.ownership_loans_pieces_request); }, diff --git a/js/stores/contract_agreement_list_store.js b/js/stores/contract_agreement_list_store.js new file mode 100644 index 00000000..ca1d8e6b --- /dev/null +++ b/js/stores/contract_agreement_list_store.js @@ -0,0 +1,22 @@ +'use strict'; + +import alt from '../alt'; +import ContractAgreementListActions from '../actions/contract_agreement_list_actions'; + + +class ContractAgreementListStore { + constructor() { + this.contractAgreementList = null; + this.bindActions(ContractAgreementListActions); + } + + onUpdateContractAgreementList(contractAgreementList) { + this.contractAgreementList = contractAgreementList; + } + + onFlushContractAgreementList() { + this.contractAgreementList = null; + } +} + +export default alt.createStore(ContractAgreementListStore, 'ContractAgreementListStore');