posts.js 729 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import datarest from '../../api/datarest'
  2. // initial state
  3. const state = {
  4. all: [],
  5. opened: null
  6. }
  7. // getters
  8. const getters = {}
  9. // actions
  10. const actions = {
  11. getAllPosts ({ commit }) {
  12. datarest.getPosts(posts => {
  13. commit('setPosts', posts)
  14. })
  15. },
  16. openPost ({ commit }, post) {
  17. datarest.getPost(post.id, post => {
  18. commit('setOpened', post)
  19. })
  20. },
  21. closePost ({ commit }) {
  22. commit('clearOpened')
  23. }
  24. }
  25. // mutations
  26. const mutations = {
  27. setPosts (state, posts) {
  28. state.all = posts
  29. },
  30. setOpened (state, post) {
  31. state.opened = post
  32. },
  33. clearOpened (state) {
  34. state.opened = null
  35. }
  36. }
  37. export default {
  38. namespaced: true,
  39. state,
  40. getters,
  41. actions,
  42. mutations
  43. }