NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

episode.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import api from '../../utils/api'
  2. const state = {
  3. all: {},
  4. loaded: false,
  5. episode: null,
  6. }
  7. const getters = {
  8. allEpisodes: state => state.all,
  9. allEpisodesLoaded: state => state.loaded,
  10. episode: state => id => {
  11. let field = typeof id === 'number' ? 'id' : 'slug'
  12. let episode = state.all.filter(episode => episode[field] === id)
  13. return (episode[0]) ? episode[0] : false
  14. },
  15. episodeContent: state => id => {
  16. let field = typeof id === 'number' ? 'id' : 'slug'
  17. let episode = state.all.filter(episode => episode[field] === id)
  18. return (episode[0]) ? episode[0].content.rendered : false
  19. },
  20. someEpisodes: state => limit => {
  21. if (state.all.length < 1) return false
  22. let all = [...state.all]
  23. return all.splice(0, Math.min(limit, state.all.length))
  24. },
  25. }
  26. const actions = {
  27. getAllEpisodes({ commit }, sortType) {
  28. api.getByType('episodes', sortType, episodes => {
  29. commit('STORE_FETCHED_EPISODES', { episodes })
  30. commit('EPISODES_LOADED', true)
  31. })
  32. }
  33. }
  34. const mutations = {
  35. STORE_FETCHED_EPISODES(state, { episodes }) { state.all = episodes },
  36. EPISODES_LOADED(state, val) { state.loaded = val },
  37. }
  38. export default { state, getters, actions, mutations }