Skip to content

Commit

Permalink
save settings to localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani committed Jul 8, 2024
1 parent be40d22 commit 9ca7d93
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import SettingsMenu from './components/SettingsMenu.vue'
import type { Activity } from './types.ts'
import { defineComponent } from 'vue'
import { useStore } from '@/store'
export default defineComponent({
data() {
return {
Expand All @@ -18,6 +20,9 @@ export default defineComponent({
showSettings: false
}
},
beforeMount() {
useStore().commit('init')
},
mounted() {
let a = localStorage.getItem('activities')
if (a != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/SettingsMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineComponent({
timeAsButton: false
}
},
onMount() {
created() {
this.timeAsButton = this.settings.state.timeAsButton
},
emits: ['close'],
Expand Down Expand Up @@ -41,7 +41,7 @@ export default defineComponent({
<label class="inline-flex items-center cursor-pointer">
<input
type="checkbox"
:value="timeAsButton"
:checked="timeAsButton"
@click="toggleSettings()"
class="sr-only peer"
/>
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ import App from './App.vue'
import './index.css'
import { storeSettings, key } from '@/store'

// Save settings to localstorage every time there is a mutation
storeSettings.subscribe((_, state) => {
localStorage.setItem('timeAsButton', JSON.stringify(state.timeAsButton))
})

createApp(App).use(storeSettings, key).mount('#app')
9 changes: 8 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ export const key: InjectionKey<Store<State>> = Symbol()

export const storeSettings = createStore<State>({
state: {
timeAsButton: false
timeAsButton: true
},
mutations: {
init(state) {
if (localStorage.getItem('timeAsButton')) {
state.timeAsButton = JSON.parse(localStorage.timeAsButton)
} else {
state.timeAsButton = true
}
},
timeAsButtonToggle(state) {
state.timeAsButton = !state.timeAsButton
}
Expand Down

0 comments on commit 9ca7d93

Please sign in to comment.