Fix comment pagination and change show limit to 8 items

This commit is contained in:
marzban-dev
2026-05-16 19:28:43 +03:30
parent b07336f9f1
commit 1a4fce3e13
4 changed files with 27 additions and 10 deletions
@@ -19,7 +19,6 @@ const props = defineProps<Props>();
const route = useRoute(); const route = useRoute();
const id = route.params.id as string | undefined; const id = route.params.id as string | undefined;
const page = ref(1);
const { token } = useAuth(); const { token } = useAuth();
const userTitle = ref(""); const userTitle = ref("");
@@ -28,7 +27,7 @@ const selectedRating = ref(5);
const showMoreComments = ref(false); const showMoreComments = ref(false);
const { data: comments, refetch: refetchComments } = useGetComments(id, page); const { data: comments, refetch: refetchComments } = useGetComments(id);
const { mutateAsync: createComment, isPending: isCreateCommentPending } = useCreateComment(id); const { mutateAsync: createComment, isPending: isCreateCommentPending } = useCreateComment(id);
const { mutateAsync: rateProduct, isPending: isRateProductPending } = useRateProduct(props.product.slug); const { mutateAsync: rateProduct, isPending: isRateProductPending } = useRateProduct(props.product.slug);
@@ -209,10 +208,10 @@ const limitedComments = computed(() => {
v-if="showMoreComments" 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 }))"
:per-page="3" :per-page="8"
/> />
<Button <Button
v-else v-else-if="comments.count > 3"
type="button" type="button"
variant="primary" variant="primary"
@click="showMoreComments = !showMoreComments" @click="showMoreComments = !showMoreComments"
@@ -1,28 +1,35 @@
// imports // imports
import { useQuery } from "@tanstack/vue-query"; import { useQuery } from "@tanstack/vue-query";
import { useAppParams } from "~/composables/global/useAppParams";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
// types // types
export type GetCommentsResponse = ApiPaginated<UserComment>; export type GetCommentsResponse = ApiPaginated<UserComment>;
const useGetComments = (id: string | number | undefined, page: Ref<number>) => { const useGetComments = (id: string | number | undefined) => {
// state // state
const { $axios: axios } = useNuxtApp(); const { $axios: axios } = useNuxtApp();
const { page } = useAppParams();
// methods // methods
const handleGetComments = async () => { const handleGetComments = async () => {
const { data } = await axios.get<GetCommentsResponse>(`${API_ENDPOINTS.product.comments}/${id}`); const { data } = await axios.get<GetCommentsResponse>(`${API_ENDPOINTS.product.comments}/${id}`, {
params: {
offset: Number(page.value) * 8 - 8,
limit: 8,
},
});
return data; return data;
}; };
return useQuery({ return useQuery({
queryKey: [QUERY_KEYS.comments, id, page], queryKey: [QUERY_KEYS.comments, id, page],
queryFn: () => handleGetComments() queryFn: () => handleGetComments(),
}); });
}; };
+4 -1
View File
@@ -3,6 +3,7 @@ import { PRODUCT_RANGE } from "~/constants";
export const useAppParams = () => { export const useAppParams = () => {
// state // state
const route = useRoute();
const { y } = useWindowScroll({ behavior: "smooth" }); const { y } = useWindowScroll({ behavior: "smooth" });
const slug = useRouteParams<string | undefined>("slug"); const slug = useRouteParams<string | undefined>("slug");
@@ -58,7 +59,9 @@ export const useAppParams = () => {
watch( watch(
() => page.value, () => page.value,
() => { () => {
y.value = 0; if (route.name !== "product-id") {
y.value = 0;
}
}, },
); );
+9 -1
View File
@@ -4,13 +4,15 @@
import ChatButton from "~/components/product/ChatBox/ChatButton.vue"; import ChatButton from "~/components/product/ChatBox/ChatButton.vue";
import useGetProduct from "~/composables/api/product/useGetProduct"; import useGetProduct from "~/composables/api/product/useGetProduct";
import ProductsSlider from "~/components/global/product-detail/ProductsSlider.vue"; import ProductsSlider from "~/components/global/product-detail/ProductsSlider.vue";
import { useAppParams } from "~/composables/global/useAppParams";
// state // state
const route = useRoute(); const route = useRoute();
const id = route.params.id as string | undefined; const id = route.params.id as string | undefined;
const { page } = useAppParams();
const { suspense: suspenseProduct, data: product } = useGetProduct(id); const { suspense: suspenseProduct, data: product } = useGetProduct(id);
useSeoMeta({ useSeoMeta({
@@ -49,6 +51,12 @@ if (productResponse.isError) {
statusMessage: `error : product ${id} prefetch error`, statusMessage: `error : product ${id} prefetch error`,
}); });
} }
// lifecycle
onMounted(() => {
page.value = 1;
});
</script> </script>
<template> <template>