mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
refactor(docker/containers): migrate networks table to react [EE-4665] (#10069)
This commit is contained in:
parent
776f6a62c3
commit
b15812a74d
28 changed files with 632 additions and 259 deletions
59
app/react/docker/networks/queries/useNetworks.ts
Normal file
59
app/react/docker/networks/queries/useNetworks.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
import { useQuery } from 'react-query';
|
||||
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
|
||||
import { buildUrl } from '../../proxy/queries/build-url';
|
||||
import { DockerNetwork } from '../types';
|
||||
|
||||
import { queryKeys } from './queryKeys';
|
||||
import { NetworksQuery } from './types';
|
||||
|
||||
export function useNetworks<T = Array<DockerNetwork>>(
|
||||
environmentId: EnvironmentId,
|
||||
query: NetworksQuery,
|
||||
{
|
||||
enabled = true,
|
||||
onSuccess,
|
||||
select,
|
||||
}: {
|
||||
enabled?: boolean;
|
||||
onSuccess?(networks: T): void;
|
||||
select?(networks: Array<DockerNetwork>): T;
|
||||
} = {}
|
||||
) {
|
||||
return useQuery(
|
||||
queryKeys.list(environmentId, query),
|
||||
() => getNetworks(environmentId, query),
|
||||
{ enabled, onSuccess, select }
|
||||
);
|
||||
}
|
||||
|
||||
export async function getNetworks(
|
||||
environmentId: EnvironmentId,
|
||||
{ local, swarm, swarmAttachable, filters }: NetworksQuery
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<Array<DockerNetwork>>(
|
||||
buildUrl(environmentId, 'networks'),
|
||||
filters && {
|
||||
params: {
|
||||
filters,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return !local && !swarm && !swarmAttachable
|
||||
? data
|
||||
: data.filter(
|
||||
(network) =>
|
||||
(local && network.Scope === 'local') ||
|
||||
(swarm && network.Scope === 'swarm') ||
|
||||
(swarmAttachable &&
|
||||
network.Scope === 'swarm' &&
|
||||
network.Attachable === true)
|
||||
);
|
||||
} catch (err) {
|
||||
throw parseAxiosError(err as Error, 'Unable to retrieve networks');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue