Skip to content

Commit 63dd73d

Browse files
authored
feat(services): filter routes/stream_routes by service_id (#3111)
* feat(services/routes): filter by `service_id` * feat(services): filter stream_routes by service_id * fix: type
1 parent 7fd10a9 commit 63dd73d

File tree

8 files changed

+50
-12
lines changed

8 files changed

+50
-12
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ 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 WithServiceIdFilter = PageSearchType & {
25+
filter?: {
26+
service_id?: string;
27+
};
28+
};
29+
30+
export const getRouteListReq = (req: AxiosInstance, params: WithServiceIdFilter) =>
2531
req
2632
.get<undefined, APISIXType['RespRouteList']>(API_ROUTES, { params })
2733
.then((v) => v.data);

src/apis/stream_routes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ import type { AxiosInstance } from 'axios';
2020
import type { StreamRoutePostType } from '@/components/form-slice/FormPartStreamRoute/schema';
2121
import { API_STREAM_ROUTES } from '@/config/constant';
2222
import type { APISIXType } from '@/types/schema/apisix';
23-
import type { PageSearchType } from '@/types/schema/pageSearch';
2423

25-
export const getStreamRouteListReq = (req: AxiosInstance, params: PageSearchType) =>
24+
import type { WithServiceIdFilter } from './routes';
25+
26+
export const getStreamRouteListReq = (
27+
req: AxiosInstance,
28+
params: WithServiceIdFilter
29+
) =>
2630
req
2731
.get<unknown, APISIXType['RespStreamRouteList']>(API_STREAM_ROUTES, {
2832
params,

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 { WithServiceIdFilter } 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<WithServiceIdFilter>;
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"

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ function StreamRouteComponent() {
3939
params={{ id, routeId: record.value.id }}
4040
/>
4141
)}
42+
defaultParams={{
43+
filter: {
44+
service_id: id,
45+
},
46+
}}
4247
/>
4348
</>
4449
);

src/routes/stream_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 { getStreamRouteListQueryOptions, useStreamRouteList } from '@/apis/hooks';
24+
import type { WithServiceIdFilter } 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';
@@ -39,11 +40,15 @@ export type StreamRouteListProps = {
3940
ToDetailBtn: (props: {
4041
record: APISIXType['RespStreamRouteItem'];
4142
}) => React.ReactNode;
43+
defaultParams?: Partial<WithServiceIdFilter>;
4244
};
4345

4446
export const StreamRouteList = (props: StreamRouteListProps) => {
45-
const { routeKey, ToDetailBtn } = props;
46-
const { data, isLoading, refetch, pagination } = useStreamRouteList(routeKey);
47+
const { routeKey, ToDetailBtn, defaultParams } = props;
48+
const { data, isLoading, refetch, pagination } = useStreamRouteList(
49+
routeKey,
50+
defaultParams
51+
);
4752
const { t } = useTranslation();
4853

4954
const columns = useMemo<

0 commit comments

Comments
 (0)