index.js 6.4 KB

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