Skip to content

Commit c7700ce

Browse files
committed
refactor
1 parent f277134 commit c7700ce

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

public/locales/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"ControlPlaneCard": {
6161
"deleteConfirmationDialog": "MCP deletion triggered. The list will refresh automatically once completed.",
6262
"editMCP": "Edit Managed Control Plane",
63+
"duplicateMCP": "Duplicate Managed Control Plane",
6364
"deleteMCP": "Delete Managed Control Plane"
6465

6566
},
@@ -383,7 +384,8 @@
383384
"createMCP": {
384385
"dialogTitle": "Create Managed Control Plane",
385386
"titleText": "Managed Control Plane Created Successfully!",
386-
"subtitleText": "Your Managed Control Plane is being set up. It will be ready to use in just a few minutes. You can safely close this window."
387+
"subtitleText": "Your Managed Control Plane is being set up. It will be ready to use in just a few minutes. You can safely close this window.",
388+
"copySuffix": "-copy"
387389
},
388390
"editMCP": {
389391
"dialogTitle": "Edit Managed Control Plane",

src/components/ControlPlanes/ControlPlaneCard/ControlPlaneCard.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,22 @@ interface Props {
3838
projectName: string;
3939
}
4040

41-
export function ControlPlaneCard({ controlPlane, workspace, projectName }: Props) {
41+
type MCPWizardState = {
42+
isOpen: boolean;
43+
mode?: 'edit' | 'duplicate';
44+
};
45+
export const ControlPlaneCard = ({ controlPlane, workspace, projectName }: Props) => {
4246
const [dialogDeleteMcpIsOpen, setDialogDeleteMcpIsOpen] = useState(false);
4347
const toast = useToast();
4448
const { t } = useTranslation();
45-
const [isEditManagedControlPlaneWizardOpen, setIsEditManagedControlPlaneWizardOpen] = useState(false);
49+
const [managedControlPlaneWizardState, setManagedControlPlaneWizardState] = useState<MCPWizardState>({
50+
isOpen: false,
51+
mode: undefined,
52+
});
53+
54+
const handleIsManagedControlPlaneWizardOpen = (isOpen: boolean, mode?: 'edit' | 'duplicate') => {
55+
setManagedControlPlaneWizardState({ isOpen, mode });
56+
};
4657
const { trigger: patchTrigger } = useApiResourceMutation<DeleteMCPType>(
4758
PatchMCPResourceForDeletion(controlPlane.metadata.namespace, controlPlane.metadata.name),
4859
);
@@ -85,7 +96,7 @@ export function ControlPlaneCard({ controlPlane, workspace, projectName }: Props
8596
<ControlPlaneCardMenu
8697
setDialogDeleteMcpIsOpen={setDialogDeleteMcpIsOpen}
8798
isDeleteMcpButtonDisabled={controlPlane.status?.status === ReadyStatus.InDeletion}
88-
setIsEditManagedControlPlaneWizardOpen={setIsEditManagedControlPlaneWizardOpen}
99+
setIsEditManagedControlPlaneWizardOpen={handleIsManagedControlPlaneWizardOpen}
89100
/>
90101
<FlexBox direction="Row" justifyContent="SpaceBetween" alignItems="Center" gap={10}>
91102
<YamlViewButtonWithLoader
@@ -130,11 +141,12 @@ export function ControlPlaneCard({ controlPlane, workspace, projectName }: Props
130141
}}
131142
/>
132143
<EditManagedControlPlaneWizardDataLoader
133-
isOpen={isEditManagedControlPlaneWizardOpen}
134-
setIsOpen={setIsEditManagedControlPlaneWizardOpen}
144+
isOpen={managedControlPlaneWizardState.isOpen}
145+
setIsOpen={handleIsManagedControlPlaneWizardOpen}
135146
workspaceName={namespace}
136147
resourceName={name}
148+
mode={managedControlPlaneWizardState.mode}
137149
/>
138150
</>
139151
);
140-
}
152+
};

src/components/ControlPlanes/ControlPlaneCard/ControlPlaneCardMenu.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useTranslation } from 'react-i18next';
99
type ControlPlanesListMenuProps = {
1010
setDialogDeleteMcpIsOpen: Dispatch<SetStateAction<boolean>>;
1111
isDeleteMcpButtonDisabled: boolean;
12-
setIsEditManagedControlPlaneWizardOpen: Dispatch<SetStateAction<boolean>>;
12+
setIsEditManagedControlPlaneWizardOpen: (isOpen: boolean, mode?: 'edit' | 'duplicate') => void;
1313
};
1414

1515
export const ControlPlaneCardMenu: FC<ControlPlanesListMenuProps> = ({
@@ -34,7 +34,10 @@ export const ControlPlaneCardMenu: FC<ControlPlanesListMenuProps> = ({
3434
onItemClick={(event) => {
3535
const action = (event.detail.item as HTMLElement).dataset.action;
3636
if (action === 'editMcp') {
37-
setIsEditManagedControlPlaneWizardOpen(true);
37+
setIsEditManagedControlPlaneWizardOpen(true, 'edit');
38+
}
39+
if (action === 'duplicateMcp') {
40+
setIsEditManagedControlPlaneWizardOpen(true, 'duplicate');
3841
}
3942
if (action === 'deleteMcp') {
4043
setDialogDeleteMcpIsOpen(true);
@@ -53,6 +56,7 @@ export const ControlPlaneCardMenu: FC<ControlPlanesListMenuProps> = ({
5356
icon="delete"
5457
disabled={isDeleteMcpButtonDisabled}
5558
/>
59+
<MenuItem key={'duplicate'} text={t('ControlPlaneCard.duplicateMCP')} data-action="duplicateMcp" icon="copy" />
5660
<MenuItem
5761
key={'edit'}
5862
text={t('ControlPlaneCard.editMCP')}

src/components/Wizards/CreateManagedControlPlane/CreateManagedControlPlaneWizardContainer.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ type CreateManagedControlPlaneWizardContainerProps = {
6363
setIsOpen: (isOpen: boolean) => void;
6464
projectName?: string;
6565
workspaceName?: string;
66-
isEditMode: boolean;
66+
isEditMode?: boolean;
67+
isDuplicateMode?: boolean;
6768
initialTemplateName?: string;
6869
initialData?: ManagedControlPlaneInterface;
6970
isOnMcpPage?: boolean;
@@ -80,6 +81,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
8081
projectName = '',
8182
workspaceName = '',
8283
isEditMode = false,
84+
isDuplicateMode = false,
8385
initialTemplateName,
8486
initialData,
8587
isOnMcpPage = false,
@@ -399,7 +401,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
399401

400402
// Prefill form when editing
401403
useEffect(() => {
402-
if (!isOpen || !isEditMode) return;
404+
if (!isOpen || !initialData) return;
403405
const roleBindings = initialData?.spec?.authorization?.roleBindings ?? [];
404406
const members: Member[] = roleBindings.flatMap((rb) =>
405407
(rb.subjects ?? []).map((s: MCPSubject) => ({
@@ -409,10 +411,11 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
409411
namespace: s.namespace,
410412
})),
411413
);
414+
const name = initialData?.metadata?.name ?? '';
412415
const labels = (initialData?.metadata?.labels as unknown as Record<string, string>) ?? {};
413416
const annotations = (initialData?.metadata?.annotations as unknown as Record<string, string>) ?? {};
414417
const data = {
415-
name: initialData?.metadata?.name ?? '',
418+
name: isDuplicateMode && !!name ? `${name}${t('createMCP.copySuffix')}` : name,
416419
displayName: annotations?.[DISPLAY_NAME_ANNOTATION] ?? '',
417420
chargingTarget: labels?.[CHARGING_TARGET_LABEL] ?? '',
418421
chargingTargetType: labels?.[CHARGING_TARGET_TYPE_LABEL]?.toLowerCase() ?? '',
@@ -550,7 +553,6 @@ export const CreateManagedControlPlaneWizardContainer: FC<CreateManagedControlPl
550553
data-testid="create-mcp-dialog"
551554
onClose={resetFormAndClose}
552555
>
553-
{' '}
554556
<ComponentsSelectionProvider
555557
componentsList={componentsList ?? []}
556558
setComponentsList={setComponentsList}

src/components/Wizards/CreateManagedControlPlane/EditManagedControlPlaneWizardDataLoader.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type EditManagedControlPlaneWizardDataLoaderProps = {
1919
setIsOpen: (isOpen: boolean) => void;
2020
isOnMcpPage?: boolean;
2121
initialSection?: WizardStepType;
22+
mode?: 'edit' | 'duplicate';
2223
};
2324

2425
export const EditManagedControlPlaneWizardDataLoader: FC<EditManagedControlPlaneWizardDataLoaderProps> = ({
@@ -28,6 +29,7 @@ export const EditManagedControlPlaneWizardDataLoader: FC<EditManagedControlPlane
2829
setIsOpen,
2930
isOnMcpPage = false,
3031
initialSection,
32+
mode = 'edit',
3133
}) => {
3234
const { isLoading, data, error } = useApiResource(
3335
ResourceObject<ManagedControlPlaneInterface>(workspaceName ?? '', 'managedcontrolplanes', resourceName),
@@ -55,7 +57,8 @@ export const EditManagedControlPlaneWizardDataLoader: FC<EditManagedControlPlane
5557
setIsOpen={setIsOpen}
5658
projectName={`project-${data?.metadata?.labels?.[PROJECT_NAME_LABEL]}`}
5759
workspaceName={data?.metadata?.labels?.[WORKSPACE_LABEL]}
58-
isEditMode={true}
60+
isEditMode={mode === 'edit'}
61+
isDuplicateMode={mode === 'duplicate'}
5962
initialData={data}
6063
isOnMcpPage={isOnMcpPage}
6164
initialSection={initialSection}

0 commit comments

Comments
 (0)