-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ability to save and restore settings and tasks (#228)
* feat: Add basic export and import buttons * feat: Improve settings management button designs and add i18n * feat: Make leading the same on settings manage description
- Loading branch information
Showing
4 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<template> | ||
<button class="px-4 py-3 transition border-2 border-red-400 rounded-full select-none dark:shadow-none hover:bg-red-400 active:bg-red-500 active:border-red-500 active:shadow-red-200 active:shadow-md" @click="downloadSettings" v-text="$i18n.t('settings.manage.buttons.save')" /> | ||
</template> | ||
|
||
<script> | ||
import { useSettings } from '@/stores/settings' | ||
import { useTasklist } from '@/stores/tasklist' | ||
export default { | ||
methods: { | ||
downloadSettings () { | ||
const settings = useSettings().$state | ||
const tasklist = useTasklist().$state | ||
const downloadObject = { | ||
settings, | ||
tasklist | ||
} | ||
const downloadElement = document.createElement('a') | ||
downloadElement.href = `data:text/plain;charset=utf-8,${encodeURIComponent(JSON.stringify(downloadObject))}` | ||
downloadElement.download = 'anotherpomodoro-settings.json' | ||
downloadElement.style.display = 'none' | ||
document.body.appendChild(downloadElement) | ||
downloadElement.click() | ||
document.body.removeChild(downloadElement) | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<template> | ||
<button class="px-6 py-3 transition border-2 border-red-400 rounded-full select-none dark:shadow-none hover:bg-red-400 active:bg-red-500 active:border-red-500 active:shadow-red-200 active:shadow-md" @click="openFileDialog"> | ||
<input ref="fileinput" accept=".json" type="file" hidden @change="importFile"> | ||
<span v-text="$i18n.t('settings.manage.buttons.load')" /> | ||
</button> | ||
</template> | ||
|
||
<script> | ||
import { useSettings } from '~/stores/settings' | ||
import { useTasklist } from '~/stores/tasklist' | ||
function filterImportedObject (store, objectToImport) { | ||
const storeKeys = Object.keys(store.$state) | ||
return Object | ||
.keys(objectToImport) | ||
.filter(key => storeKeys.includes(key)) | ||
.reduce((prev, key) => Object.assign(prev, { [key]: objectToImport[key] }), {}) | ||
} | ||
export default { | ||
methods: { | ||
openFileDialog () { | ||
this.$refs.fileinput.click() | ||
}, | ||
importFile () { | ||
// try to get file from the input field | ||
const file = this.$refs.fileinput.files[0] | ||
if (!file) { | ||
return | ||
} | ||
// read file | ||
const reader = new FileReader() | ||
reader.onload = (e) => { | ||
const fileContents = e.target.result | ||
// try to patch settings and task list with the imported values | ||
try { | ||
const importedValues = JSON.parse(fileContents) | ||
const settingsStore = useSettings() | ||
const tasklistStore = useTasklist() | ||
settingsStore.$patch(filterImportedObject(settingsStore, importedValues.settings)) | ||
tasklistStore.$patch(filterImportedObject(tasklistStore, importedValues.tasklist)) | ||
} catch (err) { | ||
console.warn(err) | ||
} | ||
} | ||
reader.readAsText(file) | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters