Forráskód Böngészése

Add reply functionality

sbkwgh 8 éve
szülő
commit
f106ff6a00
2 módosított fájl, 21 hozzáadás és 19 törlés
  1. 1 1
      src/components/routes/Thread.vue
  2. 20 18
      src/store/modules/thread.js

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

@@ -108,7 +108,7 @@
 				this.showEditor();
 			},
 			addPost () {
-				this.$store.dispatch('addPostAsync');
+				this.$store.dispatch('addPostAsync', this);
 			}
 		},
 		created () {

+ 20 - 18
src/store/modules/thread.js

@@ -1,9 +1,11 @@
+import AjaxErrorHandler from '../../assets/js/errorHandler'
+
 const state = {
 	thread: '',
 	posts: [],
 	reply: {
 		username: '',
-		id: ''
+		id: null
 	},
 	editor: {
 		show: false,
@@ -14,29 +16,29 @@ const state = {
 const getters = {}
 
 const actions = {
-	addPostAsync ({ state, commit, rootState }) {
+	addPostAsync ({ state, commit, rootState }, vue) {
 		var post = {
 			content: state.editor.value,
-			username: rootState.username,
-			date: new Date()
+			threadId: +vue.$route.params.id
 		};
 
-		if(state.reply.id.length) {
-			post.replyUsername = state.reply.username;
-			post.replyId = state.reply.id;
+		if(state.reply.id) {
+			post.replyingToId = state.reply.id;
 		}
 
-		//Post to server
-		setTimeout(function() {
-			commit('addPost', post);
-			commit('setThreadEditorValue', '');
-			commit('setThreadEditorState', false);
-			commit({
-				type: 'setReply',
-				username: '',
-				id: ''
-			});
-		}, 1);
+		vue.axios
+			.post('/api/v1/post', post)
+			.then(res => {
+				commit('addPost', res.data);
+				commit('setThreadEditorValue', '');
+				commit('setThreadEditorState', false);
+				commit({
+					type: 'setReply',
+					username: '',
+					id: ''
+				});
+			})
+			.catch(AjaxErrorHandler(vue.$store))
 	}
 }