From 74e6cc31e83ad570a9f06765d288e9024736e73f Mon Sep 17 00:00:00 2001
From: Furkan Sahin <furkan-dev@proton.me>
Date: Sat, 10 Apr 2021 21:35:13 -0500
Subject: Commit MVP

---
 src/components/ActionBar.vue | 75 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 src/components/ActionBar.vue

(limited to 'src/components/ActionBar.vue')

diff --git a/src/components/ActionBar.vue b/src/components/ActionBar.vue
new file mode 100644
index 0000000..96dbaae
--- /dev/null
+++ b/src/components/ActionBar.vue
@@ -0,0 +1,75 @@
+<template>
+  <div>
+    <file-upload
+      :accept="'text/plain'"
+      :multiple="true"
+      @file-changed="handlePtChange">Upload PT Schedule</file-upload>
+    <file-upload
+      :accept="'application/json'"
+      @file-changed="handleLabChange">Upload Lab Schedule</file-upload>
+    <button @click="save">Export</button>
+  </div>
+</template>
+
+<script>
+import FileUpload from '@/components/FileUpload.vue';
+
+import { parseLabFile, parsePtSchedule } from '@/features/parser';
+
+export default {
+  name: 'ActionBar',
+  components: {
+    FileUpload,
+  },
+  methods: {
+    handleLabChange(files) {
+      parseLabFile(files[0])
+        .then((data) => {
+          this.$store.commit('importLabs', data);
+        })
+        .catch((error) => {
+          console.error(error);
+        });
+    },
+    handlePtChange(files) {
+      const result = [];
+      const promises = [];
+
+      files.forEach((file) => {
+        const p = parsePtSchedule(file).then((data) => result.push(data));
+        promises.push(p);
+      });
+
+      Promise.all(promises)
+        .then(() => {
+          this.$store.commit('addPeerTeachers', result);
+        });
+    },
+    save() {
+      const database = {
+        labs: Object.fromEntries(this.$store.state.labs),
+        peerTeachers: Object.fromEntries(this.$store.state.peerTeachers),
+      };
+
+      const jsonObj = JSON.stringify(database, (_, value) => {
+        if (typeof value === 'object' && value instanceof Set) {
+          return [...value];
+        }
+        return value;
+      });
+
+      const blob = new Blob([jsonObj], { 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>
-- 
cgit v1.2.3

