index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import Vuex from 'vuex'
  2. import md5 from 'md5'
  3. import slugify from 'slugify'
  4. import db from '~/plugins/firestore'
  5. import { saveUserData, clearUserData } from '~/utils'
  6. const createStore = () => {
  7. return new Vuex.Store({
  8. state: {
  9. headlines: [],
  10. headline: null,
  11. feed: [],
  12. loading: false,
  13. category: '',
  14. token: null,
  15. country: 'us',
  16. user: null
  17. },
  18. mutations: {
  19. setHeadLines (state, headlines) {
  20. state.headlines = headlines
  21. },
  22. setHeadline (state, headline) {
  23. state.headline = headline
  24. },
  25. setLoading (state, loading) {
  26. state.loading = loading
  27. },
  28. setCategory (state, category) {
  29. state.category = category
  30. },
  31. setCountry (state, country) {
  32. state.country = country
  33. },
  34. setToken (state, token) {
  35. state.token = token
  36. },
  37. setUser (state, user) {
  38. state.user = user
  39. },
  40. setFeed (state, headlines) {
  41. state.feed = headlines
  42. },
  43. clearToken: state => (state.token = null),
  44. clearUser: state => (state.user = null),
  45. clearFeed: state => (state.feed = [])
  46. },
  47. actions: {
  48. async loadHeadLines ({ commit }, apiUrl) {
  49. commit('setLoading', true)
  50. const { articles } = await this.$axios.$get(apiUrl)
  51. const headlines = articles.map((article) => {
  52. const slug = slugify(article.title, {
  53. replacement: '-',
  54. remove: /[^a-zA-Z0-9 -]/g
  55. })
  56. const headline = { ...article, slug }
  57. return headline
  58. })
  59. commit('setLoading', false)
  60. commit('setHeadLines', headlines)
  61. },
  62. async authenticateUser ({ commit }, userPayload) {
  63. try {
  64. commit('setLoading', true)
  65. const autuUserData = await this.$axios.$post(
  66. `/${userPayload.action}/`,
  67. {
  68. email: userPayload.email,
  69. password: userPayload.password,
  70. resturnSecureToken: userPayload.resturnSecureToken
  71. }
  72. )
  73. let user
  74. if (userPayload.action === 'register') {
  75. const avatar = `http://gravatar.com/avatar/${md5(autuUserData.email)}?d=identicon`
  76. user = { email: autuUserData.email, avatar }
  77. await db
  78. .collection('users')
  79. .doc(userPayload.email)
  80. .set(user)
  81. } else {
  82. const loginRef = db.collection('users').doc(userPayload.email)
  83. const loggedInUser = await loginRef.get()
  84. user = loggedInUser.data()
  85. }
  86. // console.log(autuUserData)
  87. commit('setUser', user)
  88. commit('setToken', autuUserData.idToken)
  89. commit('setLoading', false)
  90. saveUserData(autuUserData, user)
  91. } catch (error) {
  92. // console.log(error)
  93. }
  94. },
  95. async loadHeadline ({ commit }, headlineSlug) {
  96. const headlineRef = db.collection('headlines').doc(headlineSlug)
  97. await headlineRef.get().then((doc) => {
  98. if (doc.exists) {
  99. const headline = doc.data()
  100. this.commit('setHeadline', headline)
  101. }
  102. })
  103. },
  104. async saveHeadline (context, headline) {
  105. const headlineRef = db.collection('headlines').doc(headline.slug)
  106. // eslint-disable-next-line no-unused-vars
  107. let headlineId
  108. await headlineRef.get().then((doc) => {
  109. if (doc.exists) {
  110. headlineId = doc.id
  111. }
  112. })
  113. if (!headlineId) {
  114. await headlineRef.set(headline)
  115. }
  116. },
  117. async removeHeadlineFromFeed ({ state }, headline) {
  118. const headlineRef = db.collection(`users/${state.user.email}/feed`).doc(headline.title)
  119. await headlineRef.delete()
  120. },
  121. async addHeadlineToFeed ({ state }, headline) {
  122. const feedRef = db.collection(`users/${state.user.email}/feed`).doc(headline.title)
  123. await feedRef.set(headline)
  124. },
  125. async loadUserFeed ({ state, commit }) {
  126. if (state.user) {
  127. const feedRef = db.collection(`users/${state.user.email}/feed`)
  128. await feedRef.onSnapshot((querySnapshot) => {
  129. // eslint-disable-next-line prefer-const
  130. let headlines = []
  131. querySnapshot.forEach((doc) => {
  132. headlines.push(doc.data())
  133. commit('setFeed', headlines)
  134. })
  135. if (querySnapshot.empty) {
  136. headlines = []
  137. commit('setFeed', headlines)
  138. }
  139. })
  140. }
  141. },
  142. setLogoutTimer ({ dispatch }, interval) {
  143. setTimeout(() => dispatch('logoutUser'), interval)
  144. },
  145. logoutUser ({ commit }) {
  146. commit('clearToken')
  147. commit('clearUser')
  148. commit('clearFeed')
  149. clearUserData()
  150. }
  151. },
  152. getters: {
  153. headlines: state => state.headlines,
  154. headline: state => state.headline,
  155. feed: state => state.feed,
  156. loading: state => state.loading,
  157. category: state => state.category,
  158. country: state => state.country,
  159. isAuthenticated: state => !!state.token,
  160. user: state => state.user
  161. }
  162. })
  163. }
  164. export default createStore