import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/24/outline'; import { useDispatch, useSelector } from 'react-redux'; import { RootState } from '../../../../app/store'; import { useGetProductsQuery } from '../../../../features/products/productsApi'; import { changeQueryParams } from '../../../../features/products/productsSlice'; const Pagination = () => { const { query } = useSelector((state: RootState) => state.products); const { data, isLoading } = useGetProductsQuery(query, { refetchOnMountOrArgChange: true, }); const dispatch = useDispatch(); const handleNext = () => { if (data?.max_num_pages === 0 || data?.max_num_pages === query.page) return; dispatch(changeQueryParams({ ...query, page: query.page + 1 })); }; const handlePrev = () => { if (query.page === 1) return; dispatch(changeQueryParams({ ...query, page: query.page - 1 })); }; if (isLoading) return; // TODO: Add Loading State return (
Showing {query.page * query.per_page - query.per_page + 1} -{' '} {query.page * query.per_page > data?.total_products ? data?.total_products : query.page * query.per_page} of{' '} {data?.total_products} Products
) : (No Product Found.
)}