Skip to content

Commit 14f1913

Browse files
authored
Merge pull request #374 from devtron-labs/fix/plugin-policy-uat
fix: group env options by cluster
2 parents e3bb51b + 5871194 commit 14f1913

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.5.8-beta-5",
3+
"version": "0.5.8-beta-8",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Hooks/useGetResourceKindsOptions/service.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import { ResponseType, Teams } from '@Common/Types'
1818
import { getTeamListMin } from '@Common/Common.service'
1919
import { get } from '@Common/Api'
2020
import { ClusterType } from '@Shared/Services'
21-
import { EnvironmentType, EnvListMinDTO } from '@Shared/types'
21+
import { EnvListMinDTO } from '@Shared/types'
2222
import { EnvironmentTypeEnum } from '@Shared/constants'
2323
import { ROUTES } from '@Common/Constants'
2424
import { stringComparatorBySortOrder } from '@Shared/Helpers'
25-
import { AppsGroupedByProjectsType, ClusterDTO } from './types'
25+
import { AppsGroupedByProjectsType, ClusterDTO, EnvironmentsGroupedByClustersType } from './types'
2626

2727
export const getAppOptionsGroupedByProjects = async (): Promise<AppsGroupedByProjectsType> => {
2828
const { result } = (await get(ROUTES.APP_LIST_MIN)) as ResponseType<AppsGroupedByProjectsType>
@@ -70,30 +70,47 @@ export const getClusterOptions = async (): Promise<ClusterType[]> => {
7070
.sort((a, b) => stringComparatorBySortOrder(a.name, b.name))
7171
}
7272

73-
export const getEnvironmentOptions = async (): Promise<EnvironmentType[]> => {
73+
export const getEnvironmentOptionsGroupedByClusters = async (): Promise<EnvironmentsGroupedByClustersType> => {
7474
const { result } = (await get(ROUTES.ENVIRONMENT_LIST_MIN)) as ResponseType<EnvListMinDTO[]>
7575

7676
if (!result) {
7777
return []
7878
}
7979

80-
return result
81-
.map(
82-
({
83-
id,
84-
environment_name: name,
85-
isVirtualEnvironment,
86-
cluster_name: cluster,
87-
default: isDefault,
88-
namespace,
89-
}) => ({
90-
id,
91-
name,
92-
isVirtual: isVirtualEnvironment ?? false,
93-
cluster,
94-
environmentType: isDefault ? EnvironmentTypeEnum.production : EnvironmentTypeEnum.nonProduction,
95-
namespace,
96-
}),
97-
)
98-
.sort((a, b) => stringComparatorBySortOrder(a.name, b.name))
80+
const sortedEnvList = result.map(
81+
({
82+
id,
83+
environment_name: name,
84+
isVirtualEnvironment,
85+
cluster_name: cluster,
86+
default: isDefault,
87+
namespace,
88+
}) => ({
89+
id,
90+
name,
91+
isVirtual: isVirtualEnvironment ?? false,
92+
cluster,
93+
environmentType: isDefault ? EnvironmentTypeEnum.production : EnvironmentTypeEnum.nonProduction,
94+
namespace,
95+
}),
96+
)
97+
98+
const envGroupedByCluster = Object.values(
99+
sortedEnvList.reduce<
100+
Record<EnvironmentsGroupedByClustersType[number]['clusterName'], EnvironmentsGroupedByClustersType[number]>
101+
>((acc, env) => {
102+
if (!acc[env.cluster]) {
103+
acc[env.cluster] = {
104+
clusterName: env.cluster,
105+
envList: [],
106+
}
107+
}
108+
109+
acc[env.cluster].envList.push(env)
110+
111+
return acc
112+
}, {}),
113+
).sort((a, b) => stringComparatorBySortOrder(a.clusterName, b.clusterName))
114+
115+
return envGroupedByCluster
99116
}

src/Shared/Hooks/useGetResourceKindsOptions/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
// ====== Service Types: Start ====== //
1818

19-
import { ResourceKindType } from '@Shared/types'
19+
import { EnvironmentType, ResourceKindType } from '@Shared/types'
2020
import { ServerErrors } from '@Common/ServerError'
21-
import { getAppOptionsGroupedByProjects, getClusterOptions, getEnvironmentOptions, getProjectOptions } from './service'
21+
import { getAppOptionsGroupedByProjects, getClusterOptions, getProjectOptions } from './service'
2222

2323
export interface AppType {
2424
name: string
@@ -30,6 +30,11 @@ export type AppsGroupedByProjectsType = {
3030
appList: AppType[]
3131
}[]
3232

33+
export type EnvironmentsGroupedByClustersType = {
34+
clusterName: EnvironmentType['cluster']
35+
envList: EnvironmentType[]
36+
}[]
37+
3338
export interface ClusterDTO {
3439
id: number
3540
cluster_name: string
@@ -54,7 +59,7 @@ export interface UseGetResourceKindOptionsReturnType {
5459
[ResourceKindType.devtronApplication]: Awaited<ReturnType<typeof getAppOptionsGroupedByProjects>>
5560
[ResourceKindType.project]: Awaited<ReturnType<typeof getProjectOptions>>
5661
[ResourceKindType.cluster]: Awaited<ReturnType<typeof getClusterOptions>>
57-
[ResourceKindType.environment]: Awaited<ReturnType<typeof getEnvironmentOptions>>
62+
[ResourceKindType.environment]: EnvironmentsGroupedByClustersType
5863
}
5964
resourcesOptionsError: ServerErrors
6065
refetchResourcesOptions: () => void

src/Shared/Hooks/useGetResourceKindsOptions/useGetResourceKindsOptions.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import { useMemo } from 'react'
1818
import { ResourceKindType } from '@Shared/types'
1919
import { useAsync } from '@Common/Helper'
20-
import { getAppOptionsGroupedByProjects, getClusterOptions, getEnvironmentOptions, getProjectOptions } from './service'
20+
import {
21+
getAppOptionsGroupedByProjects,
22+
getClusterOptions,
23+
getEnvironmentOptionsGroupedByClusters,
24+
getProjectOptions,
25+
} from './service'
2126
import { UseGetResourceKindOptionsReturnType, UseGetResourceKindsOptionsProps } from './types'
2227
import { getResourcesToFetchMap } from './utils'
2328

@@ -43,7 +48,7 @@ const useGetResourceKindsOptions = ({
4348
resourcesToFetchMap[ResourceKindType.devtronApplication] ? getAppOptionsGroupedByProjects() : null,
4449
resourcesToFetchMap[ResourceKindType.project] ? getProjectOptions() : null,
4550
resourcesToFetchMap[ResourceKindType.cluster] ? getClusterOptions() : null,
46-
resourcesToFetchMap[ResourceKindType.environment] ? getEnvironmentOptions() : null,
51+
resourcesToFetchMap[ResourceKindType.environment] ? getEnvironmentOptionsGroupedByClusters() : null,
4752
]),
4853
[resourcesToFetchMap],
4954
resourcesToFetch.length > 0,

0 commit comments

Comments
 (0)