feat: download

- able to download file from express
This commit is contained in:
cv
2021-05-26 23:10:00 +02:00
parent 250946fdc6
commit b61a82627e
6 changed files with 63 additions and 0 deletions
+15
View File
@@ -1,6 +1,21 @@
import axios from 'axios';
import { eventsURL } from '../api/apiConstants';
export const downloadEvents = async () => {
await axios({
url: eventsURL + '/download',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'events.json');
document.body.appendChild(link);
link.click();
});
};
export const fetchAllEvents = async () => {
const res = await axios.get(eventsURL);
return res.data;
@@ -0,0 +1,18 @@
import { IconButton } from '@chakra-ui/button';
import { FiDownload } from 'react-icons/fi';
export default function DownloadIconBtn(props) {
const { clickhandler, ...rest } = props;
return (
<IconButton
size={props.size || 'xs'}
icon={<FiDownload />}
isRound
variant='outline'
onClick={clickhandler}
_focus={{ boxShadow: 'none' }}
colorScheme='whiteAlpha'
{...rest}
/>
);
}
+8
View File
@@ -9,6 +9,8 @@ import { useDisclosure } from '@chakra-ui/hooks';
import SettingsModal from '../modals/SettingsModal';
import SettingsIconBtn from 'common/components/buttons/SettingsIconBtn';
import { useEffect } from 'react';
import DownloadIconBtn from 'common/components/buttons/DownloadIconBtn';
import { downloadEvents } from 'app/api/eventsApi';
export default function Editor() {
const { isOpen, onOpen, onClose } = useDisclosure();
@@ -18,6 +20,11 @@ export default function Editor() {
document.title = 'ontime - Editor';
}, []);
const handleDownload = () => {
console.log('debug download');
downloadEvents();
};
return (
<>
<SettingsModal isOpen={isOpen} onClose={onClose} />
@@ -67,6 +74,7 @@ export default function Editor() {
<Box className={styles.settings}>
<div className={styles.content}>
<SettingsIconBtn size='md' clickhandler={onOpen} />
<DownloadIconBtn size='md' clickhandler={handleDownload} />
</div>
</Box>
</div>
@@ -101,6 +101,12 @@
padding-top: 1.5em;
}
.settings > .content {
display: flex;
flex-direction: column;
gap: 1em;
}
.cornerButtonContainer {
position: relative;
top: -4.5em;