index.js 1.5 KB

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