index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Vuex from 'vuex'
  2. const createStore = () => {
  3. return new Vuex.Store({
  4. state: {
  5. headlines: [],
  6. loading: false,
  7. category: '',
  8. country: 'us'
  9. },
  10. mutations: {
  11. setHeadLines (state, headlines) {
  12. state.headlines = headlines
  13. },
  14. setLoading (state, loading) {
  15. state.loading = loading
  16. },
  17. setCategory (state, category) {
  18. state.category = category
  19. },
  20. setCountry (state, country) {
  21. state.country = country
  22. }
  23. },
  24. actions: {
  25. async loadHeadLines ({ commit }, apiUrl) {
  26. commit('setLoading', true)
  27. const { articles } = await this.$axios.$get(apiUrl)
  28. commit('setLoading', false)
  29. commit('setHeadLines', articles)
  30. },
  31. async authenticateUser ({ commit }, userPayload) {
  32. try {
  33. commit('setLoading', true)
  34. const autuUserData = await this.$axios.$post(
  35. '/register/',
  36. userPayload
  37. )
  38. console.log(autuUserData)
  39. commit('setLoading', false)
  40. } catch (error) {
  41. console.log(error)
  42. }
  43. }
  44. },
  45. getters: {
  46. headlines: state => state.headlines,
  47. loading: state => state.loading,
  48. category: state => state.category,
  49. country: state => state.country
  50. }
  51. })
  52. }
  53. export default createStore