Skip to content

Commit a7aab69

Browse files
committed
Update CreateManagedControlPlaneWizardContainer.tsx
1 parent 628cdcd commit a7aab69

File tree

1 file changed

+126
-169
lines changed

1 file changed

+126
-169
lines changed

src/components/Wizards/CreateManagedControlPlane/CreateManagedControlPlaneWizardContainer.tsx

Lines changed: 126 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import { APIError } from '../../../lib/api/error.ts';
4444
import { MetadataForm } from '../../Dialogs/MetadataForm.tsx';
4545
import { EditMembers } from '../../Members/EditMembers.tsx';
4646
import { ComponentsSelectionContainer } from '../../ComponentsSelection/ComponentsSelectionContainer.tsx';
47-
import { ComponentsSelectionProvider } from '../../ComponentsSelection/ComponentsSelectionProvider.tsx';
4847
import { IllustratedBanner } from '../../Ui/IllustratedBanner/IllustratedBanner.tsx';
4948
import { ManagedControlPlaneTemplate, noTemplateValue } from '../../../lib/api/types/templates/mcpTemplate.ts';
5049
import { stripIdpPrefix } from '../../../utils/stripIdpPrefix.ts';
@@ -57,6 +56,7 @@ import {
5756
MCPSubject,
5857
} from '../../../lib/api/types/mcpResource.ts';
5958
import { stringify } from 'yaml';
59+
import { useComponentsSelectionData } from './useComponentsSelectionData.ts';
6060

6161
type CreateManagedControlPlaneWizardContainerProps = {
6262
isOpen: boolean;
@@ -90,7 +90,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
9090
const { t } = useTranslation();
9191
const { user } = useAuthOnboarding();
9292
const errorDialogRef = useRef<ErrorDialogHandle>(null);
93-
93+
console.log(initialData);
9494
const [selectedStep, setSelectedStep] = useState<WizardStepType>(initialSection ?? 'metadata');
9595
const [metadataFormKey, setMetadataFormKey] = useState(0);
9696

@@ -129,7 +129,6 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
129129
}, [isOpen, initialTemplateName]);
130130

131131
const validationSchemaCreateManagedControlPlane = useMemo(() => createManagedControlPlaneSchema(t), [t]);
132-
const initializedComponents = useRef(false);
133132
const {
134133
register,
135134
handleSubmit,
@@ -151,14 +150,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
151150
},
152151
mode: 'onChange',
153152
});
154-
const [initialMcpDataWhenInEditMode, setInitialMcpDataWhenInEditMode] = useState<CreateDialogProps>({
155-
name: '',
156-
displayName: '',
157-
chargingTarget: '',
158-
chargingTargetType: '',
159-
members: [],
160-
componentsList: [],
161-
});
153+
// no-op: original snapshot for summarize is now built from current form state
162154
useEffect(() => {
163155
if (selectedStep !== 'metadata') return;
164156

@@ -220,7 +212,8 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
220212
isOnMcpPage,
221213
);
222214
const componentsList = watch('componentsList');
223-
215+
console.log('componentsList');
216+
console.log(componentsList);
224217
const hasMissingComponentVersions = useMemo(() => {
225218
const list = (componentsList ?? []) as ComponentsListItem[];
226219
return list.some(({ isSelected, selectedVersion }) => isSelected && !selectedVersion);
@@ -335,7 +328,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
335328

336329
const setComponentsList = useCallback(
337330
(components: ComponentsListItem[]) => {
338-
setValue('componentsList', components, { shouldValidate: false });
331+
setValue('componentsList', components, { shouldValidate: true });
339332
},
340333
[setValue],
341334
);
@@ -396,8 +389,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
396389
}
397390
});
398391
return selection;
399-
// eslint-disable-next-line react-hooks/exhaustive-deps
400-
}, [isEditMode]);
392+
}, [isEditMode, initialData]);
401393

402394
// Prefill form when editing
403395
useEffect(() => {
@@ -424,7 +416,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
424416
};
425417
reset(data);
426418

427-
setInitialMcpDataWhenInEditMode(data);
419+
// summarize step now uses current form values
428420
// eslint-disable-next-line react-hooks/exhaustive-deps
429421
}, [isOpen, isEditMode]);
430422
const normalizeMemberKind = useCallback((kindInput?: string | null) => {
@@ -483,42 +475,14 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
483475
setValue('members', normalizedMembers, { shouldValidate: true });
484476
appliedTemplateMembersRef.current = true;
485477
}, [selectedStep, selectedTemplate, watch, setValue, user?.email, normalizeMemberRole, normalizeMemberKind]);
486-
const setInitialComponentsListHandler = (components: ComponentsListItem[]) => {
487-
if (isEditMode && isDuplicateMode) return;
488-
setInitialMcpDataWhenInEditMode({ ...initialMcpDataWhenInEditMode, componentsList: components });
489-
};
490-
useEffect(() => {
491-
if (selectedStep !== 'componentSelection') return;
492-
if (!selectedTemplate) return;
493-
if (appliedTemplateComponentsRef.current) return;
494-
495-
const defaults = (selectedTemplate?.spec?.spec?.components?.defaultComponents ??
496-
[]) as ManagedControlPlaneTemplate['spec']['spec']['components']['defaultComponents'];
497-
if (!defaults?.length) {
498-
appliedTemplateComponentsRef.current = true;
499-
return;
500-
}
501-
502-
const current = (watch('componentsList') ?? []) as ComponentsListItem[];
503-
if (current.length > 0) {
504-
appliedTemplateComponentsRef.current = true;
505-
return;
506-
}
507-
508-
const mapped = defaults
509-
.filter((c) => !!c?.name && !!c?.version)
510-
.map((c) => ({
511-
name: String(c.name),
512-
version: String(c.version),
513-
selectedVersion: String(c.version),
514-
selected: true,
515-
removable: Boolean(c.removable),
516-
versionChangeable: Boolean(c.versionChangeable),
517-
})) as unknown as ComponentsListItem[];
518-
519-
setValue('componentsList', mapped, { shouldValidate: false });
520-
appliedTemplateComponentsRef.current = true;
521-
}, [selectedStep, selectedTemplate, watch, setValue]);
478+
const {
479+
isLoading: componentsLoading,
480+
error: componentsError,
481+
templateDefaultsError,
482+
} = useComponentsSelectionData(selectedTemplate, initialSelection, isOnMcpPage, (name, value, options) =>
483+
setValue(name as any, value as any, options as any),
484+
);
485+
// Template application for components is handled inside the hook
522486

523487
if (!isOpen) return null;
524488

@@ -553,126 +517,119 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
553517
data-testid="create-mcp-dialog"
554518
onClose={resetFormAndClose}
555519
>
556-
<ComponentsSelectionProvider
557-
componentsList={componentsList ?? []}
558-
setComponentsList={setComponentsList}
559-
initialSelection={initialSelection}
560-
managedControlPlaneTemplate={selectedTemplate}
561-
isOnMcpPage={isOnMcpPage}
562-
setInitialComponentsList={setInitialComponentsListHandler}
563-
initializedComponents={initializedComponents}
564-
>
565-
<ErrorDialog ref={errorDialogRef} />
566-
<Wizard contentLayout="SingleStep" onStepChange={handleStepChange}>
567-
<WizardStep
568-
icon="create-form"
569-
titleText={t('common.metadata')}
570-
disabled={isStepDisabled('metadata')}
571-
selected={selectedStep === 'metadata'}
572-
data-step="metadata"
573-
>
574-
<MetadataForm
575-
key={metadataFormKey}
576-
watch={watch}
577-
setValue={setValue}
578-
register={register}
579-
errors={errors}
580-
isEditMode={isEditMode}
581-
disableChargingFields={!!selectedTemplate}
582-
namePrefix={templateAffixes.namePrefix}
583-
displayNamePrefix={templateAffixes.displayNamePrefix}
584-
nameSuffix={templateAffixes.nameSuffix}
585-
displayNameSuffix={templateAffixes.displayNameSuffix}
586-
/>
587-
</WizardStep>
588-
<WizardStep
589-
icon="user-edit"
590-
titleText={t('common.members')}
591-
selected={selectedStep === 'members'}
592-
data-step="members"
593-
disabled={isStepDisabled('members')}
594-
>
595-
<Form>
596-
<FormGroup headerText={t('CreateProjectWorkspaceDialog.membersHeader')}>
597-
<EditMembers
598-
members={watch('members')}
599-
isValidationError={!!errors.members}
600-
requireAtLeastOneMember={false}
601-
workspaceName={workspaceName}
602-
projectName={projectName}
603-
type={'mcp'}
604-
onMemberChanged={setMembers}
605-
/>
606-
</FormGroup>
607-
</Form>
608-
</WizardStep>
609-
<WizardStep
610-
icon="add-product"
611-
titleText={t('common.componentSelection')}
612-
selected={selectedStep === 'componentSelection'}
613-
data-step="componentSelection"
614-
disabled={isStepDisabled('componentSelection')}
615-
>
616-
{/* this condition is to remount the component from scratch to fix a bug with data loading */}
617-
{selectedStep === 'componentSelection' && (
618-
<ComponentsSelectionContainer
619-
componentsList={componentsList ?? []}
620-
setComponentsList={setComponentsList}
520+
<ErrorDialog ref={errorDialogRef} />
521+
<Wizard contentLayout="SingleStep" onStepChange={handleStepChange}>
522+
<WizardStep
523+
icon="create-form"
524+
titleText={t('common.metadata')}
525+
disabled={isStepDisabled('metadata')}
526+
selected={selectedStep === 'metadata'}
527+
data-step="metadata"
528+
>
529+
<MetadataForm
530+
key={metadataFormKey}
531+
watch={watch}
532+
setValue={setValue}
533+
register={register}
534+
errors={errors}
535+
isEditMode={isEditMode}
536+
disableChargingFields={!!selectedTemplate}
537+
namePrefix={templateAffixes.namePrefix}
538+
displayNamePrefix={templateAffixes.displayNamePrefix}
539+
nameSuffix={templateAffixes.nameSuffix}
540+
displayNameSuffix={templateAffixes.displayNameSuffix}
541+
/>
542+
</WizardStep>
543+
<WizardStep
544+
icon="user-edit"
545+
titleText={t('common.members')}
546+
selected={selectedStep === 'members'}
547+
data-step="members"
548+
disabled={isStepDisabled('members')}
549+
>
550+
<Form>
551+
<FormGroup headerText={t('CreateProjectWorkspaceDialog.membersHeader')}>
552+
<EditMembers
553+
members={watch('members')}
554+
isValidationError={!!errors.members}
555+
requireAtLeastOneMember={false}
556+
workspaceName={workspaceName}
557+
projectName={projectName}
558+
type={'mcp'}
559+
onMemberChanged={setMembers}
621560
/>
622-
)}
623-
</WizardStep>
624-
<WizardStep
625-
icon="activities"
626-
titleText={t('common.summarize')}
627-
disabled={isStepDisabled('summarize')}
628-
selected={selectedStep === 'summarize'}
629-
data-step="summarize"
630-
>
631-
<SummarizeStep
632-
originalYamlString={stringify(
633-
CreateManagedControlPlane(
634-
initialMcpDataWhenInEditMode.name,
635-
`${projectName}--ws-${workspaceName}`,
636-
{
637-
displayName: initialMcpDataWhenInEditMode.displayName,
638-
chargingTarget: initialMcpDataWhenInEditMode.chargingTarget,
639-
members: initialMcpDataWhenInEditMode.members,
640-
componentsList: initialMcpDataWhenInEditMode.componentsList,
641-
chargingTargetType: initialMcpDataWhenInEditMode.chargingTargetType,
642-
},
643-
idpPrefix,
644-
),
645-
)}
646-
watch={watch}
647-
workspaceName={workspaceName}
648-
projectName={projectName}
649-
componentsList={componentsList}
650-
isEditMode={isEditMode}
561+
</FormGroup>
562+
</Form>
563+
</WizardStep>
564+
<WizardStep
565+
icon="add-product"
566+
titleText={t('common.componentSelection')}
567+
selected={selectedStep === 'componentSelection'}
568+
data-step="componentSelection"
569+
disabled={isStepDisabled('componentSelection')}
570+
>
571+
{/* this condition is to remount the component from scratch to fix a bug with data loading */}
572+
{selectedStep === 'componentSelection' && (
573+
<ComponentsSelectionContainer
574+
componentsList={componentsList ?? []}
575+
setComponentsList={setComponentsList}
576+
isLoading={componentsLoading}
577+
error={componentsError}
578+
templateDefaultsError={templateDefaultsError || undefined}
651579
/>
652-
</WizardStep>
653-
<WizardStep
654-
icon="activities"
655-
titleText={t('common.success')}
656-
disabled={isStepDisabled('success')}
657-
selected={selectedStep === 'success'}
658-
data-step="success"
659-
>
660-
{isEditMode ? (
661-
<IllustratedBanner
662-
illustrationName={IllustrationMessageType.SuccessScreen}
663-
title={t('editMCP.titleText')}
664-
subtitle={t('editMCP.subtitleText')}
665-
/>
666-
) : (
667-
<IllustratedBanner
668-
illustrationName={IllustrationMessageType.SuccessScreen}
669-
title={t('createMCP.titleText')}
670-
subtitle={t('createMCP.subtitleText')}
671-
/>
580+
)}
581+
</WizardStep>
582+
<WizardStep
583+
icon="activities"
584+
titleText={t('common.summarize')}
585+
disabled={isStepDisabled('summarize')}
586+
selected={selectedStep === 'summarize'}
587+
data-step="summarize"
588+
>
589+
<SummarizeStep
590+
originalYamlString={stringify(
591+
CreateManagedControlPlane(
592+
watch('name'),
593+
`${projectName}--ws-${workspaceName}`,
594+
{
595+
displayName: watch('displayName'),
596+
chargingTarget: watch('chargingTarget'),
597+
members: watch('members'),
598+
componentsList: componentsList ?? [],
599+
chargingTargetType: watch('chargingTargetType'),
600+
},
601+
idpPrefix,
602+
),
672603
)}
673-
</WizardStep>
674-
</Wizard>
675-
</ComponentsSelectionProvider>
604+
watch={watch}
605+
workspaceName={workspaceName}
606+
projectName={projectName}
607+
componentsList={componentsList}
608+
isEditMode={isEditMode}
609+
/>
610+
</WizardStep>
611+
<WizardStep
612+
icon="activities"
613+
titleText={t('common.success')}
614+
disabled={isStepDisabled('success')}
615+
selected={selectedStep === 'success'}
616+
data-step="success"
617+
>
618+
{isEditMode ? (
619+
<IllustratedBanner
620+
illustrationName={IllustrationMessageType.SuccessScreen}
621+
title={t('editMCP.titleText')}
622+
subtitle={t('editMCP.subtitleText')}
623+
/>
624+
) : (
625+
<IllustratedBanner
626+
illustrationName={IllustrationMessageType.SuccessScreen}
627+
title={t('createMCP.titleText')}
628+
subtitle={t('createMCP.subtitleText')}
629+
/>
630+
)}
631+
</WizardStep>
632+
</Wizard>
676633
</Dialog>
677634
</>
678635
);

0 commit comments

Comments
 (0)