Skip to content

Commit 7a3d988

Browse files
johan-bjareholtErikBjare
authored andcommitted
feat: Support import and export of categories
1 parent 0b60162 commit 7a3d988

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/store/modules/categories.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ const mutations = {
5757
console.log('Loaded classes:', state.classes);
5858
state.classes_unsaved_changes = false;
5959
},
60+
import(state, classes) {
61+
let i = 0;
62+
// overwrite id even if already set
63+
state.classes = classes.map(c => Object.assign(c, { id: i++ }));
64+
console.log('Loaded classes:', state.classes);
65+
state.classes_unsaved_changes = true;
66+
},
6067
updateClass(state, new_class) {
6168
console.log('Updating class:', new_class);
6269

src/views/settings/CategorizationSettings.vue

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ div
66
b-btn.ml-1(@click="restoreDefaultClasses", variant="outline-warning" size="sm")
77
icon(name="undo")
88
| Restore defaults
9+
label.btn.btn-sm.ml-1.btn-outline-primary(style="margin: 0")
10+
| Import
11+
input(type="file" @change="importCategories" hidden)
12+
b-btn.ml-1(@click="exportClasses", variant="outline-primary" size="sm")
13+
| Export
914
div
1015
| Rules for categorizing events. An event can only have one category. If several categories match, the deepest one will be chosen.
1116

@@ -61,6 +66,41 @@ export default {
6166
restoreDefaultClasses: async function () {
6267
await this.$store.commit('categories/restoreDefaultClasses');
6368
},
69+
exportClasses: function () {
70+
console.log('Exporting categories...');
71+
72+
const export_data = {
73+
categories: JSON.parse(localStorage.classes),
74+
};
75+
const text = JSON.stringify(export_data);
76+
const filename = 'aw-category-export.json';
77+
78+
// Initiate downloading a file by creating a hidden button and clicking it
79+
const element = document.createElement('a');
80+
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
81+
element.setAttribute('download', filename);
82+
element.style.display = 'none';
83+
document.body.appendChild(element);
84+
element.click();
85+
document.body.removeChild(element);
86+
},
87+
importCategories: async function (elem) {
88+
console.log('Importing categories...');
89+
90+
// Get file from upload
91+
const file = elem.originalTarget.files[0];
92+
if (file.type != 'application/json') {
93+
console.error('Only JSON files are possible to import');
94+
return;
95+
}
96+
97+
// Read and parse import text to JSON
98+
const text = await file.text();
99+
const import_obj = JSON.parse(text);
100+
101+
// Set import to categories as unsaved changes
102+
this.$store.commit('categories/import', import_obj.categories);
103+
},
64104
},
65105
};
66106
</script>

0 commit comments

Comments
 (0)