index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. clearFeed: state => (state.feed = [])
  41. },
  42. actions: {
  43. async loadHeadLines ({ commit }, apiUrl) {
  44. commit('setLoading', true)
  45. const { articles } = await this.$axios.$get(apiUrl)
  46. commit('setLoading', false)
  47. commit('setHeadLines', articles)
  48. },
  49. async authenticateUser ({ commit }, userPayload) {
  50. try {
  51. commit('setLoading', true)
  52. const autuUserData = await this.$axios.$post(
  53. `/${userPayload.action}/`,
  54. {
  55. email: userPayload.email,
  56. password: userPayload.password,
  57. resturnSecureToken: userPayload.resturnSecureToken
  58. }
  59. )
  60. let user
  61. if (userPayload.action === 'register') {
  62. const avatar = `http://gravatar.com/avatar/${md5(autuUserData.email)}?d=identicon`
  63. user = { email: autuUserData.email, avatar }
  64. await db
  65. .collection('users')
  66. .doc(userPayload.email)
  67. .set(user)
  68. } else {
  69. const loginRef = db.collection('users').doc(userPayload.email)
  70. const loggedInUser = await loginRef.get()
  71. user = loggedInUser.data()
  72. }
  73. // console.log(autuUserData)
  74. commit('setUser', user)
  75. commit('setToken', autuUserData.idToken)
  76. commit('setLoading', false)
  77. saveUserData(autuUserData, user)
  78. } catch (error) {
  79. // console.log(error)
  80. }
  81. },
  82. async addHeadlineToFeed ({ state }, headline) {
  83. const feedRef = db.collection(`users/${state.user.email}/feed`).doc(headline.title)
  84. await feedRef.set(headline)
  85. },
  86. async loadUserFeed ({ state, commit }) {
  87. if (state.user) {
  88. const feedRef = db.collection(`users/${state.user.email}/feed`)
  89. // feedRef.onSnapshot now work
  90. await feedRef.get().then((querySnapshot) => {
  91. // eslint-disable-next-line prefer-const
  92. let headlines = []
  93. querySnapshot.forEach((doc) => {
  94. headlines.push(doc.data())
  95. commit('setFeed', headlines)
  96. })
  97. })
  98. }
  99. },
  100. setLogoutTimer ({ dispatch }, interval) {
  101. setTimeout(() => dispatch('logoutUser'), interval)
  102. },
  103. logoutUser ({ commit }) {
  104. commit('clearToken')
  105. commit('clearUser')
  106. commit('clearFeed')
  107. clearUserData()
  108. }
  109. },
  110. getters: {
  111. headlines: state => state.headlines,
  112. feed: state => state.feed,
  113. loading: state => state.loading,
  114. category: state => state.category,
  115. country: state => state.country,
  116. isAuthenticated: state => !!state.token,
  117. user: state => state.user
  118. }
  119. })
  120. }
  121. export default createStore