From 1320b5c965fe55e832e7562bb66988f85d6319ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 22 Sep 2015 16:45:24 +0200 Subject: [PATCH] cleanup form_loan and contract_agreement_list_actions --- js/actions/contract_agreement_list_actions.js | 48 +++++++--- js/components/ascribe_forms/form_loan.js | 92 +++++++++++-------- js/components/ascribe_forms/input_checkbox.js | 6 +- .../ikonotv/ikonotv_contract_notifications.js | 41 +++++---- .../ikonotv/ikonotv_register_piece.js | 5 +- sass/ascribe_notification_page.scss | 23 +++-- 6 files changed, 136 insertions(+), 79 deletions(-) diff --git a/js/actions/contract_agreement_list_actions.js b/js/actions/contract_agreement_list_actions.js index cd78b52b..589c1f51 100644 --- a/js/actions/contract_agreement_list_actions.js +++ b/js/actions/contract_agreement_list_actions.js @@ -35,27 +35,47 @@ class ContractAgreementListActions { ); } - fetchAvailableContractAgreementList(issuer){ + fetchAvailableContractAgreementList(issuer, createContractAgreement) { return Q.Promise((resolve, reject) => { - this.actions.fetchContractAgreementList(issuer, true, null) - .then((contractAgreementListAccepted) => { - if (!contractAgreementListAccepted) { - // fetch pending agreements if no accepted ones - return this.actions.fetchContractAgreementList(issuer, null, true); + OwnershipFetcher.fetchContractAgreementList(issuer, true, null) + .then((acceptedContractAgreementList) => { + // if there is at least an accepted contract agreement, we're going to + // use it + if(acceptedContractAgreementList.count > 0) { + this.actions.updateContractAgreementList(acceptedContractAgreementList.results); + } else { + // otherwise, we're looking for contract agreements that are still pending + // + // Normally nesting promises, but for this conditional one, it makes sense to not + // overcomplicate the method + OwnershipFetcher.fetchContractAgreementList(issuer, null, true) + .then((pendingContractAgreementList) => { + if(pendingContractAgreementList.count > 0) { + this.actions.updateContractAgreementList(pendingContractAgreementList.results); + } else { + // if there was neither a pending nor an active contractAgreement + // found and createContractAgreement is set to true, we create a + // new contract agreement + if(createContractAgreement) { + this.actions.createContractAgreementFromPublicContract(issuer); + } + } + }) + .catch((err) => { + console.logGlobal(err); + reject(err); + }); } - else { - resolve(contractAgreementListAccepted); - } - }).then((contractAgreementListPending) => { - resolve(contractAgreementListPending); - }).catch((err) => { + }) + .catch((err) => { console.logGlobal(err); reject(err); }); - }); + } + ); } - createContractAgreementFromPublicContract(issuer){ + createContractAgreementFromPublicContract(issuer) { ContractListActions.fetchContractList(null, null, issuer) .then((publicContract) => { // create an agreement with the public contract if there is one diff --git a/js/components/ascribe_forms/form_loan.js b/js/components/ascribe_forms/form_loan.js index 5f7c5d78..b9942ea8 100644 --- a/js/components/ascribe_forms/form_loan.js +++ b/js/components/ascribe_forms/form_loan.js @@ -59,10 +59,13 @@ let LoanForm = React.createClass({ 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) { - // however, it can also be that at the time the component is mounting, - // the email is not defined (because it's asynchronously fetched from the server). - // Then we need to update it as soon as it is included into LoanForm's props. if(nextProps && nextProps.email && this.props.email !== nextProps.email) { this.getContractAgreementsOrCreatePublic(nextProps.email); } @@ -80,14 +83,7 @@ let LoanForm = React.createClass({ ContractAgreementListActions.flushContractAgreementList.defer(); if (email) { // fetch the available contractagreements (pending/accepted) - ContractAgreementListActions.fetchAvailableContractAgreementList(email).then( - (contractAgreementList) => { - if (!contractAgreementList && this.props.createPublicContractAgreement) { - // for public contracts: fetch the public contract and create a contractagreement if available - ContractAgreementListActions.createContractAgreementFromPublicContract(email); - } - } - ); + ContractAgreementListActions.fetchAvailableContractAgreementList(email, true); } }, @@ -119,25 +115,46 @@ let LoanForm = React.createClass({ // 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; + let contractAgreement = this.state.contractAgreementList[0]; + let contract = contractAgreement.contract; - return ( - - - - {getLangText('I agree to the')}  - - {getLangText('terms of ')} {contract.issuer} - - - - - ); + if(contractAgreement.datetime_accepted) { + return ( + + ); + } else { + return ( + + + + {getLangText('I agree to the')}  + + {getLangText('terms of ')} {contract.issuer} + + + + + ); + } } else { return ( -

{getLangText('Appendix')}

-
-                            {appendix.default}
-                        
- + +
{appendix.default}
+
); } } @@ -214,7 +230,7 @@ let LoanForm = React.createClass({ name='loanee' label={getLangText('Loanee Email')} editable={!this.props.email} - onBlur={this.handleOnChange} + onChange={this.handleOnChange} overrideForm={!!this.props.email}>
+ {this.getContractCheckbox()} + {this.getAppendix()} - {this.getContractCheckbox()} - {this.getAppendix()} {this.props.children} ); diff --git a/js/components/ascribe_forms/input_checkbox.js b/js/components/ascribe_forms/input_checkbox.js index 275b2374..38885441 100644 --- a/js/components/ascribe_forms/input_checkbox.js +++ b/js/components/ascribe_forms/input_checkbox.js @@ -25,7 +25,10 @@ let InputCheckbox = React.createClass({ // provided by Property disabled: React.PropTypes.bool, - onChange: React.PropTypes.func + onChange: React.PropTypes.func, + + // can be used to style the component from the outside + style: React.PropTypes.object }, // As HTML inputs, we're setting the default value for an input to checked === false @@ -98,6 +101,7 @@ let InputCheckbox = React.createClass({ return ( - -
- - - - Download PDF version - - -
+ ); } @@ -97,12 +94,11 @@ let IkonotvContractNotifications = React.createClass({ let appendix = notifications.contract_agreement.appendix; if (appendix && appendix.default) { return ( -
-

{getLangText('Appendix')}

-
-                        {appendix.default}
-                    
-
+ +
{appendix.default}
+
); } return null; @@ -153,9 +149,12 @@ let IkonotvContractNotifications = React.createClass({ }, render() { - if (this.state.contractAgreementListNotifications && this.state.contractAgreementListNotifications.length > 0) { + + let notifications = this.state.contractAgreementListNotifications[0]; + let blob = notifications.contract_agreement.contract.blob; + return (
@@ -168,6 +167,14 @@ let IkonotvContractNotifications = React.createClass({ {this.getContract()}
{this.getAppendix()} + {this.getCopyrightAssociationForm()}