Last active
August 15, 2018 09:23
-
-
Save duwaljyoti/8e557c11248bfb9b681e20dfab95abd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue'; | |
import Vuex from 'vuex'; | |
import axios from 'axios'; | |
import notes from '../api'; | |
Vue.use(Vuex); | |
const notesStore = new Vuex.Store({ | |
state: { | |
notes: [], | |
favouriteNotes: [] | |
}, | |
mutations: { | |
FETCH(state, notes) { | |
state.notes = notes; | |
}, | |
FETCH_FAVOURITE(state, favouriteNotes) { | |
state.favouriteNotes = favouriteNotes; | |
} | |
}, | |
actions: { | |
fetch({ commit }) { | |
return axios.get(notes) | |
.then(response => commit('FETCH', response.data)) | |
.catch(); | |
}, | |
deleteNote({}, id) { | |
axios.delete(`${notes}/${id}`) | |
.then(() => this.dispatch('fetch')) | |
.catch(); | |
}, | |
edit({}, note) { | |
axios.put(`${notes}/${note.id}`, { | |
title: note.title | |
}) | |
.then(() => this.dispatch('fetch')); | |
}, | |
toggleFavourite({}, id) { | |
axios.put(`${notes}/${id}/toggleFavourite`, { | |
is_favourite: true | |
}) | |
.then(() => this.dispatch('fetch')) | |
}, | |
fetchFavourite({commit}) { | |
return axios.get(`${notes}?type=favourite`) | |
.then(response => commit('FETCH_FAVOURITE', response.data)) | |
.catch(); | |
}, | |
add({}, title) { | |
axios.post(`${notes}`, { | |
'title': title, | |
'is_favourite': false, | |
}); | |
} | |
} | |
}); | |
export default notesStore; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment