Skip to content

Commit

Permalink
Remember last chosen tab on the settings UI.
Browse files Browse the repository at this point in the history
This commit adds a UI setting that was accidentally lost from an
earlier PR.

It introduces `$utils.setPref()|getPref()` to save arbitrary key/value
preferences in a JSON blob under the app's namespace in localStorage.
  • Loading branch information
knadh committed Feb 6, 2022
1 parent 0f6a037 commit 93366f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 20 additions & 0 deletions frontend/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

const reEmail = /(.+?)@(.+?)/ig;
const prefKey = 'listmonk_pref';

const htmlEntities = {
'&': '&',
Expand Down Expand Up @@ -188,4 +189,23 @@ export default class Utils {

return obj;
};

getPref = (key) => {
if (localStorage.getItem(prefKey) === null) {
return null;
}

const p = JSON.parse(localStorage.getItem(prefKey));
return key in p ? p[key] : null;
};

setPref = (key, val) => {
let p = {};
if (localStorage.getItem(prefKey) !== null) {
p = JSON.parse(localStorage.getItem(prefKey));
}

p[key] = val;
localStorage.setItem(prefKey, JSON.stringify(p));
}
}
11 changes: 10 additions & 1 deletion frontend/src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<hr />

<section class="wrap">
<b-tabs type="is-boxed" :animated="false">
<b-tabs type="is-boxed" :animated="false" v-model="tab">
<b-tab-item :label="$t('settings.general.name')" label-position="on-border">
<general-settings :form="form" :key="key" />
</b-tab-item><!-- general -->
Expand Down Expand Up @@ -98,6 +98,7 @@ export default Vue.extend({
// form is compared to detect changes.
formCopy: '',
form: {},
tab: 0,
};
},
Expand Down Expand Up @@ -237,7 +238,15 @@ export default Vue.extend({
},
mounted() {
this.tab = this.$utils.getPref('settings.tab') || 0;
this.getSettings();
},
watch: {
// Capture contentType and body passed from the parent as props.
tab(t) {
this.$utils.setPref('settings.tab', t);
},
},
});
</script>

0 comments on commit 93366f4

Please sign in to comment.