From 803ae60cd49f39400f1bfcb861fe34147279f168 Mon Sep 17 00:00:00 2001 From: Denis Thiessen Date: Mon, 20 May 2024 13:15:58 +0200 Subject: [PATCH] feat: Added data download button --- public/locales/de/translation.json | 3 ++- public/locales/en/translation.json | 3 ++- src/components/DownloadButton.jsx | 33 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/components/DownloadButton.jsx diff --git a/public/locales/de/translation.json b/public/locales/de/translation.json index 355bd89..56c66cc 100644 --- a/public/locales/de/translation.json +++ b/public/locales/de/translation.json @@ -1,3 +1,4 @@ { - "hello_world": "Hallo Welt" + "hello_world": "Hallo Welt", + "download": "Download" } \ No newline at end of file diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 4ea0b47..850859e 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -1,3 +1,4 @@ { - "hello_world": "Hello World" + "hello_world": "Hello World", + "download": "Download" } \ No newline at end of file diff --git a/src/components/DownloadButton.jsx b/src/components/DownloadButton.jsx new file mode 100644 index 0000000..f18bd65 --- /dev/null +++ b/src/components/DownloadButton.jsx @@ -0,0 +1,33 @@ +import React from "react"; +import { getTranslation } from "../core/i18n/I18NHandler"; +import { getSensorLog } from "../core/log/SensorLogger"; +import { Button } from '@geist-ui/core'; + +function DownloadButton() { + const downloadFile = ({ data, fileName, fileType }) => { + const blob = new Blob([data], { type: fileType }); + const a = document.createElement('a'); + a.download = fileName; + a.href = window.URL.createObjectURL(blob); + const clickEvt = new MouseEvent('click', { + view: window, + bubbles: true, + cancelable: true, + }); + a.dispatchEvent(clickEvt); + a.remove(); + } + + const exportToJson = e => { + e.preventDefault(); + downloadFile({ + data: JSON.stringify(getSensorLog()), + fileName: 'sensorData.json', + fileType: 'text/json', + }) + } + + return (); +} + +export default DownloadButton; \ No newline at end of file