Skip to content

Commit 67aa1e4

Browse files
committed
feat(services/routes): filter by service_id
1 parent 7fd10a9 commit 67aa1e4

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

src/apis/hooks.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ export const genUseList = <
8282
routeKey: T,
8383
listQueryOptions: ReturnType<typeof genListQueryOptions<P, R>>
8484
) => {
85-
return (replaceKey?: U) => {
85+
return (replaceKey?: U, defaultParams?: Partial<P>) => {
8686
const key = replaceKey || routeKey;
8787
const { params, setParams } = useSearchParams<T | U, P>(key);
88-
const listQuery = useSuspenseQuery(listQueryOptions(params));
88+
const listQuery = useSuspenseQuery(
89+
listQueryOptions({ ...defaultParams, ...params })
90+
);
8991
const { data, isLoading, refetch } = listQuery;
9092
const opts = { data, setParams, params };
9193
const pagination = useTablePagination(opts);
92-
return { data, isLoading, refetch, pagination };
94+
return { data, isLoading, refetch, pagination, setParams };
9395
};
9496
};
9597

src/apis/routes.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ import { API_ROUTES, PAGE_SIZE_MAX, PAGE_SIZE_MIN } from '@/config/constant';
2121
import type { APISIXType } from '@/types/schema/apisix';
2222
import type { PageSearchType } from '@/types/schema/pageSearch';
2323

24-
export const getRouteListReq = (req: AxiosInstance, params: PageSearchType) =>
24+
export type GetRouteListReqParams = PageSearchType & {
25+
filter?: {
26+
service_id?: string;
27+
};
28+
};
29+
export const getRouteListReq = (
30+
req: AxiosInstance,
31+
params: GetRouteListReqParams
32+
) =>
2533
req
2634
.get<undefined, APISIXType['RespRouteList']>(API_ROUTES, { params })
2735
.then((v) => v.data);

src/config/req.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ import { globalStore } from '@/stores/global';
2929
export const req = axios.create();
3030

3131
req.interceptors.request.use((conf) => {
32-
conf.paramsSerializer = (p) =>
33-
stringify(p, {
32+
conf.paramsSerializer = (p) => {
33+
// from { filter: { service_id: 1 } }
34+
// to `filter=service_id%3D1`
35+
if (p.filter) {
36+
p.filter = stringify(p.filter);
37+
}
38+
return stringify(p, {
3439
arrayFormat: 'repeat',
3540
});
41+
};
3642
conf.baseURL = API_PREFIX;
3743
conf.headers.set(API_HEADER_KEY, globalStore.settings.adminKey);
3844
return conf;

src/routes/routes/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useMemo } from 'react';
2121
import { useTranslation } from 'react-i18next';
2222

2323
import { getRouteListQueryOptions, useRouteList } from '@/apis/hooks';
24+
import type { GetRouteListReqParams } from '@/apis/routes';
2425
import { DeleteResourceBtn } from '@/components/page/DeleteResourceBtn';
2526
import PageHeader from '@/components/page/PageHeader';
2627
import { ToAddPageBtn, ToDetailPageBtn } from '@/components/page/ToAddPageBtn';
@@ -33,14 +34,18 @@ import type { ListPageKeys } from '@/utils/useTablePagination';
3334

3435
export type RouteListProps = {
3536
routeKey: Extract<ListPageKeys, '/routes/' | '/services/detail/$id/routes/'>;
37+
defaultParams?: Partial<GetRouteListReqParams>;
3638
ToDetailBtn: (props: {
3739
record: APISIXType['RespRouteItem'];
3840
}) => React.ReactNode;
3941
};
4042

4143
export const RouteList = (props: RouteListProps) => {
42-
const { routeKey, ToDetailBtn } = props;
43-
const { data, isLoading, refetch, pagination } = useRouteList(routeKey);
44+
const { routeKey, ToDetailBtn, defaultParams } = props;
45+
const { data, isLoading, refetch, pagination } = useRouteList(
46+
routeKey,
47+
defaultParams
48+
);
4449
const { t } = useTranslation();
4550

4651
const columns = useMemo<ProColumns<APISIXType['RespRouteItem']>[]>(() => {

src/routes/services/detail.$id/routes/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ function RouteComponent() {
3232
<PageHeader title={t('sources.routes')} />
3333
<RouteList
3434
routeKey="/services/detail/$id/routes/"
35+
defaultParams={{
36+
filter: {
37+
service_id: id,
38+
},
39+
}}
3540
ToDetailBtn={({ record }) => (
3641
<ToDetailPageBtn
3742
key="detail"

0 commit comments

Comments
 (0)