Skip to content

Commit ed0d1fc

Browse files
committed
feature: pagination
1 parent 2599dcb commit ed0d1fc

File tree

5 files changed

+114
-4
lines changed

5 files changed

+114
-4
lines changed

src/Campaigns/Blocks/CampaignList/app/index.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {__} from '@wordpress/i18n';
22
import {useState} from '@wordpress/element';
33
import useCampaigns from '../../shared/hooks/useCampaigns';
4+
import Pagination from '../../shared/components/Pagination';
45
import {CampaignListType} from '../types';
56
import {getGoalDescription, getGoalFormattedValue} from '../../CampaignGoal/utils';
67

@@ -20,7 +21,7 @@ const getGridSettings = (layout: string) => {
2021
export default ({attributes}: { attributes: CampaignListType }) => {
2122
const [page, setPage] = useState(1);
2223

23-
const {campaigns, hasResolved} = useCampaigns({
24+
const {campaigns, hasResolved, totalPages} = useCampaigns({
2425
ids: attributes?.filterBy?.map((item: { value: string }) => Number(item.value)),
2526
per_page: attributes?.perPage,
2627
page,
@@ -87,9 +88,9 @@ export default ({attributes}: { attributes: CampaignListType }) => {
8788
))}
8889
</div>
8990

90-
{campaigns.length > 0 && attributes.showPagination && (
91-
<div>
92-
Pagination
91+
{attributes.showPagination && totalPages >= page && (
92+
<div className="give-campaigns-campaignListBlock-grid__pagination">
93+
<Pagination currentPage={page} totalPages={totalPages} setPage={(number) => setPage(number)} />
9394
</div>
9495
)}
9596
</>

src/Campaigns/Blocks/CampaignList/app/styles.scss

+4
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@
9292
}
9393
}
9494
}
95+
96+
&__pagination {
97+
margin-top: 16px;
98+
}
9599
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {__} from '@wordpress/i18n';
2+
3+
import './styles.scss';
4+
5+
export default ({currentPage, totalPages, setPage, disabled = false}: PaginationProps) => {
6+
if (1 >= totalPages) {
7+
return null;
8+
}
9+
10+
const nextPage = currentPage + 1;
11+
const previousPage = currentPage - 1;
12+
13+
return (
14+
<div className="give-campaign-components-pagination">
15+
<div className="give-campaign-components-pagination__pages">
16+
<div className="give-campaign-components-pagination__pages-links">
17+
{previousPage > 0 ? (
18+
<>
19+
<a
20+
href="#"
21+
title={__('Previous page', 'give')}
22+
onClick={(e) => {
23+
e.preventDefault();
24+
setPage(previousPage);
25+
}}
26+
>
27+
28+
</a>
29+
</>
30+
) : (
31+
<span className="give-campaign-components-pagination__pages-links-disabled"></span>
32+
)}
33+
34+
<span className="give-campaign-components-pagination__pages-links-info">
35+
<span>{currentPage}</span>
36+
{__('of', 'give')}
37+
<span>{totalPages}</span>
38+
</span>
39+
40+
{nextPage <= totalPages ? (
41+
<>
42+
<a
43+
href="#"
44+
title={__('Next page', 'give')}
45+
onClick={(e) => {
46+
e.preventDefault();
47+
setPage(nextPage);
48+
}}
49+
>
50+
51+
</a>
52+
</>
53+
) : (
54+
<span className="give-campaign-components-pagination__pages-links-disabled"></span>
55+
)}
56+
</div>
57+
</div>
58+
</div>
59+
);
60+
}
61+
62+
interface PaginationProps {
63+
currentPage: number,
64+
totalPages: number,
65+
setPage: (page: number) => void,
66+
disabled?: boolean
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.give-campaign-components-pagination {
2+
display: flex;
3+
padding: 8px 0;
4+
flex-direction: row;
5+
6+
&__pages {
7+
display: flex;
8+
9+
&-links {
10+
display: flex;
11+
flex-direction: row;
12+
gap: 4px;
13+
14+
a,
15+
span,
16+
&-info {
17+
padding: 6px 12px;
18+
text-decoration: none !important;
19+
background-color: #f6f6f6;
20+
color: #363636;
21+
border-radius: 4px;
22+
23+
span {
24+
padding: 0 4px;
25+
}
26+
}
27+
28+
&-disabled {
29+
opacity: 0.7;
30+
cursor: not-allowed;
31+
}
32+
}
33+
}
34+
}

src/Campaigns/Blocks/shared/hooks/useCampaigns.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default function useCampaigns({ids = [], page = 1, per_page = 30, status
1313

1414
return {
1515
campaigns: data?.records as Campaign[],
16+
//@ts-ignore
17+
totalItems: data.totalItems,
18+
//@ts-ignore
19+
totalPages: data.totalPages,
1620
hasResolved: data?.hasResolved,
1721
};
1822
}

0 commit comments

Comments
 (0)