Make comments collapsable

This commit is contained in:
marzban-dev
2025-04-16 22:13:05 +03:30
parent 3d660af2b9
commit a71d40987e
@@ -15,16 +15,17 @@ const page = ref(1);
const { token } = useAuth(); const { token } = useAuth();
const userComment = ref(""); const userComment = ref("");
const showMoreComments = ref(false);
const { data: comments, refetch: refetchComments } = useGetComments(id, page); const { data: comments, refetch: refetchComments } = useGetComments(id, page);
const { mutateAsync: createComment, isPending: isCreateCommentPending } = const { mutateAsync: createComment, isPending: isCreateCommentPending } = useCreateComment(id);
useCreateComment(id);
// methods // methods
const submitComment = async () => { const submitComment = async () => {
if (userComment.value.length > 3) { if (userComment.value.length > 3) {
await createComment({ await createComment({
content: userComment.value content: userComment.value,
}); });
userComment.value = ""; userComment.value = "";
@@ -32,6 +33,16 @@ const submitComment = async () => {
await refetchComments(); await refetchComments();
} }
}; };
// computed
const limitedComments = computed(() => {
if (showMoreComments.value) {
return comments.value!.results;
}
return comments.value!.results.slice(0, 3);
});
</script> </script>
<template> <template>
@@ -43,9 +54,7 @@ const submitComment = async () => {
<h3 class="typo-h-6 max-sm:text-xl md:typo-h-5 lg:typo-h-4">نظرات کاربران</h3> <h3 class="typo-h-6 max-sm:text-xl md:typo-h-5 lg:typo-h-4">نظرات کاربران</h3>
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<Rating :rate="2" /> <Rating :rate="2" />
<span class="typo-p-sm"> <span class="typo-p-sm"> بر اساس {{ comments?.count }} نظر </span>
بر اساس {{ comments?.count }} نظر
</span>
</div> </div>
<form <form
@submit.prevent="submitComment" @submit.prevent="submitComment"
@@ -66,8 +75,14 @@ const submitComment = async () => {
> >
نظر بنویسید نظر بنویسید
</Button> </Button>
<NuxtLink v-else to="/signin"> <NuxtLink
<Button type="button" class="rounded-full w-full"> v-else
to="/signin"
>
<Button
type="button"
class="rounded-full w-full"
>
وارد شوید وارد شوید
</Button> </Button>
</NuxtLink> </NuxtLink>
@@ -75,18 +90,30 @@ const submitComment = async () => {
</div> </div>
<div class="flex flex-col gap-8 w-full"> <div class="flex flex-col gap-8 w-full">
<Comment <Comment
v-for="comment in comments!.results" v-for="comment in limitedComments"
:key="comment.id" :key="comment.id"
title="" title=""
:content="comment.content" :content="comment.content"
:date="comment.timestamp" :date="comment.timestamp"
:username="'منصور مرزبان'" :username="'منصور مرزبان'"
/> />
<div class="flex items-center justify-center w-full"> <div class="flex items-center justify-center w-full">
<Pagination <Pagination
v-if="showMoreComments"
:total="comments!.count" :total="comments!.count"
:items="comments!.results.map((item, i) => ({ type: 'page', value: i }))" :items="comments!.results.map((item, i) => ({ type: 'page', value: i }))"
/> />
<Button
v-else
type="button"
variant="primary"
@click="showMoreComments = !showMoreComments"
class="rounded-full px-8"
end-icon="bi:plus"
>
نمایش همه
</Button>
</div> </div>
</div> </div>
</div> </div>