You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  1. import React from "react";
  2. import { getTranslation } from "../core/i18n/I18NHandler";
  3. import { getSensorLog } from "../core/log/SensorLogger";
  4. import { Button } from "@geist-ui/core";
  5. import { getUserID } from "./RandomIDComponent";
  6. import { PARTICIPANT_NUMBER } from "../core/Constants";
  7. function DownloadButton() {
  8. const downloadFile = ({ data, fileName, fileType }) => {
  9. const blob = new Blob([data], { type: fileType });
  10. const a = document.createElement("a");
  11. a.download = fileName;
  12. a.href = window.URL.createObjectURL(blob);
  13. const clickEvt = new MouseEvent("click", {
  14. view: window,
  15. bubbles: true,
  16. cancelable: true,
  17. });
  18. a.dispatchEvent(clickEvt);
  19. a.remove();
  20. };
  21. const userData = {
  22. participantId: getUserID(),
  23. sensorLog: getSensorLog(),
  24. participantNumber: PARTICIPANT_NUMBER,
  25. };
  26. const exportToJson = (e) => {
  27. e.preventDefault();
  28. downloadFile({
  29. data: JSON.stringify(userData),
  30. fileName: "sensorData.json",
  31. fileType: "text/json",
  32. });
  33. };
  34. return <Button onClick={exportToJson}>{getTranslation("download")}</Button>;
  35. }
  36. export default DownloadButton;