diff options
| author | Furkan Sahin <furkan-dev@proton.me> | 2021-09-05 22:56:45 -0500 |
|---|---|---|
| committer | Furkan Sahin <furkan-dev@proton.me> | 2021-09-05 22:56:45 -0500 |
| commit | fea56ae09cd612003e1bafd2459556b67a5950e9 (patch) | |
| tree | a195fab20351891e6a65f68d77b88bf8c9671e75 /src/components/EditorActions.svelte | |
| parent | d0975a6e7ee57de4debda94e823011d813fbf4a1 (diff) | |
Add import/export functionality
Diffstat (limited to 'src/components/EditorActions.svelte')
| -rw-r--r-- | src/components/EditorActions.svelte | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/src/components/EditorActions.svelte b/src/components/EditorActions.svelte index a0372ed..fa2a041 100644 --- a/src/components/EditorActions.svelte +++ b/src/components/EditorActions.svelte @@ -3,7 +3,11 @@ import IconButton from "@smui/icon-button"; import Snackbar, { Actions } from "@smui/snackbar"; import FileUpload from "./FileUpload.svelte"; - import { parseLabScheduleFile, parsePTFile } from "../logic/EditorActions"; + import { + parseDatabaseFile, + parseLabScheduleFile, + parsePTFile, + } from "../logic/EditorActions"; import { labStore, ptStore } from "../stores"; let ptSchedules: FileList | null; @@ -50,10 +54,48 @@ } $: { - if (dbFile?.length) console.log(dbFile); + if (dbFile?.length) { + parseDatabaseFile(dbFile[0]) + .then((database) => { + labStore.set(database.labs); + ptStore.set(database.peerTeachers); + }) + .catch(() => { + snackbarText = "Failed to import database. See console for details."; + snackbar.open(); + }); + } } - function exportDB() {} + function exportDB() { + const peerTeachers = [...$ptStore.values()]; + const labs = [...$labStore.values()]; + const database = { + labs: labs, + peerTeachers: peerTeachers, + }; + + const dbObj = JSON.stringify(database, (_, value) => { + // Need to manually convert the PeerTeacher objects' + // `labs` set to an array because `JSON.stringify` doesn't + // support "stringing" it out of the box + if (typeof value === "object" && value instanceof Set) { + return [...value]; + } + return value; + }); + + const blob = new Blob([dbObj], { type: "text/json" }); + const anchor = document.createElement("a"); + const url = window.URL.createObjectURL(blob); + anchor.href = url; + anchor.download = "pt-db.json"; + anchor.style.display = "none"; + document.body.appendChild(anchor); + anchor.click(); + document.body.removeChild(anchor); + window.URL.revokeObjectURL(url); + } </script> <div id="action-bar"> |
