Add common download helper (#628)

* Refactor download functions into a common helper function

* Removed unused import
This commit is contained in:
Erik Michelson 2020-10-03 22:38:46 +02:00 committed by GitHub
parent c0d05904b2
commit 9b6fca29e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 20 deletions

View file

@ -5,10 +5,10 @@ import { useSelector } from 'react-redux'
import { deleteHistory, deleteHistoryEntry, getHistory, setHistory, updateHistoryEntry } from '../../api/history'
import { deleteNote } from '../../api/notes'
import { ApplicationState } from '../../redux'
import { download } from '../common/download/download'
import {
collectEntries,
downloadHistory,
loadHistoryFromLocalStore,
mergeEntryArrays,
setHistoryToLocalStore,
@ -90,7 +90,7 @@ export const HistoryPage: React.FC = () => {
version: 2,
entries: mergeEntryArrays(localHistoryEntries, remoteHistoryEntries)
}
downloadHistory(dataObject)
download(JSON.stringify(dataObject), `history_${(new Date()).getTime()}.json`, 'application/json')
}, [localHistoryEntries, remoteHistoryEntries])
const clearHistory = useCallback(() => {

View file

@ -3,7 +3,6 @@ import { SortModeEnum } from './sort-button/sort-button'
import {
HistoryEntry,
HistoryEntryOrigin,
HistoryJson,
LocatedHistoryEntry
} from './history-page'
import { HistoryToolbarState } from './history-toolbar/history-toolbar'
@ -126,13 +125,3 @@ export function loadHistoryFromLocalStore (): HistoryEntry[] {
export function setHistoryToLocalStore (entries: HistoryEntry[]): void {
window.localStorage.setItem('history', JSON.stringify(entries))
}
export function downloadHistory (dataObject: HistoryJson): void {
const data = 'data:text/json;charset=utf-8;base64,' + Buffer.from(JSON.stringify(dataObject)).toString('base64')
const downloadLink = document.createElement('a')
downloadLink.setAttribute('href', data)
downloadLink.setAttribute('download', `history_${(new Date()).getTime()}.json`)
document.body.appendChild(downloadLink)
downloadLink.click()
downloadLink.remove()
}