Skip to content

Commit

Permalink
🐞 fix:修复发表评论之后不刷新评论
Browse files Browse the repository at this point in the history
  • Loading branch information
eleliauk committed Aug 25, 2024
1 parent 6f44d72 commit 0aba69e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/common/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function get(url = '', isToken = true) {
const getToken = () => {
return new Promise((resolve, reject) => {
void Taro.getStorage({
key: 'token',
key: 'shortToken',
success: (res) => {
const token = res.data;
if (token) {
Expand Down
69 changes: 33 additions & 36 deletions src/pages/evaluateInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,43 +92,40 @@ export default function Index() {

const handleReplySubmit = async () => {
if (!replyContent.trim()) return; // 忽略空内容
// console.log(1);

// console.log(
// {
// biz: "Evaluation",
// biz_id,
// content: replyContent,
// parent_id: replyTo?.id || 0,
// root_id: replyTo?.root_comment_id === 0 ? replyTo?.id : (replyTo?.root_comment_id || 0)
// }
// )

post('/comments/publish', {
biz: 'Evaluation',
biz_id,
content: replyContent,
parent_id: replyTo?.id || 0,
root_id:
replyTo?.root_comment_id === 0 ? replyTo?.id : replyTo?.root_comment_id || 0,
}).then((res) => {
console.log('评论发布成功', res);
});

// try {
// const res = await post(`/comments/create`, {
// biz_id,
// content: replyContent,
// parent_comment_id: replyTo?.id || 0,
// reply_to_uid: replyTo?.commentator_id || 0,
// });
// console.log('评论发布成功', res);

// 清空回复目标和输入框
setReplyTo(null);
setReplyContent('');

setCommentsLoaded(false);
try {
await post('/comments/publish', {
biz: 'Evaluation',
biz_id,
content: replyContent,
parent_id: replyTo?.id || 0,
root_id:
replyTo?.root_comment_id === 0 ? replyTo?.id : replyTo?.root_comment_id || 0,
});
console.log('评论发布成功');

// 清空回复目标和输入框
setReplyTo(null);
setReplyContent('');
setplaceholderContent('写下你的评论...');

// 评论发布成功后,重新加载评论
setCommentsLoaded(false); // 先将commentsLoaded设为false,避免useEffect中的fetchComments不被调用
const fetchComments = async () => {
try {
const res = await get(
`/comments/list?biz=Evaluation&biz_id=${biz_id}&cur_comment_id=0&limit=100`
);
setAllComments(res.data);
setCommentsLoaded(true);
} catch (error) {
console.error('加载评论失败', error);
}
};
await fetchComments();
} catch (error) {
console.error('评论发布失败', error);
}
};

// 仅当评论数据加载完成时渲染CommentComponent
Expand Down

0 comments on commit 0aba69e

Please sign in to comment.