Add notes store
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
export const state = () => ({
|
||||
notes: [],
|
||||
})
|
||||
|
||||
export const mutations = {
|
||||
set(state, notes) {
|
||||
state.notes = notes
|
||||
},
|
||||
add(state, note) {
|
||||
state.notes.push(note)
|
||||
},
|
||||
delete(state, uuid) {
|
||||
state.notes = state.notes.filter((e) => e.uuid !== uuid)
|
||||
},
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
async load({ commit }) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000))
|
||||
|
||||
this.$axios.get('/notes').then(({ data }) => commit('set', data))
|
||||
},
|
||||
// FIXME: make the api send the new date back
|
||||
create({ commit }, note) {
|
||||
this.$axios.post('/notes', note).then(({ data }) =>
|
||||
commit('add', {
|
||||
updatedAt: new Date().toISOString(),
|
||||
uuid: data.uuid,
|
||||
...note,
|
||||
})
|
||||
)
|
||||
},
|
||||
delete({ commit }, note) {
|
||||
const result = this.$axios.delete(`/notes/${note.uuid}`)
|
||||
commit('delete', note.uuid)
|
||||
result.catch(() => commit('add', note))
|
||||
},
|
||||
}
|
||||
|
||||
export const getters = {}
|
||||
|
||||
Reference in New Issue
Block a user