Skip to content

Commit

Permalink
Merge pull request #390 from zhang-accounting/389-journal-pagination-…
Browse files Browse the repository at this point in the history
…optimization

fix: page click event in pagination
  • Loading branch information
Kilerd authored Nov 19, 2024
2 parents ff22a87 + 1a6fdfa commit f513f80
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
5 changes: 4 additions & 1 deletion frontend/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
"LEDGER_IS_HEALTHY": "Ledger is healthy",
"accounts": {
"hide_closed_accounts": "Hide closed accounts"
}
},
"PAGE": "page",
"PAGINATION_PREVIOUS": "Previous",
"PAGINATION_NEXT": "Next"
}
5 changes: 4 additions & 1 deletion frontend/public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@
"TRANSACTION_EDIT_MODAL_SAVE": "保存",
"TRANSACTION_PREVIEW_MODAL_CLOSE": "关闭",
"TRANSACTION_EDIT_MODAL_DESCRIPTION": "编辑流水项",
"TRANSACTION_EDIT_MODAL_TITLE": "编辑流水项"
"TRANSACTION_EDIT_MODAL_TITLE": "编辑流水项",
"PAGE": "",
"PAGINATION_PREVIOUS": "上一页",
"PAGINATION_NEXT": "下一页"
}
33 changes: 20 additions & 13 deletions frontend/src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ChevronLeftIcon, ChevronRightIcon, DotsHorizontalIcon } from '@radix-ui

import { cn } from '@/lib/utils';
import { ButtonProps, buttonVariants } from '@/components/ui/button';
import { useTranslation } from 'react-i18next';

const Pagination = ({ className, ...props }: React.ComponentProps<'nav'>) => (
<nav role="navigation" aria-label="pagination" className={cn('mx-auto flex w-full justify-center', className)} {...props} />
<nav role="navigation" aria-label="pagination" className={cn('mx-auto flex justify-center', className)} {...props} />
);
Pagination.displayName = 'Pagination';

Expand Down Expand Up @@ -39,20 +40,26 @@ const PaginationLink = ({ className, isActive, size = 'icon', ...props }: Pagina
);
PaginationLink.displayName = 'PaginationLink';

const PaginationPrevious = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => (
<PaginationLink aria-label="Go to previous page" size="default" className={cn('gap-1 pl-2.5', className)} {...props}>
<ChevronLeftIcon className="h-4 w-4" />
<span>Previous</span>
</PaginationLink>
);
const PaginationPrevious = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => {
const { t } = useTranslation();
return (
<PaginationLink aria-label="Go to previous page" size="default" className={cn('gap-1 pl-2.5', className)} {...props}>
<ChevronLeftIcon className="h-4 w-4" />
<span>{t('PAGINATION_PREVIOUS')}</span>
</PaginationLink>
);
};
PaginationPrevious.displayName = 'PaginationPrevious';

const PaginationNext = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => (
<PaginationLink aria-label="Go to next page" size="default" className={cn('gap-1 pr-2.5', className)} {...props}>
<span>Next</span>
<ChevronRightIcon className="h-4 w-4" />
</PaginationLink>
);
const PaginationNext = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => {
const { t } = useTranslation();
return (
<PaginationLink aria-label="Go to next page" size="default" className={cn('gap-1 pr-2.5', className)} {...props}>
<span>{t('PAGINATION_NEXT')}</span>
<ChevronRightIcon className="h-4 w-4" />
</PaginationLink>
);
};
PaginationNext.displayName = 'PaginationNext';

const PaginationEllipsis = ({ className, ...props }: React.ComponentProps<'span'>) => (
Expand Down
77 changes: 50 additions & 27 deletions frontend/src/pages/Journals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { X } from 'lucide-react';
import { JOURNALS_LINK } from '@/layout/Sidebar';
import { TransactionPreviewModal } from '@/components/modals/TransactionPreviewModal';
import { TransactionEditModal } from '@/components/modals/TransactionEditModal';

function Journals() {
const setBreadcrumb = useSetAtom(breadcrumbAtom);
const { t } = useTranslation();
Expand Down Expand Up @@ -122,35 +123,57 @@ function Journals() {
</TableBody>
</Table>
</div>
<Pagination className="my-4">
<PaginationContent>
{journalPage > 1 && (
<PaginationItem>
<PaginationPrevious href="#" onClick={() => onPage(journalPage - 1)} />
</PaginationItem>
)}
{journalPage > 1 && (
<PaginationItem>
<PaginationLink href="#">{journalPage - 1}</PaginationLink>
</PaginationItem>
)}
<PaginationItem>
<PaginationLink href="#" isActive>
{journalPage}
</PaginationLink>
</PaginationItem>
{journalPage < total_page && (
<PaginationItem>
<PaginationLink href="#">{journalPage + 1}</PaginationLink>
</PaginationItem>
)}
{journalPage < total_page && (
<div className="flex items-center gap-4 my-4">
<div className={'inline-block'}>
{journalItems.state === 'hasData' ? journalItems.data?.total_page : 0} {t('PAGE')}
</div>
<Pagination>
<PaginationContent>
{journalPage > 1 && (
<PaginationItem>
<PaginationPrevious className="cursor-pointer" onClick={() => onPage(journalPage - 1)} />
</PaginationItem>
)}
{journalPage > 2 && (
<PaginationItem>
<PaginationLink className="cursor-pointer" onClick={() => onPage(journalPage - 2)}>
{journalPage - 2}
</PaginationLink>
</PaginationItem>
)}
{journalPage > 1 && (
<PaginationItem>
<PaginationLink className="cursor-pointer" onClick={() => onPage(journalPage - 1)}>
{journalPage - 1}
</PaginationLink>
</PaginationItem>
)}
<PaginationItem>
<PaginationNext href="#" onClick={() => onPage(journalPage + 1)} />
<PaginationLink isActive>{journalPage}</PaginationLink>
</PaginationItem>
)}
</PaginationContent>
</Pagination>
{journalPage < total_page && (
<PaginationItem>
<PaginationLink className="cursor-pointer" onClick={() => onPage(journalPage + 1)}>
{journalPage + 1}
</PaginationLink>
</PaginationItem>
)}
{journalPage + 1 < total_page && (
<PaginationItem>
<PaginationLink className="cursor-pointer" onClick={() => onPage(journalPage + 2)}>
{journalPage + 2}
</PaginationLink>
</PaginationItem>
)}
{journalPage < total_page && (
<PaginationItem>
<PaginationNext className="cursor-pointer" onClick={() => onPage(journalPage + 1)} />
</PaginationItem>
)}
</PaginationContent>
</Pagination>
<div></div>
</div>
</div>
);
}
Expand Down

0 comments on commit f513f80

Please sign in to comment.