Skip to content

Instantly share code, notes, and snippets.

@louisameline
Last active November 20, 2019 21:54
Show Gist options
  • Select an option

  • Save louisameline/ac6ebf402eca4848f5f03f6ee6e0b920 to your computer and use it in GitHub Desktop.

Select an option

Save louisameline/ac6ebf402eca4848f5f03f6ee6e0b920 to your computer and use it in GitHub Desktop.
patch ({state, getters, commit, dispatch}, doc) {
const store = this
let response
// check that the payload exists
if (doc) {
const id = (getters.collectionMode) ? getId(doc) : getters.docModeId,
value = (getters.collectionMode) ? getValueFromPayloadPiece(doc) : doc
// if we have a doc id
if (id || !getters.collectionMode) {
// check userId
dispatch('setUserId')
// add id to value
if (!value.id) {
value.id = id
}
// check for a hook before local change
if (state._conf.sync.patchHook) {
state._conf.sync.patchHook(v => dispatch('_patchFinish', v), value, store)
}
else {
dispatch('_patchFinish', value)
}
response = id
}
else {
response = logError('patch-missing-id')
}
}
else {
response = logError('missing-payload')
}
return response
},
// define the store update
_patchFinish ({state, getters, commit, dispatch}, _val) {
commit('PATCH_DOC', _val)
const v = copy(_val),
// the firestore update
firestoreUpdateFn = v => dispatch('patchDoc', {id, doc: copy(v)})
let promise
// check for a hook after local change before sync
if (state._conf.sync.patchHookBeforeSync) {
promise = state._conf.sync.patchHookBeforeSync(firestoreUpdateFn, copy(_val), store)
}
else {
promise = firestoreUpdateFn(v)
}
return promise
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment