Created
January 18, 2018 17:50
-
-
Save Krasnov8953/4127de55219c775fd5a934ac3ce4567e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 find from 'lodash/find'; | |
import findIndex from 'lodash/findIndex'; | |
import moment from 'moment'; | |
import Vue from 'vue'; | |
import Vuex from 'vuex'; | |
Vue.use(Vuex); | |
const state = { | |
isAuthenticated: false, | |
authErrorMessage: '', | |
count: 0, | |
user: {}, | |
news: [], | |
collections: [], | |
notice: '', | |
rewards: [], | |
reward: {} | |
}; | |
const getters = { | |
getIsAuthenticated: state => { | |
return state.isAuthenticated; | |
} | |
}; | |
const actions = { | |
getProfileData({ commit }){ | |
Vue.http.get('me'). | |
then( response => { | |
commit('setUserData', response.body); | |
}); | |
}, | |
getNewsList({ commit, state }){ | |
Vue.http.get('news'). | |
then( response => { | |
commit('setNews', response.body.news); | |
setTimeout(function(){ | |
state.notice = '' | |
}, 2000); | |
}); | |
}, | |
getCollections({ commit, state }){ | |
Vue.http.get('admin_collections'). | |
then( response => { | |
commit('setCollections', response.body.collections) | |
commit('setFilmsToColl', response.body.collections) | |
setTimeout(function(){ | |
state.notice = '' | |
}, 2000); | |
}) | |
}, | |
deleteCollection({ commit }, {id, index}){ | |
var i = index | |
Vue.http.delete('admin_collections/' + id). | |
then( function(response, index = i) { | |
commit('deleteCollections', index) | |
}) | |
}, | |
fetchRewards({commit}, type = 'view'){ | |
Vue.http.get('awards/' + type). | |
then( response => { | |
commit('setRewards', response.body.awards) | |
}) | |
setTimeout(function(){ | |
state.notice = '' | |
}, 2000); | |
}, | |
deleteReward({commit}, {type, id, index}){ | |
Vue.http.delete('awards/' + type + '/' + id). | |
then( function(response, i = index){ | |
commit('deleteReward', i) | |
}) | |
} | |
}; | |
const mutations = { | |
setIsAuthenticated(state, isAuthenticated) { | |
state.isAuthenticated = isAuthenticated | |
state.authErrorMessage = '' | |
}, | |
setUserData(state, UserData) { | |
state.user = UserData | |
}, | |
setNews(state, News){ | |
state.news = News | |
}, | |
setCollections(state, Collections){ | |
state.collections = Collections.reverse() | |
}, | |
deleteCollections(state, index){ | |
state.collections.splice(index, 1) | |
}, | |
setFilmsToColl(state, Collections){ | |
for (let i = 0; i < Collections.length; i++) { | |
Vue.http.get('admin_collections/' + Collections[i].id ) | |
.then( function(response) { | |
let films = response.body.films.reverse() | |
Vue.set(state.collections[i], 'films', films) | |
Vue.set(state.collections[i], 'active', false) | |
}) | |
} | |
}, | |
setRewards(state, Rewards){ | |
state.rewards = Rewards | |
}, | |
deleteReward(state, index){ | |
state.rewards.splice(index, 1) | |
} | |
}; | |
export default new Vuex.Store({ | |
state, | |
getters, | |
actions, | |
mutations | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment