index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Vuex from 'vuex'
  2. import md5 from 'md5'
  3. import db from '~/plugins/firestore'
  4. import { saveUserData, clearUserData } from '~/utils'
  5. const createStore = () => {
  6. return new Vuex.Store({
  7. state: {
  8. headlines: [],
  9. feed: [],
  10. loading: false,
  11. category: '',
  12. token: null,
  13. country: 'us',
  14. user: null
  15. },
  16. mutations: {
  17. setHeadLines (state, headlines) {
  18. state.headlines = headlines
  19. },
  20. setLoading (state, loading) {
  21. state.loading = loading
  22. },
  23. setCategory (state, category) {
  24. state.category = category
  25. },
  26. setCountry (state, country) {
  27. state.country = country
  28. },
  29. setToken (state, token) {
  30. state.token = token
  31. },
  32. setUser (state, user) {
  33. state.user = user
  34. },
  35. setFeed (state, headlines) {
  36. state.feed = headlines
  37. },
  38. clearToken: state => (state.token = null),
  39. clearUser: state => (state.user = null)
  40. },
  41. actions: {
  42. async loadHeadLines ({ commit }, apiUrl) {
  43. commit('setLoading', true)
  44. const { articles } = await this.$axios.$get(apiUrl)
  45. commit('setLoading', false)
  46. commit('setHeadLines', articles)
  47. },
  48. async authenticateUser ({ commit }, userPayload) {
  49. try {
  50. commit('setLoading', true)
  51. const autuUserData = await this.$axios.$post(
  52. `/${userPayload.action}/`,
  53. {
  54. email: userPayload.email,
  55. password: userPayload.password,
  56. resturnSecureToken: userPayload.resturnSecureToken
  57. }
  58. )
  59. let user
  60. if (userPayload.action === 'register') {
  61. const avatar = `http://gravatar.com/avatar/${md5(autuUserData.email)}?d=identicon`
  62. user = { email: autuUserData.email, avatar }
  63. await db
  64. .collection('users')
  65. .doc(userPayload.email)
  66. .set(user)
  67. } else {
  68. const loginRef = db.collection('users').doc(userPayload.email)
  69. const loggedInUser = await loginRef.get()
  70. user = loggedInUser.data()
  71. }
  72. // console.log(autuUserData)
  73. commit('setUser', user)
  74. commit('setToken', autuUserData.idToken)
  75. commit('setLoading', false)
  76. saveUserData(autuUserData, user)
  77. } catch (error) {
  78. // console.log(error)
  79. }
  80. },
  81. async addHeadlineToFeed ({ state }, headline) {
  82. const feedRef = db.collection(`users/${state.user.email}/feed`).doc(headline.title)
  83. await feedRef.set(headline)
  84. },
  85. async loadUserFeed ({ state, commit }) {
  86. const feedRef = db.collection(`users/${state.user.email}/feed`)
  87. await feedRef.get().then((querySnapshot) => {
  88. const headlines = []
  89. querySnapshot.forEach((doc) => {
  90. headlines.push(doc.data())
  91. })
  92. commit('setFeed', headlines)
  93. })
  94. },
  95. setLogoutTimer ({ dispatch }, interval) {
  96. setTimeout(() => dispatch('logoutUser'), interval)
  97. },
  98. logoutUser ({ commit }) {
  99. commit('clearToken')
  100. commit('clearUser')
  101. clearUserData()
  102. }
  103. },
  104. getters: {
  105. headlines: state => state.headlines,
  106. feed: state => state.feed,
  107. loading: state => state.loading,
  108. category: state => state.category,
  109. country: state => state.country,
  110. isAuthenticated: state => !!state.token,
  111. user: state => state.user
  112. }
  113. })
  114. }
  115. export default createStore