Skip to content

Commit 7b480f2

Browse files
committed
refactor: ConfigMapSecret and EnvironmentOverride components - streamline state management and improve data fetching with useQuery
1 parent 633aa8b commit 7b480f2

File tree

9 files changed

+38
-95
lines changed

9 files changed

+38
-95
lines changed

src/Pages/Shared/ConfigMapSecret/ConfigMapSecret.wrapper.tsx

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useEffect, useRef } from 'react'
1817
import { useParams } from 'react-router-dom'
1918

20-
import {
21-
abortPreviousRequests,
22-
CMSecretComponentType,
23-
ErrorScreenManager,
24-
getIsRequestAborted,
25-
Progressing,
26-
showError,
27-
useAsync,
28-
} from '@devtron-labs/devtron-fe-common-lib'
19+
import { CMSecretComponentType, ErrorScreenManager, Progressing, useQuery } from '@devtron-labs/devtron-fe-common-lib'
2920

30-
import { ComponentStates } from '@Pages/Shared/EnvironmentOverride/EnvironmentOverrides.types'
3121
import { getAppChartRefForAppAndEnv } from '@Services/service'
3222

3323
import { ConfigMapSecretContainer } from './ConfigMapSecretContainer'
@@ -36,53 +26,37 @@ import { CMSecretWrapperProps } from './types'
3626

3727
export const ConfigMapSecretWrapper = (props: CMSecretWrapperProps) => {
3828
// PROPS
39-
const {
40-
componentType = CMSecretComponentType.ConfigMap,
41-
parentState,
42-
setParentState,
43-
onErrorRedirectURL,
44-
isTemplateView,
45-
} = props
29+
const { componentType = CMSecretComponentType.ConfigMap, onErrorRedirectURL, isTemplateView } = props
4630

4731
// HOOKS
4832
const { appId, envId, name } = useParams<{ appId: string; envId: string; name: string }>()
4933

50-
// REFS
51-
const abortControllerRef = useRef<AbortController>(new AbortController())
52-
53-
// ASYNC CALLS
54-
const [appChartRefLoading, appChartRefRes, appChartRefErr, reload] = useAsync(
55-
() =>
56-
abortPreviousRequests(() => getAppChartRefForAppAndEnv(+appId, +envId, isTemplateView), abortControllerRef),
57-
[componentType],
58-
)
59-
60-
useEffect(() => {
61-
if (appChartRefRes) {
62-
setParentState?.(ComponentStates.loaded)
63-
}
64-
if (appChartRefErr && !getIsRequestAborted(appChartRefErr)) {
65-
setParentState?.(ComponentStates.failed)
66-
showError(appChartRefErr)
67-
}
68-
69-
return () => {
70-
setParentState?.(ComponentStates.loading)
71-
}
72-
}, [appChartRefRes, appChartRefErr])
73-
74-
if (parentState === ComponentStates.loading || appChartRefLoading || getIsRequestAborted(appChartRefErr)) {
34+
const {
35+
data: appChartRef,
36+
isLoading,
37+
isFetching,
38+
error: appChartRefErr,
39+
refetch,
40+
} = useQuery({
41+
queryFn: ({ signal }) => getAppChartRefForAppAndEnv(+appId, +envId, isTemplateView, signal),
42+
queryKey: [componentType, appId, envId, isTemplateView],
43+
select: ({ result }) => result,
44+
})
45+
46+
const appChartRefLoading = isLoading || isFetching
47+
48+
if (appChartRefLoading) {
7549
return <Progressing fullHeight pageLoader />
7650
}
7751

7852
if (appChartRefErr) {
79-
return <ErrorScreenManager code={appChartRefErr.code} redirectURL={onErrorRedirectURL} reload={reload} />
53+
return <ErrorScreenManager code={appChartRefErr.code} redirectURL={onErrorRedirectURL} reload={refetch} />
8054
}
8155

8256
return (
8357
<ConfigMapSecretContainer
8458
key={`${CM_SECRET_COMPONENT_NAME[componentType]}-${name}`}
85-
appChartRef={appChartRefRes?.result}
59+
appChartRef={appChartRef}
8660
{...props}
8761
/>
8862
)

src/Pages/Shared/ConfigMapSecret/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Dispatch, MutableRefObject, SetStateAction } from 'react'
17+
import { MutableRefObject } from 'react'
1818

1919
import {
2020
AppConfigProps,
@@ -36,7 +36,7 @@ import {
3636

3737
import { ConfigToolbarProps } from '@Pages/Applications'
3838

39-
import { ComponentStates, EnvironmentOverrideComponentProps } from '../EnvironmentOverride/EnvironmentOverrides.types'
39+
import { EnvironmentOverrideComponentProps } from '../EnvironmentOverride/EnvironmentOverrides.types'
4040

4141
// PAYLOAD PROPS
4242
export interface CMSecretDraftPayloadType {
@@ -74,8 +74,6 @@ export interface CMSecretWrapperProps
7474
> {
7575
componentType?: CMSecretComponentType
7676
parentName?: string
77-
parentState?: ComponentStates
78-
setParentState?: Dispatch<SetStateAction<ComponentStates>>
7977
clusterId?: string
8078
isApprovalPolicyConfigured?: boolean
8179
envName: string

src/Pages/Shared/EnvironmentOverride/EnvironmentOverride.tsx

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useEffect, useState } from 'react'
17+
import { useEffect } from 'react'
1818
import { generatePath, Route, Switch, useHistory, useLocation, useParams, useRouteMatch } from 'react-router-dom'
1919

2020
import {
2121
ApprovalConfigDataKindType,
2222
CMSecretComponentType,
2323
getIsApprovalPolicyConfigured,
24-
Reload,
2524
} from '@devtron-labs/devtron-fe-common-lib'
2625

2726
import { ErrorBoundary, mapByKey, useAppContext } from '@Components/common'
2827
import { APP_COMPOSE_STAGE, getAppComposeURL, URLS } from '@Config/index'
2928
import { DeploymentTemplate } from '@Pages/Applications'
3029
import { ConfigMapSecretWrapper } from '@Pages/Shared/ConfigMapSecret/ConfigMapSecret.wrapper'
3130

32-
import { ComponentStates, EnvironmentOverrideComponentProps } from './EnvironmentOverrides.types'
31+
import { EnvironmentOverrideComponentProps } from './EnvironmentOverrides.types'
3332

3433
import './environmentOverride.scss'
3534

@@ -48,7 +47,6 @@ const EnvironmentOverride = ({
4847
}: EnvironmentOverrideComponentProps) => {
4948
const isAppGroupView = !!envName
5049
const params = useParams<{ appId: string; envId: string }>()
51-
const [viewState, setViewState] = useState<ComponentStates>(null)
5250
const { path, url } = useRouteMatch()
5351
const { push } = useHistory()
5452
const location = useLocation()
@@ -79,24 +77,9 @@ const EnvironmentOverride = ({
7977
}
8078
}, [])
8179

82-
useEffect(() => {
83-
if (viewState === ComponentStates.reloading) {
84-
reloadEnvironments()
85-
}
86-
}, [viewState])
87-
8880
if (!params.envId) {
8981
return null
9082
}
91-
if (viewState === ComponentStates.failed) {
92-
return (
93-
<Reload
94-
reload={() => {
95-
setViewState(ComponentStates.reloading)
96-
}}
97-
/>
98-
)
99-
}
10083

10184
if (params.envId && !environmentsMap.has(+params.envId) && environments.length) {
10285
const newUrl = url.replace(
@@ -164,9 +147,7 @@ const EnvironmentOverride = ({
164147
isApprovalPolicyConfigured={getIsApprovalPolicyConfigured(
165148
approvalConfigMap?.[ApprovalConfigDataKindType.configMap],
166149
)}
167-
parentState={viewState}
168150
parentName={getParentName()}
169-
setParentState={setViewState}
170151
clusterId={clusterId}
171152
envConfig={envConfig}
172153
fetchEnvConfig={fetchEnvConfig}
@@ -185,9 +166,7 @@ const EnvironmentOverride = ({
185166
isApprovalPolicyConfigured={getIsApprovalPolicyConfigured(
186167
approvalConfigMap?.[ApprovalConfigDataKindType.configSecret],
187168
)}
188-
parentState={viewState}
189169
parentName={getParentName()}
190-
setParentState={setViewState}
191170
clusterId={environmentsMap.get(+params.envId)?.clusterId?.toString()}
192171
componentType={CMSecretComponentType.Secret}
193172
envConfig={envConfig}

src/Pages/Shared/EnvironmentOverride/EnvironmentOverrides.types.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import React from 'react'
18-
1917
import { AppConfigProps } from '@devtron-labs/devtron-fe-common-lib'
2018

2119
import {
@@ -25,14 +23,6 @@ import {
2523

2624
import { ConfigAppList } from '../../../components/ApplicationGroup/AppGroup.types'
2725

28-
export enum ComponentStates {
29-
loading = 'loading',
30-
loaded = 'loaded',
31-
success = 'success',
32-
failed = 'failed',
33-
reloading = 'reloading',
34-
}
35-
3626
export interface EnvironmentOverrideComponentProps extends Required<Pick<AppConfigProps, 'isTemplateView'>> {
3727
appList?: ConfigAppList[]
3828
environments: AppConfigState['environmentList']
@@ -46,12 +36,6 @@ export interface EnvironmentOverrideComponentProps extends Required<Pick<AppConf
4636
appOrEnvIdToResourceApprovalConfigurationMap: AppConfigState['envIdToEnvApprovalConfigurationMap']
4737
}
4838

49-
export interface CommonEnvironmentOverridesProps {
50-
parentState: ComponentStates
51-
setParentState: React.Dispatch<React.SetStateAction<ComponentStates>>
52-
isJobView?: boolean
53-
}
54-
5539
export interface ListComponentType {
5640
name: string
5741
type: string

src/components/CIPipelineN/AdvancedConfigOptions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import CIConfig from '../ciConfig/CIConfig'
2020
import DockerArgs from './DockerArgs'
2121
import CustomImageTags from './CustomImageTags'
2222
import TargetPlatformSelector from '../ciConfig/TargetPlatformSelector'
23-
import { ComponentStates } from '../../Pages/Shared/EnvironmentOverride/EnvironmentOverrides.types'
2423
import { AdvancedConfigOptionsProps, CIConfigParentState } from '../ciConfig/types'
2524
import { DockerConfigOverrideKeys } from '../ciPipeline/types'
2625
import { getTargetPlatformMap } from '../ciConfig/CIConfig.utils'
2726
import { pipelineContext } from '../workflowEditor/workflowEditor'
2827
import '../ciConfig/CIConfig.scss'
28+
import { ComponentStates } from './types'
2929

3030
export default function AdvancedConfigOptions({ ciPipeline, appId, isTemplateView }: AdvancedConfigOptionsProps) {
3131
const { formData, setFormData, loadingState, setLoadingState, formDataErrorObj, setFormDataErrorObj } =

src/components/CIPipelineN/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ export interface EnvironmentListType {
7373
setSelectedEnv?: React.Dispatch<React.SetStateAction<EnvironmentWithSelectPickerType>>
7474
isBorderLess?: boolean
7575
}
76+
77+
export enum ComponentStates {
78+
loading = 'loading',
79+
loaded = 'loaded',
80+
success = 'success',
81+
failed = 'failed',
82+
reloading = 'reloading',
83+
}

src/components/ciConfig/CIConfig.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { showError, Progressing } from '@devtron-labs/devtron-fe-common-lib'
1919
import { getGitProviderIcon, sortObjectArrayAlphabetically } from '../common'
2020
import { getDockerRegistryMinAuth } from './service'
2121
import { getSourceConfig, getCIConfig } from '../../services/service'
22-
import { ComponentStates } from '../../Pages/Shared/EnvironmentOverride/EnvironmentOverrides.types'
22+
import { ComponentStates } from '@Components/CIPipelineN/types'
2323
import { CIConfigProps, MaterialOptionType } from './types'
2424
import './CIConfig.scss'
2525
import CIConfigForm from './CIConfigForm'

src/components/ciConfig/types.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ import {
3232
WorkflowType,
3333
} from '@devtron-labs/devtron-fe-common-lib'
3434

35-
import { EnvironmentWithSelectPickerType } from '@Components/CIPipelineN/types'
35+
import { ComponentStates, EnvironmentWithSelectPickerType } from '@Components/CIPipelineN/types'
3636
import { OptionTypeWithIcon } from '@Components/externalLinks/ExternalLinks.type'
3737

38-
import { ComponentStates } from '../../Pages/Shared/EnvironmentOverride/EnvironmentOverrides.types'
3938
import { ConfigOverrideWorkflowDetails } from '../../services/service.types'
4039
import { CiPipelineResult } from '../app/details/triggerView/types'
4140
import { CIPipelineDataType } from '../ciPipeline/types'

src/services/service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ export function getChartReferencesForAppAndEnv(
430430
appId: number,
431431
envId: number | null,
432432
isTemplateView: AppConfigProps['isTemplateView'],
433+
signal?: AbortSignal,
433434
): Promise<ResponseType<MinChartRefDTO>> {
434435
const url = isTemplateView
435436
? getTemplateAPIRoute({
@@ -440,15 +441,16 @@ export function getChartReferencesForAppAndEnv(
440441
},
441442
})
442443
: `${Routes.CHART_REFERENCES_MIN}/${appId}${envId ? `/${envId}` : ''}`
443-
return get(url)
444+
return get(url, { signal })
444445
}
445446

446447
export function getAppChartRefForAppAndEnv(
447448
appId: number,
448449
envId: number | null,
449450
isTemplateView: AppConfigProps['isTemplateView'],
451+
signal?: AbortSignal
450452
): Promise<ResponseType> {
451-
return getChartReferencesForAppAndEnv(appId, envId, isTemplateView).then((response) => {
453+
return getChartReferencesForAppAndEnv(appId, envId, isTemplateView, signal).then((response) => {
452454
const {
453455
result: { chartRefs, latestEnvChartRef, latestAppChartRef },
454456
} = response
@@ -555,4 +557,3 @@ export const validateContainerConfiguration = (request: any): Promise<any> => {
555557

556558
export const getTemplateOptions = (appId: number, envId: number): Promise<ResponseType<TemplateListDTO[]>> =>
557559
get(getUrlWithSearchParams(Routes.DEPLOYMENT_OPTIONS, { appId, envId }))
558-

0 commit comments

Comments
 (0)