pagination + ...

This commit is contained in:
hamid zarghami
2025-04-23 16:18:51 +03:30
parent 72bd413aee
commit 7e2f0af7c9
7 changed files with 131 additions and 14 deletions
+17 -2
View File
@@ -1,5 +1,5 @@
'use client' 'use client'
import { FC } from 'react' import { FC, useState } from 'react'
import HeroSection from './HeroSection' import HeroSection from './HeroSection'
import Category from './Category' import Category from './Category'
import BigBlogItem from './BigBlogItem' import BigBlogItem from './BigBlogItem'
@@ -10,10 +10,12 @@ import { useBlogStore } from '../store/BlogStore'
import { useGetAds } from '@/app/ads/hooks/useAdsData' import { useGetAds } from '@/app/ads/hooks/useAdsData'
import { AdsDisplayLocation } from '@/app/ads/types/AdsTypes' import { AdsDisplayLocation } from '@/app/ads/types/AdsTypes'
import Image from 'next/image' import Image from 'next/image'
import Pagination from '@/components/Pagination'
const BlogList: FC = () => { const BlogList: FC = () => {
const { selectedCategory } = useBlogStore() const { selectedCategory } = useBlogStore()
const { data } = useGetBlogs(selectedCategory) const [page, setPage] = useState<number>(1)
const { data } = useGetBlogs(selectedCategory, page)
const getAds = useGetAds(AdsDisplayLocation.BLOG_PAGE_TOP_RIGHT) const getAds = useGetAds(AdsDisplayLocation.BLOG_PAGE_TOP_RIGHT)
const getAdsBottom = useGetAds(AdsDisplayLocation.BLOG_PAGE_BOTTOM_RIGHT) const getAdsBottom = useGetAds(AdsDisplayLocation.BLOG_PAGE_BOTTOM_RIGHT)
@@ -78,6 +80,19 @@ const BlogList: FC = () => {
{data?.data.blogs.map((item) => ( {data?.data.blogs.map((item) => (
<BigBlogItem key={item.id} item={item} /> <BigBlogItem key={item.id} item={item} />
))} ))}
{
data?.data.pager &&
<div>
<Pagination
currentPage={data?.data?.pager.page}
totalPages={data?.data?.pager.totalPages}
onPageChange={(page) => {
setPage(page)
}}
/>
</div>
}
</div> </div>
<div className='block xl:hidden'> <div className='block xl:hidden'>
+1 -1
View File
@@ -7,7 +7,7 @@ const MostVisited: FC = () => {
return ( return (
<div className='w-full rounded-4xl bg-white p-4'> <div className='w-full rounded-4xl bg-white p-4'>
<h4> <h4>
پر بازدیدترین ها انتخاب سردبیر
</h4> </h4>
<div className='flex flex-col gap-6 mt-7 text-sm'> <div className='flex flex-col gap-6 mt-7 text-sm'>
+5 -5
View File
@@ -3,10 +3,10 @@ import * as api from "../service/BlogService";
import { CreateBlogCommentType } from "../types/BlogTypes"; import { CreateBlogCommentType } from "../types/BlogTypes";
// برای client // برای client
export const useGetBlogs = (categoryId: string) => export const useGetBlogs = (categoryId: string, page: number) =>
useQuery({ useQuery({
queryKey: ["blogs", categoryId], queryKey: ["blogs", categoryId, page],
queryFn: () => api.getBlogs(categoryId), queryFn: () => api.getBlogs(categoryId, page),
}); });
export const useGetMostVisitedBlogs = () => export const useGetMostVisitedBlogs = () =>
@@ -47,8 +47,8 @@ export const useCreateBlogComment = () =>
// برای server prefetch // برای server prefetch
export const blogsQueryOptions = { export const blogsQueryOptions = {
queryKey: ["blogs", ""], queryKey: ["blogs", "", 1],
queryFn: () => api.getBlogs(""), queryFn: () => api.getBlogs("", 1),
}; };
export const blogSingleQueryOptions = (id: string) => ({ export const blogSingleQueryOptions = (id: string) => ({
+3 -3
View File
@@ -7,12 +7,12 @@ import {
CreateBlogCommentType, CreateBlogCommentType,
} from "../types/BlogTypes"; } from "../types/BlogTypes";
export const getBlogs = async (categoryId: string) => { export const getBlogs = async (categoryId: string, page: number) => {
let query = ""; let query = "";
if (categoryId) { if (categoryId) {
query = `?categoryId=${categoryId}`; query = `&categoryId=${categoryId}`;
} }
const { data } = await axios.get<BlogResponse>(`/blogs${query}`); const { data } = await axios.get<BlogResponse>(`/blogs?page=${page}${query}`);
return data; return data;
}; };
+2 -2
View File
@@ -8,9 +8,9 @@ import {
} from "../types/ProductTypes"; } from "../types/ProductTypes";
export const getServicesByCategory = async (categoryId?: string) => { export const getServicesByCategory = async (categoryId?: string) => {
let query = ``; let query = `?limit=50`;
if (categoryId) { if (categoryId) {
query = `?categoryId=${categoryId}`; query = `&categoryId=${categoryId}`;
} }
const { data } = await axios.get<ServiceResponse>( const { data } = await axios.get<ServiceResponse>(
`/danak-services/public${query}` `/danak-services/public${query}`
+100
View File
@@ -0,0 +1,100 @@
'use client'
import React from "react";
interface PaginationProps {
currentPage: number;
totalPages: number;
onPageChange: (page: number) => void;
}
const Pagination: React.FC<PaginationProps> = ({
currentPage,
totalPages,
onPageChange,
}) => {
const getPageNumbers = () => {
const pageNumbers: (number | string)[] = [];
const maxVisiblePages = 5; // تعداد حداکثری صفحات قابل مشاهده
if (totalPages <= maxVisiblePages) {
for (let i = 1; i <= totalPages; i++) {
pageNumbers.push(i);
}
} else {
if (currentPage > 3) {
pageNumbers.push(1);
if (currentPage > 4) {
pageNumbers.push("...");
}
}
const start = Math.max(2, currentPage - 1);
const end = Math.min(totalPages - 1, currentPage + 1);
for (let i = start; i <= end; i++) {
pageNumbers.push(i);
}
if (currentPage < totalPages - 2) {
if (currentPage < totalPages - 3) {
pageNumbers.push("...");
}
pageNumbers.push(totalPages);
}
}
return pageNumbers;
};
const pageNumbers = getPageNumbers();
return (
<div className="flex gap-2 text-xs justify-center items-center space-x-2 mt-4">
{/* دکمه قبلی */}
{/* <button
className={`px-3 py-1 rounded-md ${currentPage === 1
? "text-gray-400 cursor-not-allowed"
: "text-blue-600 hover:bg-gray-100"
}`}
onClick={() => currentPage > 1 && onPageChange(currentPage - 1)}
disabled={currentPage === 1}
>
قبلی
</button> */}
{/* شماره صفحات */}
{pageNumbers.map((page, index) =>
typeof page === "number" ? (
<button
key={index}
className={`size-8 rounded-md ${currentPage === page
? "bg-primary text-white"
: "text-primary bg-[#EAECF5] hover:bg-gray-100"
}`}
onClick={() => onPageChange(page)}
>
{page}
</button>
) : (
<span key={index} className="px-3 py-1 text-gray-500">
{page}
</span>
)
)}
{/* دکمه بعدی */}
{/* <button
className={`px-3 py-1 rounded-md ${currentPage === totalPages
? "text-gray-400 cursor-not-allowed"
: "text-blue-600 hover:bg-gray-100"
}`}
onClick={() => currentPage < totalPages && onPageChange(currentPage + 1)}
disabled={currentPage === totalPages}
>
بعدی
</button> */}
</div>
);
};
export default Pagination;
+3 -1
View File
@@ -117,7 +117,9 @@ const Header: FC = () => {
/> />
</div> </div>
<Image src='/images/logo.svg' alt='logo' width={73} height={24} /> <Link href='/'>
<Image src='/images/logo.svg' alt='logo' width={73} height={24} />
</Link>
<div className='flex gap-2 items-center flex-1 justify-end'> <div className='flex gap-2 items-center flex-1 justify-end'>
<SearchNormal <SearchNormal