Add notes store

This commit is contained in:
Hubert Van De Walle 2020-06-09 16:44:57 +02:00
parent 882635cfc0
commit ca46e2645b
2 changed files with 59 additions and 20 deletions

View File

@ -6,7 +6,6 @@
:loading="loading" :loading="loading"
loading-text="Loading notes..." loading-text="Loading notes..."
class="elevation-1" class="elevation-1"
disable-sort
:footer-props="{ :footer-props="{
showFirstLastPage: false, showFirstLastPage: false,
prevIcon: mdiChevronLeft, prevIcon: mdiChevronLeft,
@ -19,7 +18,7 @@
Notes Notes
</h2> </h2>
<v-spacer></v-spacer> <v-spacer></v-spacer>
<v-btn to="/create" outlined dark> <v-btn outlined dark @click="test">
New Note New Note
</v-btn> </v-btn>
</v-toolbar> </v-toolbar>
@ -65,7 +64,7 @@
small small
color="secondary" color="secondary"
dark dark
@click="deleteItem(item)" @click="deleteNote(item)"
> >
{{ mdiDelete }} {{ mdiDelete }}
</v-icon> </v-icon>
@ -79,6 +78,7 @@
<script> <script>
import { format } from 'timeago.js' import { format } from 'timeago.js'
import { mapState, mapActions } from 'vuex'
import { import {
mdiEye, mdiEye,
mdiPencil, mdiPencil,
@ -89,10 +89,6 @@ import {
export default { export default {
name: 'Notes', name: 'Notes',
async asyncData(context) {
const notes = await context.$axios.$get('/notes')
return { notes, loading: false }
},
data: () => ({ data: () => ({
mdiEye, mdiEye,
mdiPencil, mdiPencil,
@ -107,21 +103,24 @@ export default {
{ text: 'Actions', value: 'actions' }, { text: 'Actions', value: 'actions' },
], ],
}), }),
methods: { computed: {
async loadNotes() { ...mapState('notes', { notes: (state) => state.notes }),
await this.$axios.$get('/notes').then((notes) => {
this.notes = notes
this.loading = false
})
}, },
mounted() {
this.$store.dispatch('notes/load').then(() => (this.loading = false))
},
methods: {
...mapActions({
deleteNote: 'notes/delete',
createNote: 'notes/create',
}),
editItem(item) {}, editItem(item) {},
async deleteItem(item) { test() {
try { this.createNote({
await this.$axios.delete(`/notes/${item.uuid}`) title: 'title',
} catch (_) { tags: [],
} finally { chapters: [],
await this.loadNotes() })
}
}, },
format, format,
}, },

View File

@ -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 = {}