Skip to content

Commit a61e1fb

Browse files
author
Jicheng Lu
committed
refine agents page pagination
1 parent b60ca93 commit a61e1fb

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

src/lib/common/markdown/Markdown.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
/** @type {boolean} */
1919
export let rawText = false;
2020
21+
/** @type {boolean} */
22+
export let scrollable = false;
23+
2124
const scrollbarId = uuidv4();
2225
2326
const options = {
@@ -33,7 +36,9 @@
3336
};
3437
3538
onMount(() => {
36-
initScrollbar();
39+
if (scrollable) {
40+
initScrollbar();
41+
}
3742
});
3843
3944
function initScrollbar() {

src/lib/helpers/utils/common.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,22 @@ export function removeDuplicates(arr, key) {
8181
/**
8282
* @param {(string | null)[]} args
8383
*/
84-
export const classnames = (...args) => args.filter(Boolean).join(' ');
84+
export const classnames = (...args) => args.filter(Boolean).join(' ');
85+
86+
87+
/**
88+
* @param {URL} url
89+
* @param {import('$commonTypes').KeyValuePair[]} pairs
90+
* @param {() => void} [callback]
91+
*/
92+
export function setUrlQueryParams(url, pairs, callback) {
93+
if (!pairs?.length) {
94+
return;
95+
}
96+
97+
pairs?.map(p => {
98+
url.searchParams.set(p.key, p.value);
99+
});
100+
101+
callback?.();
102+
}

src/routes/page/agent/+page.svelte

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { _ } from 'svelte-i18n';
44
import { goto } from '$app/navigation';
55
import Swal from 'sweetalert2';
6+
import { page } from '$app/stores';
67
import { Button, Col, Row } from '@sveltestrap/sveltestrap';
78
import Breadcrumb from '$lib/common/Breadcrumb.svelte';
89
import HeadTitle from '$lib/common/HeadTitle.svelte';
@@ -15,6 +16,7 @@
1516
import { ADMIN_ROLES } from '$lib/helpers/constants';
1617
import { globalEventStore } from '$lib/helpers/store';
1718
import CardAgent from './card-agent.svelte';
19+
import { setUrlQueryParams } from '$lib/helpers/utils/common';
1820
1921
2022
const firstPage = 1;
@@ -32,7 +34,7 @@
3234
};
3335
3436
/** @type {import('$agentTypes').AgentFilter} */
35-
let filter = { ... initFilter };
37+
let filter = { ...initFilter };
3638
3739
/** @type {import('$commonTypes').Pagination} */
3840
let pager = filter.pager;
@@ -57,6 +59,21 @@
5759
let selectedAgentLabels = [];
5860
5961
onMount(async () => {
62+
const { pageNum, pageSizeNum } = getQueryParams();
63+
setUrlQueryParams($page.url, [
64+
{ key: 'page', value: `${pageNum}` },
65+
{ key: 'pageSize', value: `${pageSizeNum}` }
66+
], () => replaceUrl());
67+
68+
filter = {
69+
...filter,
70+
pager: {
71+
...filter.pager,
72+
page: pageNum,
73+
size: pageSizeNum
74+
}
75+
};
76+
6077
user = await myInfo();
6178
getPagedAgents();
6279
getAgentLabelOptions();
@@ -70,6 +87,11 @@
7087
labels: selectedAgentLabels?.length > 0 ? selectedAgentLabels : null,
7188
similarName: event.payload || null
7289
};
90+
91+
setUrlQueryParams($page.url, [
92+
{ key: 'page', value: `${filter.pager.page}` }
93+
], () => () => replaceUrl());
94+
7395
getPagedAgents();
7496
});
7597
});
@@ -131,6 +153,22 @@
131153
refreshPager(agents.count, filter.pager.page, filter.pager.size);
132154
}
133155
156+
function getQueryParams() {
157+
const pNum = Number($page.url.searchParams.get('page'));
158+
const pSize = Number($page.url.searchParams.get('pageSize'));
159+
const pageNum = pNum > 0 ? pNum : firstPage;
160+
const pageSizeNum = pSize > 0 ? Math.min(pSize, 30) : pageSize;
161+
162+
return {
163+
pageNum,
164+
pageSizeNum
165+
};
166+
}
167+
168+
function replaceUrl() {
169+
goto(`${$page.url.pathname}${$page.url.search}`, { replaceState: true });
170+
}
171+
134172
function refreshAgents() {
135173
agents = {
136174
items: agents?.items?.map(t => { return { ...t }; }) || [],
@@ -148,12 +186,16 @@
148186
}
149187
150188
/**
151-
* @param {number} pageNum
189+
* @param {number} pn
152190
*/
153-
function pageTo(pageNum) {
191+
function pageTo(pn) {
192+
setUrlQueryParams($page.url, [
193+
{ key: 'page', value: `${pn}` }
194+
], () => replaceUrl());
195+
154196
pager = {
155197
...pager,
156-
page: pageNum
198+
page: pn
157199
};
158200
159201
filter = {

src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439
>
440440
<Markdown
441441
rawText
442+
scrollable
442443
containerClasses={'markdown-div'}
443444
containerStyles={`max-width: ${Math.floor(windowWidth*0.55)}px;`}
444445
text={description}

0 commit comments

Comments
 (0)