Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Start download while data is still being fetched from API calls #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gpadbidri opened this issue Aug 4, 2022 · 1 comment
Open

Comments

@gpadbidri
Copy link

I want to download CSV considering the following scenario :

  1. Run the API Call to fetch data from an API call for the number of pages. Meaning if I have 10 pages with pageSize (number of records per page) = 50 -> So I invoke the API 10 times. But I also want the download to start immideately after the first api call, that way I dont load the browser and user sees some progress that download hass started. For consecutive downloads I would add the response to the data object. Is it possibe withr react-csv ?

I need while the data is not yet pulled completely; I want to start downloading

@davidfrisch
Copy link

Hey,
while waiting for a fix, here's a workaround.

import "./styles.css";
import { useEffect, useRef, useState } from "react";
import { CSVLink } from "react-csv";
import MyService from "../../services/MyService";
import FileDownloadIcon from "@mui/icons-material/FileDownload";
import { LoadingButton } from "@mui/lab";

type Props = {
   id:number
};

export function ButtonToCSV({
   id
}: Props) {
  const [csvData, setCsvData] = useState<any>([]);
  const [isLoading, setIsLoading] = useState(false);
  const csvLinkEl = useRef<any>(null);

  const handleDownload = async (event) => {
    if (!isLoading) {
      setIsLoading(true);
      try {
        const data = await MyService.getCSV(id);
        setCsvData(data);
        setIsLoading(false);
      } catch (error) {
        console.log(error);
        setIsLoading(false);
      }
    }
  };

  useEffect(() => {
    if (csvData.length > 0 && csvLinkEl.current && !isLoading) {
      csvLinkEl.current.link.click();
    }
  }, [csvData]);

  return (
    <div>
      <LoadingButton
        loading={isLoading}
        loadingPosition="start"
        startIcon={<FileDownloadIcon />}
        variant="outlined"
        color="primary"
        className="button-export-csv-container"
        onClick={handleDownload}
      >
        Export
      </LoadingButton>
      <CSVLink
        asyncOnClick={true}
        style={{ color: "#000" }}
        data={csvData}
        ref={csvLinkEl}
      ></CSVLink>
    </div>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants