Skip to content

Instantly share code, notes, and snippets.

@mrcat323
Forked from duwaljyoti/noteStore.js
Created August 15, 2018 09:23
Show Gist options
  • Save mrcat323/6c8f7292f59b87c4792485c1f56d48b9 to your computer and use it in GitHub Desktop.
Save mrcat323/6c8f7292f59b87c4792485c1f56d48b9 to your computer and use it in GitHub Desktop.
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