Skip to content

Commit

Permalink
feat: save/load API key from/to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
juzraai committed Mar 14, 2021
1 parent 2a584de commit e312377
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
render: h => h(App),
beforeCreate() {
this.$store.commit('init')
}
}).$mount('#app')
21 changes: 20 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@ import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
const store = new Vuex.Store({
state: {
apiKey: null
},
mutations: {
init(state) {
if(localStorage.getItem('store')) {
const json = localStorage.getItem('store')
const obj = JSON.parse(json)
const newState = Object.assign(state, obj)
this.replaceState(newState)
}
},
setApiKey(state, apiKey) {
state.apiKey = apiKey
}
},
actions: {
},
modules: {
}
})

store.subscribe((mutation, state) => {
console.log('STATE MODIFIED', JSON.stringify(state))
localStorage.setItem('store', JSON.stringify(state))
})

export default store
16 changes: 16 additions & 0 deletions src/views/Home.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
<template>
<div>
<input
type="text"
v-model="apiKeyInput"
>
<button @click="setApiKey(apiKeyInput)">Set API key</button>
</div>
</template>

<script>
//import HelloWorld from '@/components/HelloWorld.vue'
import { mapMutations } from "vuex";
export default {
name: "Home",
components: {
//HelloWorld
},
data() {
return {
apiKeyInput: this.$store.state.apiKey,
};
},
methods: {
...mapMutations(["setApiKey"]),
},
};
</script>

0 comments on commit e312377

Please sign in to comment.