React: onClick I want to download the csv files, with queryparam and if download fails due to wrong params want to display popup message

Viewed 81

onClick I want to download the csv files, with queryparam and if download fails due to wrong params want to display popup message

Here is my onClick doSubmit method,

 const doSubmit = () => {
    let timeZoneoffset = new Date().getTimezoneOffset();
    let location = `${apiEndpoint}/export-device-data?fromDate=${fromDateTime}&toDate=${toDateTime}&userId=${values.userId}&deviceTypeId=${deviceId}&timeZoneoffset=${timeZoneoffset}`;
    window.open(location, '_blank');
  };

with the above code I am able to download the csv file if I pass the correct params values. But when I pass the incorrect params values getting error object in new tab:

{"status":false,"responseCode":400,"message":"Invalid Request"}
1 Answers

I would suggest you to make a "GET" AJAX call and only if you get 200 as the HTTP status code then you should open the popup and load the page. And if you get > 400 in HTTP status code then show a popup.

fetch("linkedin.com")
.then(data => {
if(data.status < 400)
//popup
else
//display error message
enter code here
})

But with this you ll end up sending 2 requests to load the page.

Related