Skip to content

Commit 1fb0923

Browse files
susnuxbackportbot[bot]
authored andcommitted
feat: add pagination to the frontend
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> [skip ci]
1 parent 3d4841c commit 1fb0923

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

src/settings/Api.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ export class Api {
1515
return generateUrl(`apps/groupfolders/${endpoint}`)
1616
}
1717

18-
async listFolders(): Promise<Folder[]> {
19-
const response = await axios.get<OCSResponse<Folder[]>>(this.getUrl('folders'))
18+
async listFolders(offset = 0, limit?: number): Promise<Folder[]> {
19+
const response = await axios.get<OCSResponse<Folder[]>>(this.getUrl('folders'), {
20+
params: {
21+
offset,
22+
limit,
23+
},
24+
})
2025
return Object.keys(response.data.ocs.data).map(id => response.data.ocs.data[id])
2126
}
2227

src/settings/App.scss

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,17 @@
178178
}
179179
}
180180
}
181-
}
181+
}
182+
183+
.groupfolders-pagination__list {
184+
display: flex;
185+
gap: var(--default-grid-baseline);
186+
justify-content: center;
187+
}
188+
189+
.groupfolders-pagination__button {
190+
height: var(--default-clickable-area);
191+
}
182192

183193
}
184194

src/settings/App.tsx

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const defaultQuotaOptions = {
2828
Unlimited: -3,
2929
}
3030

31+
const pageSize = 50
32+
3133
export type SortKey = 'mount_point' | 'quota' | 'groups' | 'acl';
3234

3335
export interface AppState {
@@ -45,6 +47,7 @@ export interface AppState {
4547
sortOrder: number;
4648
isAdminNextcloud: boolean;
4749
checkAppsInstalled: boolean;
50+
currentPage: number;
4851
}
4952

5053
export class App extends Component<unknown, AppState> implements OC.Plugin<OC.Search.Core> {
@@ -66,10 +69,12 @@ export class App extends Component<unknown, AppState> implements OC.Plugin<OC.Se
6669
sortOrder: 1,
6770
isAdminNextcloud: false,
6871
checkAppsInstalled: false,
72+
currentPage: 0,
6973
}
7074

7175
componentDidMount() {
72-
this.api.listFolders().then((folders) => {
76+
// list first pageSize + 1 folders so we know if there are more pages
77+
this.api.listFolders(0, pageSize + 1).then((folders) => {
7378
this.setState({ folders })
7479
})
7580
this.api.listGroups().then((groups) => {
@@ -169,6 +174,21 @@ export class App extends Component<unknown, AppState> implements OC.Plugin<OC.Se
169174
this.api.setACL(folder.id, acl)
170175
}
171176

177+
async goToPage(page: number) {
178+
const loadedPage = Math.floor(this.state.folders.length / pageSize)
179+
if (loadedPage <= page) {
180+
const folders = await this.api.listFolders(this.state.folders.length, (page + 1) * pageSize - this.state.folders.length + 1)
181+
this.setState({
182+
folders: [...this.state.folders, ...folders],
183+
currentPage: page,
184+
})
185+
} else {
186+
this.setState({
187+
currentPage: page,
188+
})
189+
}
190+
}
191+
172192
onSortClick = (sort: SortKey) => {
173193
if (this.state.sort === sort) {
174194
this.setState({ sortOrder: -this.state.sortOrder })
@@ -361,6 +381,60 @@ export class App extends Component<unknown, AppState> implements OC.Plugin<OC.Se
361381
</tr>
362382
</FlipMove>
363383
</table>
384+
<nav className="groupfolders-pagination" aria-label={t('groupfolders', 'Pagination of team folders')}>
385+
<ul className="groupfolders-pagination__list">
386+
<li>
387+
<button
388+
aria-label={t('groupfolders', 'Previous')}
389+
className="groupfolders-pagination__button"
390+
disabled={this.state.currentPage === 0}
391+
title={t('groupfolders', 'Previous')}
392+
onClick={() => this.goToPage(this.state.currentPage - 1)}></button>
393+
</li>
394+
{
395+
// show the "1" button if we are not on the first page
396+
this.state.currentPage > 0 && <li><button onClick={() => this.goToPage(0)}>1</button></li>
397+
}
398+
{
399+
// show the ellipsis button if there are more than 2 pages before the current
400+
this.state.currentPage > 2 && <li><button disabled>&#8230;</button></li>}
401+
{
402+
// show the page right before the current - if there is such a page
403+
this.state.currentPage > 1 && <li><button onClick={() => this.goToPage(this.state.currentPage - 1)}>{this.state.currentPage}</button></li>
404+
}
405+
{ /* the current page as a button */}
406+
<li><button aria-current="page" aria-disabled className="primary">{this.state.currentPage + 1}</button></li>
407+
{
408+
// show the next page if it exists (we know at least that the next exists or not)
409+
(this.state.currentPage + 1) < (this.state.folders.length / pageSize)
410+
&& <li>
411+
<button onClick={() => this.goToPage(this.state.currentPage + 1)}>{this.state.currentPage + 2}</button>
412+
</li>
413+
}
414+
{
415+
// If we know more than two next pages exist we show the ellipsis for the intermediate pages
416+
(this.state.currentPage + 3) < (this.state.folders.length / pageSize)
417+
&& <li>
418+
<button disabled>&#8230;</button>
419+
</li>
420+
}
421+
{
422+
// If more than one next page exist we show the last page as a button
423+
(this.state.currentPage + 2) < (this.state.folders.length / pageSize)
424+
&& <li>
425+
<button onClick={() => this.goToPage(Math.floor(this.state.folders.length / pageSize))}>{Math.floor(this.state.folders.length / pageSize) + 1}</button>
426+
</li>
427+
}
428+
<li>
429+
<button
430+
aria-label={t('groupfolders', 'Next')}
431+
className="groupfolders-pagination__button"
432+
disabled={this.state.currentPage >= Math.floor(this.state.folders.length / pageSize)}
433+
title={t('groupfolders', 'Next')}
434+
onClick={() => this.goToPage(this.state.currentPage + 1)}></button>
435+
</li>
436+
</ul>
437+
</nav>
364438
</div>
365439
}
366440

0 commit comments

Comments
 (0)