The setBalance() method is called to set the current user balance. It is always invoked after a new transaction is submitted to the server. It calls the /customerinfo endpoint and updates the new balance to the app state, as follows:
- We call the /customerinfo endpoint with the customer's userID from the app state, like this:
setBalance = () => {
let app = this;
var url = 'http://'+ this.state.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 receiving a response from the /customerinfo endpoint, we fetch the balance variable and update it to the app state, like this:
if(response)
{
return response.json();
}
else
{
console.log(error);
}
}).then(function(data){
app.setState({
balance: data.balance
});
})
}
That brings us to the end of our frontend and all of the components we need to develop for our project. In the next section, let's try out an end-to-end remittance transaction and see how the project works.