@@ -7,73 +7,87 @@ import {SALESFORCE_PROJECT_LEAD_LINK} from '../../../../config/constants'
77
88import styles from './styles.module.scss'
99
10+ /**
11+ * Build Select option from Billing Account object
12+ * @param {{ name: string, tcBillingAccountId: number } } billingAccountObj billing account object
13+ *
14+ * @returns {{ label: string, value: number } } option for Select
15+ */
16+ const buildOption = ( billingAccountObj ) => ( {
17+ label : `${ billingAccountObj . name } (${ billingAccountObj . tcBillingAccountId } )` ,
18+ value : billingAccountObj . tcBillingAccountId
19+ } )
20+
1021class BillingAccountField extends React . Component {
1122 constructor ( props ) {
1223 super ( props )
1324
1425 this . state = {
26+ isLoading : true ,
1527 billingAccounts : [ ] ,
16- selectedBillingAccount : this . props . billingAccountId
28+ selectedBillingAccount : null ,
1729 }
1830
1931 this . handleChange = this . handleChange . bind ( this )
20- this . mapOpts = this . mapOpts . bind ( this )
2132 }
2233
2334 componentDidMount ( ) {
2435 fetchBillingAccounts ( this . props . projectId )
25- . then ( ( { data : billingAccounts } ) => this . setState ( { billingAccounts} ) )
26- . then ( ( ) => {
27- if ( this . props . billingAccountId ) {
28- this . setState ( state => {
29- const existentBillingAccount = _ . find ( state . billingAccounts , {
30- tcBillingAccountId : this . props . billingAccountId
31- } )
32- if ( ! existentBillingAccount ) {
33- const billingAccountObj = {
34- name : '<Assigned Account>' ,
35- tcBillingAccountId : this . props . billingAccountId
36- }
37- return {
38- billingAccounts : [ ...state . billingAccounts , billingAccountObj ] ,
39- selectedBillingAccount : this . mapOpts ( billingAccountObj )
40- }
41- } else {
42- return {
43- selectedBillingAccount : this . mapOpts ( existentBillingAccount )
44- }
45- }
36+ . catch ( error => {
37+ // in case of error during loading billing accounts list
38+ // still show the empty list or currently selected value
39+ // and just log error to console
40+ console . error ( error )
41+ return { data : [ ] }
42+ } )
43+ . then ( ( { data } ) => {
44+ let billingAccounts = data . map ( buildOption )
45+ let selectedBillingAccount = null
46+
47+ // if some value is already selected we have to find and select the option for it
48+ if ( this . props . value ) {
49+ selectedBillingAccount = _ . find ( billingAccounts , {
50+ value : this . props . value
4651 } )
52+
53+ // if option is not on the list, then create such option
54+ // this is needed if the user doesn't have access to the selected account
55+ if ( ! selectedBillingAccount ) {
56+ selectedBillingAccount = buildOption ( {
57+ name : '<Assigned Account>' ,
58+ tcBillingAccountId : this . props . value
59+ } )
60+
61+ billingAccounts = [ selectedBillingAccount , ...billingAccounts ]
62+ }
4763 }
64+
65+ this . setState ( {
66+ isLoading : false ,
67+ billingAccounts,
68+ selectedBillingAccount,
69+ } )
4870 } )
49- . catch ( console . error )
5071 }
5172
5273 handleChange ( value ) {
53- this . setState ( { selectedBillingAccount : value } )
74+ this . setState ( { selectedBillingAccount : value } )
5475 this . props . setValue ( value . value )
5576 }
5677
57- mapOpts ( opt ) {
58- return {
59- label : `${ opt . name } (${ opt . tcBillingAccountId } )` ,
60- value : opt . tcBillingAccountId
61- }
62- }
63-
6478 render ( ) {
79+ const placeholder = this . state . billingAccounts . length > 0
80+ ? 'Select billing account'
81+ : 'No Billing Account Available'
82+
6583 return (
6684 < div className = { styles . container } >
6785 < div className = { styles . fieldName } > Choose a Billing Account</ div >
6886 < Select
69- placeholder = {
70- this . state . billingAccounts . length > 0
71- ? 'Select billing account'
72- : 'No Billing Account Available'
73- }
87+ placeholder = { this . state . isLoading ? 'Loading...' : placeholder }
7488 onChange = { this . handleChange }
7589 value = { this . state . selectedBillingAccount }
76- options = { this . state . billingAccounts . map ( this . mapOpts ) }
90+ options = { this . state . billingAccounts }
7791 isDisabled = { this . state . billingAccounts . length === 0 }
7892 />
7993 < div className = { styles . manageBillingAccountLinkWrapper } >
0 commit comments