aboutsummaryrefslogtreecommitdiff
path: root/src/components/ActionBar.vue
blob: da70b8afc26128445489dd4f887a333dee5abbe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<template>
  <div id="action-bar">
    <FileUpload
      :accept="'application/json'"
      @fileChanged="handleLabChange">Import Lab Schedule</FileUpload>
    <FileUpload
      :accept="'text/plain'"
      :multiple="true"
      @fileChanged="handlePtChange">Upload PT Schedule</FileUpload>
      <UIButton @click="save">Export</UIButton>
  </div>
</template>

<script>
import FileUpload from '@/components/FileUpload.vue';
import { parseLabFile, parsePtSchedule } from '@/features/parser';
import UIButton from '@/components/UIButton.vue';

export default {
  name: 'ActionBar',
  components: {
    FileUpload,
    UIButton,
  },
  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>

<style>
#action-bar > * {
  margin-left: 0.5rem;
}

#action-bar > *:first-child {
  margin-left: 0;
}
</style>