index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 sendComment ({ commit, state }, comment) {
  105. const commentRef = db.collection(`headlines/${state.headline.slug}/comments`)
  106. commit('setLoading', true)
  107. await commentRef.doc(comment.id).set(comment)
  108. await commentRef.get().then((querySnapshot) => {
  109. // eslint-disable-next-line prefer-const
  110. let comments = []
  111. querySnapshot.forEach((doc) => {
  112. comments.push(doc.data())
  113. const updateHeadline = { ...state.headline, comments }
  114. commit('setHeadline', updateHeadline)
  115. })
  116. })
  117. commit('setLoading', false)
  118. },
  119. async saveHeadline (context, headline) {
  120. const headlineRef = db.collection('headlines').doc(headline.slug)
  121. // eslint-disable-next-line no-unused-vars
  122. let headlineId
  123. await headlineRef.get().then((doc) => {
  124. if (doc.exists) {
  125. headlineId = doc.id
  126. }
  127. })
  128. if (!headlineId) {
  129. await headlineRef.set(headline)
  130. }
  131. },
  132. async removeHeadlineFromFeed ({ state }, headline) {
  133. const headlineRef = db.collection(`users/${state.user.email}/feed`).doc(headline.title)
  134. await headlineRef.delete()
  135. },
  136. async addHeadlineToFeed ({ state }, headline) {
  137. const feedRef = db.collection(`users/${state.user.email}/feed`).doc(headline.title)
  138. await feedRef.set(headline)
  139. },
  140. async loadUserFeed ({ state, commit }) {
  141. if (state.user) {
  142. const feedRef = db.collection(`users/${state.user.email}/feed`)
  143. await feedRef.onSnapshot((querySnapshot) => {
  144. // eslint-disable-next-line prefer-const
  145. let headlines = []
  146. querySnapshot.forEach((doc) => {
  147. headlines.push(doc.data())
  148. commit('setFeed', headlines)
  149. })
  150. if (querySnapshot.empty) {
  151. headlines = []
  152. commit('setFeed', headlines)
  153. }
  154. })
  155. }
  156. },
  157. setLogoutTimer ({ dispatch }, interval) {
  158. setTimeout(() => dispatch('logoutUser'), interval)
  159. },
  160. logoutUser ({ commit }) {
  161. commit('clearToken')
  162. commit('clearUser')
  163. commit('clearFeed')
  164. clearUserData()
  165. }
  166. },
  167. getters: {
  168. headlines: state => state.headlines,
  169. headline: state => state.headline,
  170. feed: state => state.feed,
  171. loading: state => state.loading,
  172. category: state => state.category,
  173. country: state => state.country,
  174. isAuthenticated: state => !!state.token,
  175. user: state => state.user
  176. }
  177. })
  178. }
  179. export default createStore