BUG fix: PublishComment

This commit is contained in:
KUN1007 2023-10-08 20:45:48 +08:00
parent 93d2ce395c
commit 1f33d3b765
9 changed files with 154 additions and 118 deletions

View file

@ -102,16 +102,6 @@ export const useKUNGalgameTopicStore = defineStore({
isReplyRewriting: false, isReplyRewriting: false,
}, },
commentDraft: {
tid: 0,
rid: 0,
c_uid: 0,
to_uid: 0,
content: '',
isShowCommentPanelRid: 0,
},
}), }),
actions: { actions: {
// 左侧相同标签下的其它话题 // 左侧相同标签下的其它话题
@ -265,12 +255,17 @@ export const useKUNGalgameTopicStore = defineStore({
}, },
// 创建一个评论 // 创建一个评论
async postNewComment(): Promise<TopicCreateCommentResponseData> { async postNewComment(
tid: number,
rid: number,
toUid: number,
content: string
): Promise<TopicCreateCommentResponseData> {
const requestData: TopicCreateCommentRequestData = { const requestData: TopicCreateCommentRequestData = {
tid: this.commentDraft.tid, tid: tid,
rid: this.commentDraft.rid, rid: rid,
to_uid: this.commentDraft.to_uid, to_uid: toUid,
content: this.commentDraft.content, content: content,
} }
return await postCommentByPidAndRidApi(requestData) return await postCommentByPidAndRidApi(requestData)
}, },
@ -291,16 +286,6 @@ export const useKUNGalgameTopicStore = defineStore({
this.replyRequest.page = 1 this.replyRequest.page = 1
this.isLoading = true this.isLoading = true
}, },
// 设置评论草稿为原始值,用于评论发布按钮
resetCommentDraft() {
this.commentDraft.tid = 0
this.commentDraft.rid = 0
this.commentDraft.c_uid = 0
this.commentDraft.to_uid = 0
this.commentDraft.content = ''
this.commentDraft.isShowCommentPanelRid = 0
},
// 重置重新编辑回复数据,用于重新编辑回复 // 重置重新编辑回复数据,用于重新编辑回复
resetRewriteTopicData() { resetRewriteTopicData() {
this.replyDraft.textCount = 0 this.replyDraft.textCount = 0

20
src/store/temp/comment.ts Normal file
View file

@ -0,0 +1,20 @@
// 评论的临时数据,用于组件间传输
import { defineStore } from 'pinia'
export const useTempCommentStore = defineStore({
id: 'tempComment',
// 不持久
persist: false,
state: () => ({
tid: 0,
rid: 0,
toUid: 0,
toUsername: '',
content: '',
// 要展示哪个回复底下的评论面板
isShowCommentPanelRid: 0,
}),
getters: {},
actions: {},
})

View file

@ -36,19 +36,6 @@ interface ReplyRequest {
sortOrder: 'asc' | 'desc' sortOrder: 'asc' | 'desc'
} }
// 评论的缓存
interface CommentDraft {
// 评论的内容
tid: number
rid: number
c_uid: number
to_uid: number
content: string
// 显示哪个评论的评论面板
isShowCommentPanelRid: number
}
// 更新评论的缓存 // 更新评论的缓存
interface ReplyRewrite { interface ReplyRewrite {
tid: number tid: number
@ -83,7 +70,4 @@ export interface TopicStore {
replyRequest: ReplyRequest replyRequest: ReplyRequest
// 更新评论的缓存 // 更新评论的缓存
replyRewrite: ReplyRewrite replyRewrite: ReplyRewrite
// 评论的缓存
commentDraft: CommentDraft
} }

18
src/utils/toggle.ts Normal file
View file

@ -0,0 +1,18 @@
import { ref } from 'vue'
const toggleStatus = (initState: boolean, delay: number) => {
const on = ref(initState ?? false)
const toggle = (value: boolean) => {
setTimeout(() => {
on.value = value ?? !on.value
}, delay)
}
return {
on,
toggle,
}
}
export default toggleStatus

View file

@ -32,6 +32,8 @@ import { useKUNGalgameTopicStore } from '@/store/modules/topic'
import { useTempReplyStore } from '@/store/temp/reply' import { useTempReplyStore } from '@/store/temp/reply'
// //
import { useTempReplyRewriteStore } from '@/store/temp/replyRewrite' import { useTempReplyRewriteStore } from '@/store/temp/replyRewrite'
// 使 store
import { useTempCommentStore } from '@/store/temp/comment'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
// //
@ -42,7 +44,6 @@ const {
isShowAdvance, isShowAdvance,
isEdit, isEdit,
replyRequest, replyRequest,
commentDraft,
isScrollToTop, isScrollToTop,
isLoading, isLoading,
scrollToReplyId, scrollToReplyId,
@ -51,6 +52,7 @@ const { tempReply } = storeToRefs(useTempReplyStore())
const { rid, replyContent, tags, edited } = storeToRefs( const { rid, replyContent, tags, edited } = storeToRefs(
useTempReplyRewriteStore() useTempReplyRewriteStore()
) )
const { isShowCommentPanelRid } = storeToRefs(useTempCommentStore())
const tid = computed(() => { const tid = computed(() => {
return Number(route.params.tid) return Number(route.params.tid)
@ -227,7 +229,7 @@ const topicPageWidth = computed(() => {
// //
const resetPanelStatus = () => { const resetPanelStatus = () => {
commentDraft.value.isShowCommentPanelRid = 0 isShowCommentPanelRid.value = 0
isShowAdvance.value = false isShowAdvance.value = false
isEdit.value = false isEdit.value = false
// //

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref } from 'vue' import { ref } from 'vue'
// //
import message from '@/components/alert/Message' import message from '@/components/alert/Message'
import { debounce } from '@/utils/debounce' import { debounce } from '@/utils/debounce'
@ -10,28 +10,20 @@ import { TopicComment } from '@/api/index'
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer' import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
// store // store
import { useKUNGalgameTopicStore } from '@/store/modules/topic' import { useKUNGalgameTopicStore } from '@/store/modules/topic'
// 使 store
import { useTempCommentStore } from '@/store/temp/comment'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
const { name, uid } = storeToRefs(useKUNGalgameUserStore()) const { name } = storeToRefs(useKUNGalgameUserStore())
// props const { tid, rid, toUid, toUsername, content, isShowCommentPanelRid } =
const props = defineProps<{ storeToRefs(useTempCommentStore())
tid: number
rid: number
to_user: {
uid: number
name: string
}
}>()
// emits // emits
const emits = defineEmits<{ const emits = defineEmits<{
getCommentEmits: [newComment: TopicComment] getCommentEmits: [newComment: TopicComment]
}>() }>()
// 使 store
const { commentDraft } = storeToRefs(useKUNGalgameTopicStore())
// //
const commentValue = ref('') const commentValue = ref('')
@ -39,32 +31,23 @@ const commentValue = ref('')
const handleInputComment = () => { const handleInputComment = () => {
// //
const debouncedUpdateContent = debounce(() => { const debouncedUpdateContent = debounce(() => {
commentDraft.value.content = commentValue.value content.value = commentValue.value
}, 1007) }, 1007)
// //
debouncedUpdateContent() debouncedUpdateContent()
} }
//
const saveComment = () => {
commentDraft.value.tid = props.tid
commentDraft.value.rid = props.rid
commentDraft.value.c_uid = uid.value
commentDraft.value.to_uid = props.to_user.uid
commentDraft.value.content = commentValue.value
}
// //
const isValidComment = () => { const isValidComment = () => {
// //
if (!commentDraft.value.content.trim()) { if (!content.value.trim()) {
message('Comment content cannot be empty!', '评论内容不能为空!', 'warn') message('Comment content cannot be empty!', '评论内容不能为空!', 'warn')
return false return false
} }
// //
if (commentDraft.value.content.trim().length > 1007) { if (content.value.trim().length > 1007) {
message( message(
'The maximum length for comments should not exceed 1007 characters.', 'The maximum length for comments should not exceed 1007 characters.',
'评论最大长度不可超过1007个字符', '评论最大长度不可超过1007个字符',
@ -76,45 +59,44 @@ const isValidComment = () => {
return true return true
} }
//
onMounted(() => {
//
useKUNGalgameTopicStore().resetCommentDraft()
})
// //
const handlePublishComment = async () => { const handlePublishComment = async () => {
//
saveComment()
if (isValidComment()) { if (isValidComment()) {
// //
const newComment = (await useKUNGalgameTopicStore().postNewComment()).data const newComment = (
await useKUNGalgameTopicStore().postNewComment(
// tid.value,
useKUNGalgameTopicStore().resetCommentDraft() rid.value,
toUid.value,
content.value
)
).data
// //
emits('getCommentEmits', newComment) emits('getCommentEmits', newComment)
// //
message('Comment publish successfully!', '评论发布成功', 'success') message('Comment publish successfully!', '评论发布成功', 'success')
handleCloseCommentPanel()
} else {
message('Comment publish failed!', '评论发布失败', 'success')
} }
} }
// //
const handleCloseCommentPanel = () => { const handleCloseCommentPanel = () => {
commentDraft.value.isShowCommentPanelRid = 0 isShowCommentPanelRid.value = 0
} }
</script> </script>
<template> <template>
<div class="panel" v-if="commentDraft.isShowCommentPanelRid === rid"> <div class="panel">
<div class="top"> <div class="top">
<div class="title"> <div class="title">
<span>{{ name }}</span> <span>{{ name }}</span>
<span>{{ $tm('topic.content.comment') }}</span> <span>{{ $tm('topic.content.comment') }}</span>
<span>{{ to_user.name }}</span> <span>{{ toUsername }}</span>
</div> </div>
<div class="confirm"> <div class="confirm">
<button @click="handlePublishComment"> <button @click="handlePublishComment">

View file

@ -2,7 +2,7 @@
这是回复话题下方的评论区包含了所有的评论是一个单独的组件它的子组件是单个评论 这是回复话题下方的评论区包含了所有的评论是一个单独的组件它的子组件是单个评论
--> -->
<script setup lang="ts"> <script setup lang="ts">
import { defineAsyncComponent, onMounted, reactive, ref, toRaw } from 'vue' import { defineAsyncComponent, onMounted, reactive, ref, watch } from 'vue'
import { Icon } from '@iconify/vue' import { Icon } from '@iconify/vue'
const CommentPanel = defineAsyncComponent(() => import('./CommentPanel.vue')) const CommentPanel = defineAsyncComponent(() => import('./CommentPanel.vue'))
@ -10,8 +10,14 @@ import { TopicComment } from '@/api/index'
// store // store
import { useKUNGalgameTopicStore } from '@/store/modules/topic' import { useKUNGalgameTopicStore } from '@/store/modules/topic'
// 使 store
import { useTempCommentStore } from '@/store/temp/comment'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
const { commentDraft } = storeToRefs(useKUNGalgameTopicStore())
const { tid, rid, toUid, toUsername, isShowCommentPanelRid } = storeToRefs(
useTempCommentStore()
)
const props = defineProps<{ const props = defineProps<{
tid: number tid: number
@ -22,16 +28,25 @@ const props = defineProps<{
} }
}>() }>()
// const tidRef = ref(props.tid)
const commentsData = ref<TopicComment[]>() const ridRef = ref(props.rid)
// const toUser = ref(props.toUser)
const toUserInfo = reactive({
uid: 0,
name: '',
})
const getComments = async (tid: number, rid: number) => { //
return (await useKUNGalgameTopicStore().getComments(tid, rid)).data watch(
() => props.rid,
async () => {
ridRef.value = props.rid
toUser.value = props.toUser
commentsData.value = await getComments(tidRef.value, ridRef.value)
}
)
//
const commentsData = ref<TopicComment[]>([])
const getComments = async (topicId: number, replyId: number) => {
return (await useKUNGalgameTopicStore().getComments(topicId, replyId)).data
} }
// push // push
@ -40,21 +55,22 @@ const getCommentEmits = (newComment: TopicComment) => {
} }
onMounted(async () => { onMounted(async () => {
// commentsData.value = await getComments(tidRef.value, ridRef.value)
toUserInfo.name = props.toUser.name
toUserInfo.uid = props.toUser.uid
commentsData.value = await getComments(props.tid, props.rid)
}) })
// //
const handleClickReply = (uid: number, name: string) => { const handleClickComment = (
// topicId: number,
toUserInfo.name = name replyId: number,
toUserInfo.uid = uid uid: number,
name: string
) => {
tid.value = topicId
rid.value = replyId
toUid.value = uid
toUsername.value = name
// //
commentDraft.value.isShowCommentPanelRid = props.rid isShowCommentPanelRid.value = ridRef.value
} }
</script> </script>
@ -64,9 +80,7 @@ const handleClickReply = (uid: number, name: string) => {
<!-- 评论的弹出面板 --> <!-- 评论的弹出面板 -->
<CommentPanel <CommentPanel
@getCommentEmits="getCommentEmits" @getCommentEmits="getCommentEmits"
:tid="tid" v-if="isShowCommentPanelRid === ridRef"
:rid="rid"
:to_user="toUserInfo"
/> />
<!-- 评论的展示区域 --> <!-- 评论的展示区域 -->
@ -107,7 +121,12 @@ const handleClickReply = (uid: number, name: string) => {
</li> </li>
<li <li
@click=" @click="
handleClickReply(comment.c_user.uid, comment.c_user.name) handleClickComment(
comment.tid,
comment.rid,
comment.c_user.uid,
comment.c_user.name
)
" "
> >
<Icon class="icon" icon="fa-regular:comment-dots" /> <Icon class="icon" icon="fa-regular:comment-dots" />

View file

@ -105,7 +105,7 @@ const currUserUid = useKUNGalgameUserStore().uid
<!-- 分享 --> <!-- 分享 -->
<span class="icon"><Icon icon="majesticons:share-line" /></span> <span class="icon"><Icon icon="majesticons:share-line" /></span>
<!-- 只看 --> <!-- 只看 TODO: -->
<!-- <span class="icon"><Icon icon="ph:user-focus-duotone" /></span> --> <!-- <span class="icon"><Icon icon="ph:user-focus-duotone" /></span> -->
<!-- 编辑 --> <!-- 编辑 -->

View file

@ -28,9 +28,16 @@ import { TopicReply } from '@/api/index'
// store // store
import { useKUNGalgameTopicStore } from '@/store/modules/topic' import { useKUNGalgameTopicStore } from '@/store/modules/topic'
// 使 store
import { useTempCommentStore } from '@/store/temp/comment'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
const { scrollToReplyId, commentDraft } = storeToRefs(useKUNGalgameTopicStore()) const { scrollToReplyId } = storeToRefs(useKUNGalgameTopicStore())
const { tid, rid, toUid, toUsername, isShowCommentPanelRid } = storeToRefs(
useTempCommentStore()
)
const props = defineProps<{ const props = defineProps<{
repliesData: TopicReply[] repliesData: TopicReply[]
@ -42,12 +49,21 @@ const replies = computed(() => props.repliesData)
// //
const isCommentPanelOpen = ref(false) const isCommentPanelOpen = ref(false)
// //
const handleClickComment = (rid: number) => { const handleClickComment = (
topicId: number,
replyIid: number,
uid: number,
name: string
) => {
isCommentPanelOpen.value = !isCommentPanelOpen.value isCommentPanelOpen.value = !isCommentPanelOpen.value
if (isCommentPanelOpen.value) { if (isCommentPanelOpen.value) {
commentDraft.value.isShowCommentPanelRid = rid tid.value = topicId
rid.value = replyIid
toUid.value = uid
toUsername.value = name
isShowCommentPanelRid.value = replyIid
} else { } else {
commentDraft.value.isShowCommentPanelRid = 0 isShowCommentPanelRid.value = 0
} }
} }
</script> </script>
@ -132,7 +148,17 @@ const handleClickComment = (rid: number) => {
:to-floor="reply.floor" :to-floor="reply.floor"
> >
<template #comment> <template #comment>
<span @click="handleClickComment(reply.rid)" class="icon"> <span
@click="
handleClickComment(
reply.tid,
reply.rid,
reply.r_user.uid,
reply.r_user.name
)
"
class="icon"
>
<Icon icon="fa-regular:comment-dots" /> <Icon icon="fa-regular:comment-dots" />
</span> </span>
</template> </template>
@ -273,7 +299,7 @@ const handleClickComment = (rid: number) => {
/* 回复被推的样式 */ /* 回复被推的样式 */
.active-upvote .container { .active-upvote .container {
border: 2px solid var(--kungalgame-pink-3); border: 1px solid var(--kungalgame-red-4);
} }
/* 滚动到指定话题激活后的样式 */ /* 滚动到指定话题激活后的样式 */