diff --git a/js/actions/contract_actions.js b/js/actions/contract_actions.js deleted file mode 100644 index d1bf1432..00000000 --- a/js/actions/contract_actions.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -import alt from '../alt'; -import OwnershipFetcher from '../fetchers/ownership_fetcher'; - - -class ContractActions { - constructor() { - this.generateActions( - 'updateContract', - 'flushContract' - ); - } - - fetchContract(email) { - if(email.match(/.+\@.+\..+/)) { - OwnershipFetcher.fetchContract(email) - .then((contracts) => { - if (contracts && contracts.length > 0) { - this.actions.updateContract({ - contractKey: contracts[0].s3Key, - contractUrl: contracts[0].s3Url, - contractEmail: email - }); - } - else { - this.actions.updateContract({ - contractKey: null, - contractUrl: null, - contractEmail: null - }); - } - }) - .catch((err) => { - console.logGlobal(err); - this.actions.updateContract({ - contractKey: null, - contractUrl: null, - contractEmail: null - }); - }); - } - } - -} - -export default alt.createActions(ContractActions); diff --git a/js/actions/contract_agreement_list_actions.js b/js/actions/contract_agreement_list_actions.js index 2c34be65..4f5ff7d6 100644 --- a/js/actions/contract_agreement_list_actions.js +++ b/js/actions/contract_agreement_list_actions.js @@ -4,7 +4,7 @@ import alt from '../alt'; import Q from 'q'; import OwnershipFetcher from '../fetchers/ownership_fetcher'; - +import ContractListActions from './contract_list_actions'; class ContractAgreementListActions { constructor() { @@ -16,25 +16,69 @@ class ContractAgreementListActions { fetchContractAgreementList(issuer, accepted, pending) { return Q.Promise((resolve, reject) => { + this.actions.updateContractAgreementList(null); OwnershipFetcher.fetchContractAgreementList(issuer, accepted, pending) .then((contractAgreementList) => { if (contractAgreementList.count > 0) { this.actions.updateContractAgreementList(contractAgreementList.results); + resolve(contractAgreementList.results); } - else { - this.actions.updateContractAgreementList(null); + else{ + resolve(null); } - resolve(); }) .catch((err) => { console.logGlobal(err); - this.actions.updateContractAgreementList(null); reject(err); }); } ); } + fetchAvailableContractAgreementList(issuer){ + this.actions.fetchContractAgreementList(issuer, 'True', null) + .then((contractAgreementListAccepted) => { + if (!contractAgreementListAccepted) { + // fetch pending agreements if no accepted ones + return this.actions.fetchContractAgreementList(issuer, null, 'True'); + } + }).then((contractAgreementListPending) => { + // fetch public contract if no accepted nor pending agreements + if (!contractAgreementListPending) { + return ContractListActions.fetchContractList(null, null, issuer); + } + }).then((publicContract) => { + // create an agreement with the public contract if there is one + if (publicContract && publicContract.length > 0) { + return this.actions.createContractAgreement(null, publicContract[0]); + } + else { + /* + contractAgreementList in the store is already set to null; + */ + } + }).then((publicContracAgreement) => { + if (publicContracAgreement) { + this.actions.updateContractAgreementList([publicContracAgreement]); + } + }).catch((err) => { + console.logGlobal(err); + }); + } + + createContractAgreement(issuer, contract){ + return Q.Promise((resolve, reject) => { + OwnershipFetcher.createContractAgreement(issuer, contract).then( + (contractAgreement) => { + resolve(contractAgreement); + } + ).catch((err) => { + console.logGlobal(err); + reject(err); + }); + }); + } + } export default alt.createActions(ContractAgreementListActions); diff --git a/js/actions/contract_list_actions.js b/js/actions/contract_list_actions.js index aaee33c6..a856fb2b 100644 --- a/js/actions/contract_list_actions.js +++ b/js/actions/contract_list_actions.js @@ -12,15 +12,19 @@ class ContractListActions { ); } - fetchContractList(isActive) { - OwnershipFetcher.fetchContractList(isActive) - .then((contracts) => { - this.actions.updateContractList(contracts.results); - }) - .catch((err) => { - console.logGlobal(err); - this.actions.updateContractList([]); - }); + fetchContractList(isActive, isPublic, issuer) { + return Q.Promise((resolve, reject) => { + OwnershipFetcher.fetchContractList(isActive, isPublic, issuer) + .then((contracts) => { + this.actions.updateContractList(contracts.results); + resolve(contracts.results); + }) + .catch((err) => { + console.logGlobal(err); + this.actions.updateContractList([]); + reject(err); + }); + }); } diff --git a/js/components/ascribe_forms/form_loan.js b/js/components/ascribe_forms/form_loan.js index a307acae..29d0ecd4 100644 --- a/js/components/ascribe_forms/form_loan.js +++ b/js/components/ascribe_forms/form_loan.js @@ -54,14 +54,8 @@ let LoanForm = React.createClass({ componentDidMount() { ContractAgreementListStore.listen(this.onChange); if (this.props.email){ - ContractAgreementListActions.fetchContractAgreementList( - this.props.email, 'True', null); + ContractAgreementListActions.fetchAvailableContractAgreementList.defer(this.props.email); } - - /* @Tim: - throws Uncaught TypeError: Cannot read property 'defer' of undefined - We might not need this - ContractAgreementListActions.flushContractAgreementList().defer();*/ }, componentWillUnmount() { diff --git a/js/fetchers/ownership_fetcher.js b/js/fetchers/ownership_fetcher.js index 6738a59f..fde66c7d 100644 --- a/js/fetchers/ownership_fetcher.js +++ b/js/fetchers/ownership_fetcher.js @@ -15,8 +15,21 @@ let OwnershipFetcher = { /** * Fetch the contracts of the logged-in user from the API. */ - fetchContractList(isActive){ - return requests.get(ApiUrls.ownership_contract_list, isActive); + fetchContractList(isActive, isPublic, issuer){ + let queryParams = { + isActive, + isPublic, + issuer + }; + return requests.get(ApiUrls.ownership_contract_list, queryParams); + }, + + + /** + * Create a contractagreement between the logged-in user and the email from the API with contract. + */ + createContractAgreement(signee, contractObj){ + return requests.post(ApiUrls.ownership_contract_agreements, { body: {signee: signee, contract: contractObj.id }}); }, /** diff --git a/js/stores/contract_store.js b/js/stores/contract_store.js deleted file mode 100644 index 623bec6a..00000000 --- a/js/stores/contract_store.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -import alt from '../alt'; -import ContractActions from '../actions/contract_actions'; - - -class ContractStore { - constructor() { - this.contractKey = null; - this.contractUrl = null; - this.contractEmail = null; - this.bindActions(ContractActions); - } - - onUpdateContract({contractKey, contractUrl, contractEmail}) { - this.contractKey = contractKey; - this.contractUrl = contractUrl; - this.contractEmail = contractEmail; - } - - onFlushContract() { - this.contractKey = null; - this.contractUrl = null; - this.contractEmail = null; - } -} - -export default alt.createStore(ContractStore, 'ContractStore');