瀏覽代碼

Use api to remove posts on backend

sbkwgh 8 年之前
父節點
當前提交
55b5b4017c
共有 2 個文件被更改,包括 28 次插入2 次删除
  1. 9 1
      src/components/routes/Thread.vue
  2. 19 1
      src/store/modules/thread.js

+ 9 - 1
src/components/routes/Thread.vue

@@ -1,7 +1,12 @@
 <template>
 	<div class='route_container'>
 		<div class='thread_side_bar'>
-			<button class='button' :class='{ "button--disabled" : !$store.state.thread.selectedPosts.length }' v-if='showSelect'>
+			<button
+				class='button'
+				:class='{ "button--disabled" : !$store.state.thread.selectedPosts.length }'
+				@click='removePosts'
+				v-if='showSelect'
+			>
 				Delete selected posts ({{$store.state.thread.selectedPosts.length}})
 			</button>
 			<menu-button
@@ -145,6 +150,9 @@
 			editorState () { return this.$store.state.thread.editor.show }
 		},
 		methods: {
+			removePosts () {
+				this.$store.dispatch("removePostsAsync", this)
+			},
 			setThreadLockedState () {
 				this.$store.dispatch('setThreadLockedState', this)
 			},

+ 19 - 1
src/store/modules/thread.js

@@ -21,7 +21,8 @@ const state = {
 	nextPostsCount: 10,
 	previousPostsCount: 0,
 	totalPostsCount: 0,
-	selectedPosts: []
+	selectedPosts: [],
+	removePostsButtonLoading: false
 }
 
 const getters = {
@@ -33,6 +34,20 @@ const getters = {
 }
 
 const actions = {
+	removePostsAsync ({ state, commit }, vue) {
+		commit('setRemovePostsButtonLoading', true)
+
+		let promises = state.selectedPosts.map(id => vue.axios.delete('/api/v1/post/' + id))
+
+		Promise.all(promises)
+			.then(res => {
+				commit('setRemovePostsButtonLoading', false)
+			})
+			.catch(e => {
+				commit('setRemovePostsButtonLoading', false)
+				AjaxErrorHandler(vue.$store)(e)
+			})
+	},
 	addPostAsync ({ state, commit, rootState }, vue) {
 		let content = state.editor.value
 		state.mentions.forEach(mention => {
@@ -251,6 +266,9 @@ const mutations = {
 		} else {
 			state.selectedPosts.push(id)
 		}
+	},
+	setRemovePostsButtonLoading (state, value) {
+		state.removePostsButtonLoading = value
 	}
 }