Add import and export functionality
This commit is contained in:
@@ -18,6 +18,10 @@ export function getNextId() {
|
||||
return nextId;
|
||||
}
|
||||
|
||||
export function setAdventures(importArray: Adventure[]) {
|
||||
adventures = importArray
|
||||
}
|
||||
|
||||
export function addAdventure(adventure: Adventure) {
|
||||
adventures = [...adventures, adventure];
|
||||
if (isBrowser) {
|
||||
|
||||
11
src/services/export.ts
Normal file
11
src/services/export.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { Adventure } from '$lib/utils/types';
|
||||
import { getAdventures } from './adventureService';
|
||||
|
||||
export function exportData() {
|
||||
let adventures: Adventure[] = getAdventures()
|
||||
let jsonArray = JSON.stringify(adventures)
|
||||
console.log(jsonArray)
|
||||
let blob = new Blob([jsonArray], {type: "application/json"});
|
||||
let url = URL.createObjectURL(blob);
|
||||
return url
|
||||
}
|
||||
12
src/services/import.ts
Normal file
12
src/services/import.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { Adventure } from '$lib/utils/types';
|
||||
import { setAdventures } from './adventureService';
|
||||
|
||||
export function importData(file:File) {
|
||||
let reader = new FileReader();
|
||||
reader.onload = function() {
|
||||
let importArray: Adventure[] = JSON.parse(reader.result as string);
|
||||
setAdventures(importArray);
|
||||
}
|
||||
reader.readAsText(file);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user