Skip to content

Commit

Permalink
feat: add pagination to token factory list (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
chalabi2 authored Dec 2, 2024
1 parent 0a6e0b5 commit 20f6fdb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 22 deletions.
102 changes: 87 additions & 15 deletions components/factory/components/MyDenoms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ExtendedMetadataSDKType, shiftDigits } from '@/utils';
import { MultiMintModal } from '@/components/factory/modals';
import { MultiBurnModal } from '../modals/multiMfxBurnModal';
import { usePoaGetAdmin } from '@/hooks';
import useIsMobile from '@/hooks/useIsMobile';

export default function MyDenoms({
denoms,
isLoading,
Expand All @@ -25,12 +27,27 @@ export default function MyDenoms({
address: string;
}) {
const [searchQuery, setSearchQuery] = useState('');
const [currentPage, setCurrentPage] = useState(1);
const isMobile = useIsMobile();

const pageSize = isMobile ? 6 : 8;

const router = useRouter();
const [selectedDenom, setSelectedDenom] = useState<ExtendedMetadataSDKType | null>(null);
const [modalType, setModalType] = useState<
'mint' | 'burn' | 'multimint' | 'multiburn' | 'update' | null
>(null);

const filteredDenoms = useMemo(() => {
return denoms.filter(denom => denom?.display.toLowerCase().includes(searchQuery.toLowerCase()));
}, [denoms, searchQuery]);

const totalPages = Math.ceil(filteredDenoms.length / pageSize);
const paginatedDenoms = filteredDenoms.slice(
(currentPage - 1) * pageSize,
currentPage * pageSize
);

const handleDenomSelect = (denom: ExtendedMetadataSDKType) => {
if (!modalType) {
// Only show denom info if no other modal is active
Expand Down Expand Up @@ -107,10 +124,6 @@ export default function MyDenoms({
});
};

const filteredDenoms = useMemo(() => {
return denoms.filter(denom => denom?.display.toLowerCase().includes(searchQuery.toLowerCase()));
}, [denoms, searchQuery]);

return (
<div className="relative w-full h-screen overflow-hidden">
<div className="h-full flex flex-col p-4">
Expand Down Expand Up @@ -149,13 +162,13 @@ export default function MyDenoms({
<tr className="text-sm font-medium">
<th className="bg-transparent w-1/4">Token</th>
<th className="bg-transparent w-2/5 xl:table-cell hidden">Name</th>
<th className="bg-transparent w-2/5">Total Supply</th>
<th className="bg-transparent w-2/5 md:table-cell hidden">Total Supply</th>
<th className="bg-transparent w-1/4">Actions</th>
</tr>
</thead>
<tbody className="space-y-4">
{isLoading
? Array(10)
? Array(isMobile ? 6 : 8)
.fill(0)
.map((_, index) => (
<tr key={index}>
Expand Down Expand Up @@ -207,7 +220,7 @@ export default function MyDenoms({
</td>
</tr>
))
: filteredDenoms.map(denom => (
: paginatedDenoms.map(denom => (
<TokenRow
key={denom.base}
denom={denom}
Expand All @@ -227,6 +240,70 @@ export default function MyDenoms({
))}
</tbody>
</table>
{totalPages > 1 && (
<div
className="flex items-center justify-center gap-2 "
onClick={e => e.stopPropagation()}
role="navigation"
aria-label="Pagination"
>
<button
onClick={e => {
e.stopPropagation();
setCurrentPage(prev => Math.max(1, prev - 1));
}}
disabled={currentPage === 1 || isLoading}
className="p-2 hover:bg-[#FFFFFF1A] rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
aria-label="Previous page"
>
</button>

{[...Array(totalPages)].map((_, index) => {
const pageNum = index + 1;
if (
pageNum === 1 ||
pageNum === totalPages ||
(pageNum >= currentPage - 1 && pageNum <= currentPage + 1)
) {
return (
<button
key={pageNum}
onClick={e => {
e.stopPropagation();
setCurrentPage(pageNum);
}}
className={`w-8 h-8 flex items-center justify-center rounded-lg transition-colors
${currentPage === pageNum ? 'bg-[#FFFFFF1A] text-white' : 'hover:bg-[#FFFFFF1A]'}`}
aria-label={`Page ${pageNum}`}
aria-current={currentPage === pageNum ? 'page' : undefined}
>
{pageNum}
</button>
);
} else if (pageNum === currentPage - 2 || pageNum === currentPage + 2) {
return (
<span key={pageNum} aria-hidden="true">
...
</span>
);
}
return null;
})}

<button
onClick={e => {
e.stopPropagation();
setCurrentPage(prev => Math.min(totalPages, prev + 1));
}}
disabled={currentPage === totalPages || isLoading}
className="p-2 hover:bg-[#FFFFFF1A] rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
aria-label="Next page"
>
</button>
</div>
)}
</div>
<div className="block md:hidden mt-8">
<Link href="/factory/create" passHref>
Expand All @@ -237,12 +314,7 @@ export default function MyDenoms({
</div>
</div>
</div>
<DenomInfoModal
denom={selectedDenom}
modalId="denom-info-modal"
isOpen={!!selectedDenom && !modalType}
onClose={handleCloseModal}
/>
<DenomInfoModal denom={selectedDenom} modalId="denom-info-modal" />
<MintModal
admin={poaAdmin ?? 'manifest1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzsfmy9qj'}
isPoaAdminLoading={isPoaAdminLoading}
Expand Down Expand Up @@ -343,8 +415,8 @@ function TokenRow({
<td className="bg-secondary group-hover:bg-base-300 w-2/5 xl:table-cell hidden">
{truncateString(denom?.name ?? 'No name provided', 20)}
</td>
<td className="bg-secondary group-hover:bg-base-300 w-2/5 sm:w-1/4">
<div className="flex flex-col sm:flex-row sm:items-center">
<td className="bg-secondary group-hover:bg-base-300 w-2/5 md:table-cell hidden sm:w-1/4">
<div className="flex flex-col sm:flex-row sm:items-center ">
<span className="sm:mr-2">{formatAmount(totalSupply)}</span>
<span className="font-extralight">
{truncateString(denom?.display ?? 'No ticker provided', 10).toUpperCase()}
Expand Down
4 changes: 1 addition & 3 deletions components/groups/components/myGroups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function YourGroups({
</thead>
<tbody className="space-y-4" role="rowgroup">
{isLoading
? Array(8)
? Array(isMobile ? 6 : 8)
.fill(0)
.map((_, index) => (
<tr key={index} data-testid="skeleton-row">
Expand Down Expand Up @@ -325,8 +325,6 @@ export function YourGroups({
/>
</React.Fragment>
))}

{/* Add pagination controls */}
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion pages/admins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function Admins() {
const isMember = group?.members?.some(member => member?.member?.address === address);
console.log(group, groupByAdmin, isMember);
return (
<div className="min-h-screen relative py-4 px-2 mx-auto text-white mt-12 md:mt-0">
<div className="min-h-screen relative py-4 px-2 mx-auto text-white ">
<Head>
<title>Admins - Alberto</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Expand Down
2 changes: 1 addition & 1 deletion pages/bank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default function Bank() {
</script>
</Head>

<div className="h-[calc(100vh-1.5rem)] py-1 gap-6 flex flex-col w-full lg:flex-row">
<div className="h-[calc(100vh-1.5rem)] py-1 gap-6 flex flex-col w-full lg:flex-row animate-fadeIn">
{!isWalletConnected ? (
<WalletNotConnected
description=" Use the button below to connect your wallet and start interacting with your
Expand Down
2 changes: 1 addition & 1 deletion pages/factory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function Factory() {
const isDataReady = combinedData.length > 0;

return (
<div className="min-h-screen relative py-4 px-2 mx-auto text-white mt-12 md:mt-0">
<div className="min-h-screen relative py-4 px-2 mx-auto text-white ">
<Head>
<title>Factory - Alberto</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Expand Down
2 changes: 1 addition & 1 deletion pages/groups/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Groups() {
const isError = isGroupByMemberError || isProposalsError;

return (
<div className="min-h-screen relative py-4 px-2 mx-auto text-white mt-12 md:mt-0">
<div className="min-h-screen relative py-4 px-2 mx-auto text-white ">
<Head>
<title>Groups - Alberto</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Expand Down

0 comments on commit 20f6fdb

Please sign in to comment.