Commit 44948440 authored by Adam Procter's avatar Adam Procter 💩

vuex and pouch data /sync to couch

parent e98b5f86
<template>
<div>
<p>You like {{ $store.getters.currentflavour }}</p>
<p>Your current fav flavour is: {{ $store.getters.currentflavour }}</p>
</div>
</template>
......
import Vue from 'vue'
import Vuex from 'vuex'
import PouchDB from 'pouchdb'
Vue.use(Vuex)
export default new Vuex.Store({
state: { flavour: null },
var pouchdb = new PouchDB('icecream')
var remote = 'https://nn.adamprocter.co.uk/icecream'
const store = new Vuex.Store({
state: {
flavour: null
},
mutations: {
GET_DB(state) {
pouchdb
.get('myfav')
.then(function(doc) {
state.flavour = doc.flavour
})
.catch(function(err) {
console.log(err)
})
},
CHANGE_STATE(state, flavour) {
state.flavour = flavour
pouchdb
.get('myfav')
.then(function(doc) {
return pouchdb.put({
_id: 'myfav',
_rev: doc._rev,
flavour: flavour
})
})
.then(function(response) {
// handle response
if (response.ok == true) {
//if all good could do something
}
})
.catch(function(err) {})
},
CLEAR_STATE() {
this.replaceState({ flavour: null })
store.replaceState({ flavour: null })
}
},
actions: {
SYNC_DB() {
pouchdb.sync(remote, { live: true, retry: true })
},
INCOMING_CHANGE(_state, event) {
this.commit('CHANGE_STATE', event)
var id = 'myfav'
store.commit('CHANGE_STATE', event)
// send change also to the pouch
pouchdb
.get('myfav')
.then(function(doc) {
return pouchdb.put({
_id: 'myfav',
_rev: doc._rev,
flavour: event
})
})
.then(function(response) {
// handle response
if (response.ok == true) {
//if all good could do something
}
})
.catch(function(err) {
if (err.status == 404) {
pouchdb.put({ _id: id, flavour: event })
}
})
}
},
getters: {
//flavour: state => state.flavour
currentflavour: state => {
return state.flavour
}
}
})
export default store
store.dispatch('SYNC_DB')
store.commit('GET_DB')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment