55 lines
1.3 KiB
JavaScript

export const state = () => ({
notes: [],
isInitialized: false,
})
export const mutations = {
setInitialized(state) {
state.isInitialized = true
},
set(state, notes) {
state.notes = notes
},
add(state, note) {
state.notes.unshift(note)
},
delete(state, uuid) {
state.notes = state.notes.filter((e) => e.uuid !== uuid)
},
// used when logging out
clear(state) {
state.notes = []
state.initialized = false
},
}
export const actions = {
load({ commit }) {
return this.$axios.get('/notes').then(({ data }) => {
commit('set', data)
commit('setInitialized')
})
},
create({ commit }, note) {
this.$axios.post('/notes', note).then(({ data }) =>
commit('add', {
uuid: data.uuid,
title: data.title,
updatedAt: data.updatedAt,
tags: data.tags,
})
)
},
delete({ commit }, note) {
const result = this.$axios.delete(`/notes/${note.uuid}`)
commit('delete', note.uuid)
result.catch(() => commit('add', note))
},
}
export const getters = {
isEmpty(state) {
return state.isInitialized && state.notes.length === 0
},
}