import type { FormikErrors, FormikProps } from 'formik'; import { FieldArray, getIn } from 'formik'; import React from 'react'; import type { IAccount } from '../../account'; import type { IProject, IProjectCluster } from '../../domain'; import { HelpField } from '../../help'; import type { IWizardPageComponent } from '../../modal'; import { FormikFormField, ReactSelectInput, TextInput } from '../../presentation'; export interface IClustersProps { accounts: IAccount[]; } export class Clusters extends React.Component implements IWizardPageComponent { public validate = (value: IProject): FormikErrors => { const applications = value.config.applications || []; if (value.config.clusters && value.config.clusters.length) { const clusterErrors = value.config.clusters.map((cluster) => { const errors: any = {}; if (!cluster.account) { errors.account = 'Account must be specified'; } const apps = cluster.applications || []; const applicationErrors = apps.map((app) => !applications.includes(app) && 'This app is not in the project'); if (applicationErrors.some((val) => !!val)) { errors.applications = applicationErrors; } const areStackAndDetailDisabled = this.areStackAndDetailDisabled(cluster); const stackErrors = areStackAndDetailDisabled && cluster.stack !== '*' && 'Only * is valid for the selected account'; if (stackErrors) { errors.stack = stackErrors; } const detailErrors = areStackAndDetailDisabled && cluster.detail !== '*' && 'Only * is valid for the selected account'; if (detailErrors) { errors.detail = detailErrors; } return Object.keys(errors).length ? errors : null; }); if (clusterErrors.some((val) => !!val)) { return { config: { clusters: clusterErrors, }, } as any; } } return {}; }; private toggleAllApps(formik: FormikProps, path: string) { const isChecked = !getIn(formik.values, path); formik.setFieldValue(path, isChecked ? [] : null); } private areStackAndDetailDisabled(cluster: IProjectCluster): boolean { if (!cluster || !cluster.account) { return false; } const account = this.props.accounts.find(({ name }) => name === cluster.account); return account.type === 'kubernetes'; } public render() { const { accounts } = this.props; const tableHeader = ( Application Account Stack Detail ); return ( { const formik = clustersArrayHelpers.form; const values: IProject = formik.values; const clusters: IProjectCluster[] = values.config.clusters || []; const applications: string[] = values.config.applications || []; const accountNames = accounts.map((account) => account.name); return (
{tableHeader} {clusters.map((cluster, idx) => { const clusterPath = `config.clusters[${idx}]`; const applicationsPath = `${clusterPath}.applications`; return ( ); })}
{!!cluster.applications && ( ( )} /> )}
{input}
} input={(props) => ( )} />
} /> } />
clustersArrayHelpers.push({ stack: '*', detail: '*' })} > Add Cluster
); }} /> ); } }