Add import and export functionality

This commit is contained in:
Sean Morley
2024-03-29 22:20:21 +00:00
parent 97d98003f6
commit bd5e1a813b
5 changed files with 43 additions and 1 deletions

View File

@@ -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
View 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
View 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);
}