diff --git a/src/components/create-job-form/index.tsx b/src/components/create-job-form/index.tsx index 7a1626d66..e720c727a 100644 --- a/src/components/create-job-form/index.tsx +++ b/src/components/create-job-form/index.tsx @@ -3,17 +3,23 @@ import * as PropTypes from 'prop-types'; import { type ComponentProps, type FunctionComponent, - type PropsWithChildren, - useState, + useCallback, + useMemo, } from 'react'; -import { PipelineFormPromote } from './pipeline-form-promote'; - -import './style.css'; import { useSearchParams } from 'react-router-dom'; +import { pollingInterval } from '../../store/defaults'; +import { + type Application, + useGetApplicationQuery, +} from '../../store/radix-api'; import { PipelineFormApplyConfig } from './pipeline-form-apply-config'; import { PipelineFormBuildBranches } from './pipeline-form-build-branches'; import { PipelineFormDeploy } from './pipeline-form-deploy'; +import { PipelineFormPromote } from './pipeline-form-promote'; +import { useGetApplicationBranches } from './use-get-application-branches'; +import './style.css'; +import AsyncResource from '../async-resource/async-resource'; export interface CreateJobFormProps { appName: string; @@ -25,11 +31,11 @@ type SupportedPipelineNames = | ComponentProps['pipelineName'] | ComponentProps['pipelineName']; -export type FormProp = PropsWithChildren<{ - appName: string; +export type FormProp = { + application: Application; onSuccess: (jobName: string) => void; pipelineName: string; -}>; +}; const Pipelines = { build: PipelineFormBuildBranches, @@ -39,28 +45,53 @@ const Pipelines = { 'apply-config': PipelineFormApplyConfig, } satisfies Record>; +const pipelineSearchParam = 'pipeline'; + export default function CreateJobForm({ appName, onSuccess, }: CreateJobFormProps) { - const [searchParams] = useSearchParams(); - const [pipeline, setPipeline] = useState(() => { - const urlPipeline = searchParams.get('pipeline'); + const { data: application, ...appState } = useGetApplicationQuery( + { appName }, + { pollingInterval } + ); + + const hasEnvironments = useMemo( + () => application?.environments?.length > 0, + [application] + ); + const buildBranches = useGetApplicationBranches(application); + const hasBuildBranches = useMemo( + () => Object.keys(buildBranches).length > 0, + [buildBranches] + ); + const [searchParams, setSearchParams] = useSearchParams(); + const setPipelineType = useCallback( + (pipeline: string) => { + setSearchParams((prev) => { + prev.set(pipelineSearchParam, pipeline); + return prev; + }); + }, + [setSearchParams] + ); + const pipeline = useMemo(() => { + const urlPipeline = searchParams.get(pipelineSearchParam); if (Object.keys(Pipelines).includes(urlPipeline)) { - return urlPipeline as SupportedPipelineNames; + return urlPipeline; } - return 'build-deploy'; - }); + return !hasEnvironments + ? 'apply-config' + : hasBuildBranches + ? 'build-deploy' + : ''; + }, [searchParams, hasEnvironments, hasBuildBranches]); const PipelineForm = Pipelines[pipeline]; return ( - + setPipeline(e.target.value as SupportedPipelineNames)} + onChange={(e) => setPipelineType(e.target.value)} > ))} - + {PipelineForm && ( + + )} + ); } CreateJobForm.propTypes = { diff --git a/src/components/create-job-form/pipeline-form-apply-config.tsx b/src/components/create-job-form/pipeline-form-apply-config.tsx index 4dcbbd45b..04f427778 100644 --- a/src/components/create-job-form/pipeline-form-apply-config.tsx +++ b/src/components/create-job-form/pipeline-form-apply-config.tsx @@ -12,19 +12,16 @@ import { Alert } from '../alert'; import { handlePromiseWithToast } from '../global-top-nav/styled-toaster'; import type { FormProp } from './index'; -export function PipelineFormApplyConfig({ - children, - appName, - onSuccess, -}: FormProp) { +export function PipelineFormApplyConfig({ application, onSuccess }: FormProp) { const [trigger, state] = useTriggerPipelineApplyConfigMutation(); const [deployExternalDNS, setDeployExternalDNS] = useState(false); + const handleSubmit = handlePromiseWithToast( async (e: FormEvent) => { e.preventDefault(); const response = await trigger({ - appName, + appName: application.name, pipelineParametersApplyConfig: { deployExternalDNS: deployExternalDNS, }, @@ -37,7 +34,6 @@ export function PipelineFormApplyConfig({
- {children} -
+
- {children} - Build (but do not deploy) a git branch + {isBuildDeployPipeline ? ( + <>Build and deploy a git branch + ) : ( + <>Build (but do not deploy) a git branch + )} Git branch to build - - + + {Object.keys(branches ?? {}).map((branch, i) => (
)} - {pipelineName === 'build-deploy' && - branch && - !isAnyValidRegex(branch) && ( - - )} + {isBuildDeployPipeline && branch && !isAnyValidRegex(branch) && ( + + )}
- {state.isLoading && ( + {createJobState.isLoading && (
Creating…
)} - {state.isError && ( + {createJobState.isError && ( - Failed to create job. {getFetchErrorMessage(state.error)} + Failed to create job. {getFetchErrorMessage(createJobState.error)} )}
diff --git a/src/components/create-job-form/pipeline-form-deploy.tsx b/src/components/create-job-form/pipeline-form-deploy.tsx index 397b5e85d..5ebcdb977 100644 --- a/src/components/create-job-form/pipeline-form-deploy.tsx +++ b/src/components/create-job-form/pipeline-form-deploy.tsx @@ -5,31 +5,22 @@ import { Typography, } from '@equinor/eds-core-react'; import { type FormEvent, useState } from 'react'; -import { pollingInterval } from '../../store/defaults'; -import { - useGetEnvironmentSummaryQuery, - useTriggerPipelineDeployMutation, -} from '../../store/radix-api'; +import { useTriggerPipelineDeployMutation } from '../../store/radix-api'; import { getFetchErrorMessage } from '../../store/utils'; import { Alert } from '../alert'; import { handlePromiseWithToast } from '../global-top-nav/styled-toaster'; import type { FormProp } from './index'; -export function PipelineFormDeploy({ children, appName, onSuccess }: FormProp) { +export function PipelineFormDeploy({ application, onSuccess }: FormProp) { const [trigger, state] = useTriggerPipelineDeployMutation(); - const { data: environments } = useGetEnvironmentSummaryQuery( - { appName }, - { pollingInterval } - ); const [toEnvironment, setToEnvironment] = useState(''); - const handleSubmit = handlePromiseWithToast( async (e: FormEvent) => { e.preventDefault(); const response = await trigger({ - appName, + appName: application.name, pipelineParametersDeploy: { toEnvironment, }, @@ -45,7 +36,6 @@ export function PipelineFormDeploy({ children, appName, onSuccess }: FormProp) {
- {children} setToEnvironment(e.target.value)} value={toEnvironment} > - - {environments?.map(({ name }, i) => ( + + {application.environments?.map(({ name }, i) => ( diff --git a/src/components/create-job-form/pipeline-form-promote.tsx b/src/components/create-job-form/pipeline-form-promote.tsx index 1bd65479d..c4051b9a5 100644 --- a/src/components/create-job-form/pipeline-form-promote.tsx +++ b/src/components/create-job-form/pipeline-form-promote.tsx @@ -11,7 +11,6 @@ import { pollingInterval } from '../../store/defaults'; import { type DeploymentSummary, useGetDeploymentsQuery, - useGetEnvironmentSummaryQuery, useTriggerPipelinePromoteMutation, } from '../../store/radix-api'; import { formatDateTime } from '../../utils/datetime'; @@ -23,19 +22,11 @@ import { Alert } from '../alert'; import { handlePromiseWithToast } from '../global-top-nav/styled-toaster'; import type { FormProp } from './index'; -export function PipelineFormPromote({ - children, - appName, - onSuccess, -}: FormProp) { +export function PipelineFormPromote({ application, onSuccess }: FormProp) { const [searchParams] = useSearchParams(); const [trigger, state] = useTriggerPipelinePromoteMutation(); const { data: deployments } = useGetDeploymentsQuery( - { appName }, - { pollingInterval } - ); - const { data: environments } = useGetEnvironmentSummaryQuery( - { appName }, + { appName: application.name }, { pollingInterval } ); const [toEnvironment, setToEnvironment] = useState(''); @@ -53,7 +44,7 @@ export function PipelineFormPromote({ e.preventDefault(); const response = await trigger({ - appName, + appName: application.name, pipelineParametersPromote: { toEnvironment, deploymentName, @@ -81,7 +72,6 @@ export function PipelineFormPromote({
- {children} - + {Object.keys(groupedDeployments).map((key, i) => ( {groupedDeployments[key].map((x, j) => ( @@ -158,8 +150,10 @@ export function PipelineFormPromote({ onChange={(e) => setToEnvironment(e.target.value)} value={toEnvironment} > - - {environments?.map(({ name, activeDeployment }, i) => ( + + {application.environments?.map(({ name, activeDeployment }, i) => ( - Config file{' '} - - {getRadixConfigFullName(registration.radixConfigFullName)} - + Config file Now you can run the{' '} - + apply-config pipeline job - {' '} + to apply radixconfig.yaml, or go to{' '}