Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
feat(question): (#461) 添加回答功能
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Jul 30, 2018
1 parent 55b8a5b commit 9b3f5cb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
18 changes: 18 additions & 0 deletions src/api/question/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ export function getAnswer(answerId) {
return api.get(url, { validateStatus: s => s === 200 });
}

/**
* 添加回答
* @author mutoe <[email protected]>
* @export
* @param {number} questionId
* @param {Object} data
* @param {string} data.body
* @param {string} [data.text_body]
* @param {number} [data.anonymity] 1: 匿名 0: 不匿名
* @returns
*/
export function postAnswer(questionId, data) {
const url = `/questions/${questionId}/answers`;
return api.post(url, data, {
validateStatus: s => s === 201
});
}

/**
* 打赏回答
* @author mutoe <[email protected]>
Expand Down
30 changes: 28 additions & 2 deletions src/page/question/AnswerAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<div class="p-answer-add">
<common-header class="header">
添加回答
<span slot="right" @click="onPublish">发布</span>
<span
slot="right"
:class="{disabled}"
class="post-btn"
@click="onPost">发布</span>
</common-header>

<main>
Expand All @@ -22,10 +26,21 @@ export default {
computed: {
questionId() {
return this.$route.params.questionId || 0;
},
disabled() {
return !this.content;
}
},
methods: {
onPublish() {}
async onPost() {
if (this.disabled) return;
const payload = {
questionId: this.questionId,
content: this.content.replace(/\n/g, "\n\n")
};
await this.$store.dispatch("question/postAnswer", payload);
this.goBack();
}
}
};
</script>
Expand All @@ -36,13 +51,24 @@ export default {
flex-direction: column;
height: 100%;
.header {
.post-btn {
color: @primary;
&.disabled {
color: @border-color;
}
}
}
> main {
flex: auto;
background-color: #fff;
textarea {
padding: 20px;
font-size: 30px;
width: 100%;
height: 100%;
}
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/page/question/QuestionDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</svg>
{{ question.amount ? '已' : '未' }}设置悬赏
</div>
<div class="button">
<div class="button" @click="addAnswer">
<svg class="main-button-icon" fill="#666">
<use xlink:href="#base-edit" />
</svg>
Expand Down Expand Up @@ -126,10 +126,12 @@

<script>
import { render } from "@/util/markdown";
import { show, watch, unwatch } from "@/api/question/questions";
import * as api from "@/api/question/questions";
import { listByDefault, listByTime } from "@/api/question/answer";
import QuestionAnswerItem from "./QuestionAnswerItem";
const noop = () => {};
export default {
name: "QuestionDetail",
components: {
Expand Down Expand Up @@ -204,13 +206,12 @@ export default {
this.loadContainer.beforeRefresh();
},
methods: {
fetch(cb = null) {
show(this.$route.params.id)
fetch(cb = noop) {
api
.show(this.$route.params.id)
.then(({ data }) => {
this.question = data;
if (cb instanceof Function) {
cb();
}
cb();
})
.catch(({ response: { data } = {} }) => {
this.loadContainer.topEnd(false);
Expand Down Expand Up @@ -238,7 +239,8 @@ export default {
this.answersTimeOrder = !this.answersTimeOrder;
},
handleWatch() {
watch(this.$route.params.id)
api
.watch(this.$route.params.id)
.then(() => {
this.question.watched = true;
this.question.watchers_count += 1;
Expand All @@ -248,7 +250,8 @@ export default {
});
},
handleUnwatch() {
unwatch(this.$route.params.id)
api
.unwatch(this.$route.params.id)
.then(() => {
this.question.watched = false;
this.question.watchers_count -= 1;
Expand All @@ -258,9 +261,7 @@ export default {
});
},
handleLoadMoreAnswers() {
if (!this.answers.length) {
return;
}
if (!this.answers.length) return;
this.answerRequestMethod(this.$route.params.id, this.answers.length)
.then(({ data }) => {
this.loadContainer.bottomEnd(data.length < 15);
Expand All @@ -270,6 +271,9 @@ export default {
this.loadContainer.bottomEnd(true);
this.$Message.error(data);
});
},
addAnswer() {
this.$router.push({ path: `/question/${this.question.id}/answers/add` });
}
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/stores/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import post from "./post";
import easemob from "./easemob";
import wallet from "./wallet";
import currency from "./currency";
import question from "./question";

export default {
rank,
post,
MESSAGE,
easemob,
wallet,
currency
currency,
question
};
23 changes: 23 additions & 0 deletions src/stores/module/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as answerApi from "@/api/question/answer";
import Message from "@/plugins/message";

const actions = {
async postAnswer(state, payload) {
const { questionId, content } = payload;
const { data } = await answerApi
.postAnswer(questionId, {
body: content,
text_body: content
})
.catch(({ response }) => {
const { errors } = response.data;
Message.error(errors);
});
return data;
}
};

export default {
namespaced: true,
actions
};

0 comments on commit 9b3f5cb

Please sign in to comment.