Explorar o código

Move loadInitialPosts to vuex action

sbkwgh %!s(int64=8) %!d(string=hai) anos
pai
achega
6c87421802
Modificáronse 2 ficheiros con 23 adicións e 19 borrados
  1. 2 19
      src/components/routes/Thread.vue
  2. 21 0
      src/store/modules/thread.js

+ 2 - 19
src/components/routes/Thread.vue

@@ -125,25 +125,8 @@
 				this.$store.dispatch('loadPostsAsync', { vue, previous: true });
 			},
 			loadInitialPosts () {
-				let postId = this.$route.params.post_id
-				let apiURL = '/api/v1/thread/' + this.$route.params.id
-				if(postId) {
-					apiURL += '?postId=' + postId
-				}
-
-				this.axios
-					.get(apiURL)
-					.then(res => {
-						this.$store.commit('setThread', res.data)
-						this.$store.commit('setNextURL', res.data.meta.nextURL)
-						this.$store.commit('setPreviousURL', res.data.meta.previousURL)
-						this.$store.commit('setPosts', res.data.Posts)
-
-						if(postId) {
-							this.highlightPost(+postId)
-						}
-					}).catch(AjaxErrorHandler(this.$store))
-
+				let vue = this
+				this.$store.dispatch('loadInitialPostsAsync', vue)
 			},	
 			goToPost (id) {
 				this.$router.push({ params: { post_id: id } })

+ 21 - 0
src/store/modules/thread.js

@@ -51,6 +51,27 @@ const actions = {
 			})
 			.catch(AjaxErrorHandler(vue.$store))
 	},
+	loadInitialPostsAsync ({ state, commit, rootState }, vue) {
+		let postId = vue.$route.params.post_id
+		let apiURL = '/api/v1/thread/' + vue.$route.params.id
+
+		if(postId) {
+			apiURL += '?postId=' + postId
+		}
+
+		vue.axios
+			.get(apiURL)
+			.then(res => {
+				commit('setThread', res.data)
+				commit('setNextURL', res.data.meta.nextURL)
+				commit('setPreviousURL', res.data.meta.previousURL)
+				commit('setPosts', res.data.Posts)
+
+				if(postId) {
+					vue.highlightPost(+postId)
+				}
+			}).catch(AjaxErrorHandler(vue.$store))
+	},
 	loadPostsAsync ({ state, commit, rootState }, { vue, previous }) {
 		let URL