Skip to content

Instantly share code, notes, and snippets.

@denisfl denisfl/store.js
Created Aug 3, 2018

Embed
What would you like to do?
Vue code exaple
import Vue from 'vue'
import Vuex from 'vuex'
import slice from 'lodash/slice'
import take from 'lodash/take'
import flattenDeep from 'lodash/flattenDeep'
import each from 'lodash/each'
Vue.use(Vuex)
const URL = '/teasers'
export default new Vuex.Store({
state: {
list: null,
item: null,
filterItems: [],
details: {
count: null,
item: {
article: {
title: null,
body: null,
cover: null,
},
fullArticle: false,
readMore: false
}
}
},
getters: {
loadingItems(state) {
return flattenDeep(state.filterItems)
},
getItemsForSidebar(state) {
return take(state.list, state.details.count)
},
getItemsForList(state) {
return slice(state.list, state.details.count)
},
getArticle(state) {
return state.details.item
},
getArticleId(state) {
return state.details.item.article.id
}
},
mutations: {
setList(state, data) {
let serialNumber = 0
state.list = each(data.markets, (value) => {
value.serial_number = serialNumber
serialNumber += 1
})
},
setItem(state, data) {
state.item = data
},
setLoadingSince(state) {
state.loadingPosition.start += state.count
state.loadingPosition.end += state.count
},
sliceItems(state) {
state.filterItems.push(slice(state.list, state.loadingPosition.start, state.loadingPosition.end))
},
setArticle(state, data) {
state.details.item = data
},
setSidebarItemsCount(state, data) {
state.details.count = data
},
showFullArticle(state) {
state.details.item.article.fullArticle = true
}
},
actions: {
fetchList({ commit }, data) {
return fetch(URL, {
method: 'POST',
mode: 'cors',
headers:{
'Access-Control-Allow-Origin':'*'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then((json) => {
commit("setList", json)
})
},
setFullArticle({ commit }, data) {
commit("setArticle", data)
},
updateSidebarItemsCount({ commit }, data) {
commit("setSidebarItemsCount", data)
},
sendViewedItems(data) {
return fetch('/record', {
method: 'POST',
body: JSON.stringify(data)
})
.catch((error) => {
console.log(error.message)
})
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.