Writing a method for setting the user account

The setAccount() method is called by the AppLogin component. Depending on whether the user clicks on Acme Inc or Apex Corp, the input parameter, 1 or 2, is sent to the method.

If the value of the input parameter is 2, indicating the customer is Apex Corp (a Bank B customer), the server state variable is set to localhost:8001, which is the listening port of the backend server of Bank B, as shown in the following code block:

if (flag == 2)
{
server = 'localhost:8001'
};

Next, we fetch the customer's details from the backend server. We make a fetch call to the /customerinfo API endpoint, like this:

var url = 'http://'+ server +'/customerinfo';
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId : app.state.userId
})
}).then(function(response,error){
  • On the response from the backend server, we parse the response object for the customer's name, account, address, and current balance. We set these values to the app state.
  • Additionally, a bank variable is also added to the app state. The bank variable is to Bank A or Bank B, depending on whether the input parameter to the setAccount() method was 1 or 2. We also add the server variable with the listening port of the bank backend server, as follows:
then(function(data){

var bank;

if(flag == 1)
{
bank = 'Bank A';
}
else
{
bank = 'Bank B';
}

app.setState({
name: data.name,
account: data.account,
balance: data.balance,
address: data.address,
bank: bank,
server: server
});

Next, let's look at the view setters for the app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.60.192