From 309f893dbcf50e021588111c6644848cf177373b Mon Sep 17 00:00:00 2001
From: Furkan Sahin <furkan-dev@proton.me>
Date: Tue, 19 Jul 2022 15:35:32 -0400
Subject: Add option to export to DB, also make function to unmarshall/strigify
 a db object. See whether this function can instead be moved to the
 `DatabaseFile` interface, just like `UnmarshalJson` in `Go`

---
 src/components/FileUploads.svelte | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

(limited to 'src/components/FileUploads.svelte')

diff --git a/src/components/FileUploads.svelte b/src/components/FileUploads.svelte
index 2c246f7..df952df 100644
--- a/src/components/FileUploads.svelte
+++ b/src/components/FileUploads.svelte
@@ -37,7 +37,7 @@
             snackbar.open();
           }
         })
-        .finally( () => {
+        .finally(() => {
           snackbarText = "Sucessfullyed imported Peer Teacher/s!";
           snackbar.open();
         });
@@ -80,7 +80,7 @@
     }
   }
 
-  function exportDB() {
+  function dbStringify(): string {
     const peerTeachers = [...$ptStore.values()];
     const labs = [...$labStore.values()];
     const database = {
@@ -98,6 +98,11 @@
       return value;
     });
 
+    return dbObj;
+  }
+
+  function exportDB() {
+    const dbObj = dbStringify();
     const blob = new Blob([dbObj], { type: "text/json" });
     const anchor = document.createElement("a");
     const url = window.URL.createObjectURL(blob);
@@ -109,6 +114,11 @@
     document.body.removeChild(anchor);
     window.URL.revokeObjectURL(url);
   }
+
+  function exportDB2LocalStorage() {
+    const db = dbStringify();
+    localStorage.setItem("db", db);
+  }
 </script>
 
 <div class="flex flex-col items-center justify-center h-full ">
@@ -154,6 +164,15 @@
     >
       <button class="btn btn-warning" on:click={exportDB}>Download</button>
     </Card>
+
+    <Card
+      title="Export to Local Storage"
+      desc="Save current DB to local storage. Local storage db should only be used for testing purposes to avoid data anomalies"
+    >
+      <button class="btn btn-warning" on:click={exportDB2LocalStorage}
+        >LocalStorage</button
+      >
+    </Card>
   </div>
 </div>
 
-- 
cgit v1.2.3

