From 4c56892436ec84d37e61ef2bd7ccd04663367163 Mon Sep 17 00:00:00 2001 From: Bowen Tan Date: Fri, 13 Oct 2023 15:26:34 +0800 Subject: [PATCH] use useList to get pods and jobs --- .../src/components/CronjobJobsTable/index.tsx | 34 ++++++++----------- .../WorkloadPodsTable/WorkloadPodsTable.tsx | 34 ++++++++----------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/packages/refine/src/components/CronjobJobsTable/index.tsx b/packages/refine/src/components/CronjobJobsTable/index.tsx index 71f4d0f6..8c41217f 100644 --- a/packages/refine/src/components/CronjobJobsTable/index.tsx +++ b/packages/refine/src/components/CronjobJobsTable/index.tsx @@ -1,9 +1,9 @@ import { useUIKit } from '@cloudtower/eagle'; import { css } from '@linaria/core'; -import { useDataProvider, useParsed } from '@refinedev/core'; +import { useList } from '@refinedev/core'; import { Job } from 'kubernetes-types/batch/v1'; import { OwnerReference } from 'kubernetes-types/meta/v1'; -import React, { useEffect, useState } from 'react'; +import React, { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { AgeColumnRenderer, @@ -42,26 +42,22 @@ export const CronjobJobsTable: React.FC<{ owner?: OwnerReference & { namespace: string }; }> = ({ owner }) => { const kit = useUIKit(); - const dataProvider = useDataProvider()(); - const { id } = useParsed(); const { i18n } = useTranslation(); - const [jobs, setJobs] = useState(undefined); const [selectedKeys, setSelectedKeys] = useState([]); const [currentPage, setCurrentPage] = useState(1); - useEffect(() => { - dataProvider - .getList({ resource: 'jobs', meta: { resourceBasePath: '/apis/batch/v1' } }) - .then(res => { - setJobs( - res.data - .map(p => new JobModel(p as WithId)) - .filter(p => { - return owner ? matchOwner(p, owner) : true; - }) - ); + const { data } = useList({ + resource: 'jobs', + meta: { resourceBasePath: '/apis/batch/v1', kind: 'Job' }, + }); + + const dataSource = useMemo(() => { + return data?.data + .map(p => new JobModel(p as WithId)) + .filter(p => { + return owner ? matchOwner(p, owner) : true; }); - }, [dataProvider, id, owner]); + }, [data?.data, owner]); const columns: Column[] = [ PhaseColumnRenderer(i18n), @@ -82,8 +78,8 @@ export const CronjobJobsTable: React.FC<{ > setSelectedKeys(keys as string[])} rowKey="id" diff --git a/packages/refine/src/components/WorkloadPodsTable/WorkloadPodsTable.tsx b/packages/refine/src/components/WorkloadPodsTable/WorkloadPodsTable.tsx index 979447c3..261620ed 100644 --- a/packages/refine/src/components/WorkloadPodsTable/WorkloadPodsTable.tsx +++ b/packages/refine/src/components/WorkloadPodsTable/WorkloadPodsTable.tsx @@ -1,9 +1,9 @@ import { useUIKit } from '@cloudtower/eagle'; import { css } from '@linaria/core'; -import { useDataProvider, useParsed } from '@refinedev/core'; +import { useList } from '@refinedev/core'; import { Pod } from 'kubernetes-types/core/v1'; import { LabelSelector } from 'kubernetes-types/meta/v1'; -import React, { useEffect, useState } from 'react'; +import React, { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { NameColumnRenderer, @@ -36,26 +36,22 @@ export const WorkloadPodsTable: React.FC<{ selector?: LabelSelector }> = ({ selector, }) => { const kit = useUIKit(); - const dataProvider = useDataProvider()(); - const { id } = useParsed(); const { i18n } = useTranslation(); - const [pods, setPods] = useState(undefined); const [selectedKeys, setSelectedKeys] = useState([]); const [currentPage, setCurrentPage] = useState(1); - useEffect(() => { - dataProvider - .getList({ resource: 'pods', meta: { resourceBasePath: '/api/v1' } }) - .then(res => { - setPods( - res.data - .map(p => new PodModel(p as WithId)) - .filter(p => { - return selector ? matchSelector(p, selector) : true; - }) - ); + const { data } = useList({ + resource: 'pods', + meta: { resourceBasePath: '/api/v1', kind: 'Pod' }, + }); + + const dataSource = useMemo(() => { + return data?.data + .map(p => new PodModel(p as WithId)) + .filter(p => { + return selector ? matchSelector(p, selector) : true; }); - }, [dataProvider, id, selector]); + }, [data?.data, selector]); const columns: Column[] = [ PhaseColumnRenderer(i18n), @@ -74,8 +70,8 @@ export const WorkloadPodsTable: React.FC<{ selector?: LabelSelector }> = ({ >
setSelectedKeys(keys as string[])} rowKey="id"