I have around 10 similar components on the page with different names. Each component is drawn by for loop and have some buttons and data.
If I click one button on a component it carries out an API call and buttons becomes disable till the the call is in success.
The issue is if I click another button on another component button becomes disable(which is expected) but enables the previously clicked button( not expected) as the setState is changed. How can I make the button only enable when its in their own success state.
That is why I am adding the button names in the state of clickedRunButton making it an object but How to add the items in this object during the API call and remove it during the success of the call.
Instead of adding another item its overwriting it.
this.state = {
clickedRunButton:{ pipeName :''}
}
onClickRun(params) {
const url = `/api/${params}/run`;
let clickedRunButton = Object.assign({},
this.state.clickedRunButton); //creating copy of object
return $.ajax({
type: 'POST',
url,
processData: false,
contentType: 'application/json',
beforeSend :() =>{
clickedRunButton.pipeName = params;
clickedRunButton[pipeName] = "run"+params;
this.setState({
clickedRunButton
})
},
success: (response) => {
if(!response.error) {
clickedRunButton.pipeName = params;
clickedRunButton[pipeName] = '';
this.setState({
clickedRunButton
})
}
},
error: (error) => {
}
});
}