Skip to content

Commit e97fc24

Browse files
committed
Add pagination to fetchAllLocales and add a pagination section to LocaleMenu
1 parent 95a4d24 commit e97fc24

4 files changed

Lines changed: 95 additions & 18 deletions

File tree

‎translate/src/api/other-locales.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,30 @@ export async function fetchOtherLocales(
2929
return Array.isArray(results) ? results : [];
3030
}
3131

32-
export async function fetchAllLocales(): Promise<LocaleOption[]> {
32+
const PAGE_SIZE = 100;
33+
34+
export async function fetchAllLocales(page = 1): Promise<{
35+
results: LocaleOption[];
36+
page: number;
37+
totalPages: number;
38+
hasNext: boolean;
39+
hasPrevious: boolean;
40+
}> {
3341
const search = new URLSearchParams({
3442
fields: 'code,name',
35-
page_size: '1000',
3643
ordering: 'name',
44+
page: String(page),
45+
page_size: String(PAGE_SIZE),
3746
});
38-
const result = await GET('/api/v2/locales/', search);
39-
return Array.isArray(result?.results) ? result.results : [];
47+
48+
const url = '/api/v2/locales/?' + search.toString();
49+
const result = await GET(url);
50+
51+
return {
52+
results: result.results ?? [],
53+
page,
54+
totalPages: Math.ceil(result.count / PAGE_SIZE),
55+
hasNext: result.next !== null,
56+
hasPrevious: result.previous !== null,
57+
};
4058
}

‎translate/src/modules/batchactions/components/BatchActions.tsx‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import './BatchActions.css';
1919
import { RejectAll } from './RejectAll';
2020
import { ReplaceAll } from './ReplaceAll';
2121
import { CopyFromLocale } from './CopyFromLocale';
22-
import { fetchAllLocales } from '~/api/other-locales';
23-
import type { LocaleOption } from '~/api/other-locales';
2422
import LocaleMenu from '~/modules/locale/components/LocaleMenu';
2523
/**
2624
* Renders batch editor, used for performing mass actions on translations.
@@ -35,8 +33,6 @@ export function BatchActions(): React.ReactElement<'div'> {
3533
const replace = useRef<HTMLInputElement>(null);
3634

3735
const [otherLocale, setOtherLocale] = useState('');
38-
const [locales, setLocales] = useState<LocaleOption[]>([]);
39-
4036
const quitBatchActions = useCallback(() => dispatch(resetSelection()), []);
4137

4238
useEffect(() => {
@@ -50,12 +46,6 @@ export function BatchActions(): React.ReactElement<'div'> {
5046
return () => document.removeEventListener('keydown', handleShortcuts);
5147
}, []);
5248

53-
useEffect(() => {
54-
fetchAllLocales().then((all) => {
55-
setLocales(all);
56-
});
57-
}, []);
58-
5949
const selectAllEntities = useCallback(
6050
() => dispatch(selectAll(location)),
6151
[location],
@@ -245,7 +235,6 @@ export function BatchActions(): React.ReactElement<'div'> {
245235
</Localized>
246236
<form id='copy-locale-form' onSubmit={submitCopyFromLocaleForm}>
247237
<LocaleMenu
248-
locales={locales}
249238
currentLocale={location.locale}
250239
selected={otherLocale}
251240
onSelect={setOtherLocale}

‎translate/src/modules/locale/components/LocaleMenu.css‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,36 @@
6060
margin: 0;
6161
padding: 0;
6262
}
63+
64+
.locale-menu-dropdown .pagination {
65+
display: flex;
66+
align-items: center;
67+
justify-content: space-between;
68+
margin-top: 10px;
69+
}
70+
71+
.locale-menu-dropdown .pagination > div {
72+
flex: 1;
73+
}
74+
75+
.locale-menu-dropdown .pagination > p {
76+
flex: 1;
77+
text-align: center;
78+
margin: 0;
79+
}
80+
81+
.locale-menu-dropdown .pagination > div:last-child {
82+
text-align: right;
83+
}
84+
85+
.locale-menu-dropdown .pagination-hidden {
86+
visibility: hidden;
87+
}
88+
89+
.locale-menu-dropdown .pagination .icon {
90+
color: var(--white-1);
91+
font-size: 1.2em;
92+
box-sizing: border-box;
93+
flex-shrink: 0;
94+
padding-left: 6px;
95+
}

‎translate/src/modules/locale/components/LocaleMenu.tsx‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import React, { useState, useRef, useEffect } from 'react';
22
import './LocaleMenu.css';
33
import LocaleItem from './LocaleItem';
4-
import { LocaleOption } from '~/api/other-locales';
4+
import { LocaleOption, fetchAllLocales } from '~/api/other-locales';
55

66
type Props = {
7-
locales: LocaleOption[];
87
currentLocale: string;
98
selected: string;
109
onSelect: (code: string) => void;
1110
};
1211

1312
export default function LocaleMenu({
14-
locales,
1513
currentLocale,
1614
selected,
1715
onSelect,
1816
}: Props) {
1917
const [isOpen, setIsOpen] = useState(false);
2018
const [search, setSearch] = useState('');
2119
const ref = useRef<HTMLDivElement>(null);
20+
const [page, setPage] = useState(1);
21+
const [locales, setLocales] = useState<LocaleOption[]>([]);
22+
const [totalPages, setTotalPages] = useState(1);
23+
const [hasNext, setHasNext] = useState(false);
24+
const [hasPrevious, setHasPrevious] = useState(false);
2225

2326
useEffect(() => {
2427
const handleClickOutside = (e: MouseEvent) => {
@@ -31,6 +34,17 @@ export default function LocaleMenu({
3134
return () => document.removeEventListener('mousedown', handleClickOutside);
3235
}, []);
3336

37+
useEffect(() => {
38+
fetchAllLocales(page).then((result) => {
39+
setLocales(result.results);
40+
setTotalPages(result.totalPages);
41+
setHasNext(result.hasNext);
42+
setHasPrevious(result.hasPrevious);
43+
});
44+
}, [page]);
45+
46+
const isSearching = search.trim() !== '';
47+
3448
const filtered = locales
3549
.filter((locale) => locale.code !== currentLocale)
3650
.filter(
@@ -99,6 +113,29 @@ export default function LocaleMenu({
99113
))}
100114
</ul>
101115
</div>
116+
<div className='pagination'>
117+
{!isSearching && (
118+
<>
119+
<div
120+
className={!hasPrevious ? 'pagination-hidden' : ''}
121+
aria-label='Previous page'
122+
onClick={() => hasPrevious && setPage(page - 1)}
123+
>
124+
<i className='icon fas fa-angle-left'></i>
125+
</div>
126+
<p className='number-pages'>
127+
Page {page} of {totalPages}
128+
</p>
129+
<div
130+
className={!hasNext ? 'pagination-hidden' : ''}
131+
aria-label='Next page'
132+
onClick={() => hasNext && setPage(page + 1)}
133+
>
134+
<i className='icon fas fa-angle-right'></i>
135+
</div>
136+
</>
137+
)}
138+
</div>
102139
</div>
103140
)}
104141
</div>

0 commit comments

Comments
 (0)