Skip to content

Commit aaa35ee

Browse files
committed
refactor
1 parent 8d76211 commit aaa35ee

File tree

6 files changed

+41
-38
lines changed

6 files changed

+41
-38
lines changed

public/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@
308308
"componentsSelection": {
309309
"selectComponents": "Select Components",
310310
"selectedComponents": "Selected Components",
311-
"pleaseSelectComponents": "Choose the components you want to add to your Managed Control Plane."
311+
"pleaseSelectComponents": "Choose the components you want to add to your Managed Control Plane.",
312+
"cannotLoad": "Cannot load components list"
312313
}
313314
}

src/components/ComponentsSelection/ComponentsSelection.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ import {
2020
import styles from './ComponentsSelection.module.css';
2121
import { Infobox } from '../Ui/Infobox/Infobox.tsx';
2222
import { useTranslation } from 'react-i18next';
23-
import { ComponentSelectionItem } from '../../lib/api/types/crate/createManagedControlPlane.ts';
23+
import { ComponentsListItem } from '../../lib/api/types/crate/createManagedControlPlane.ts';
2424
import { getSelectedComponents } from './ComponentsSelectionContainer.tsx';
2525

2626
export interface ComponentsSelectionProps {
27-
components: ComponentSelectionItem[];
28-
setSelectedComponents: (components: ComponentSelectionItem[]) => void;
27+
componentsList: ComponentsListItem[];
28+
setComponentsList: (components: ComponentsListItem[]) => void;
2929
}
3030

3131
export const ComponentsSelection: React.FC<ComponentsSelectionProps> = ({
32-
components,
33-
setSelectedComponents,
32+
componentsList,
33+
setComponentsList,
3434
}) => {
3535
const [searchTerm, setSearchTerm] = useState('');
3636
const { t } = useTranslation();
3737
const handleSelectionChange = (
3838
e: Ui5CustomEvent<CheckBoxDomRef, { checked: boolean }>,
3939
) => {
4040
const id = e.target?.id;
41-
setSelectedComponents(
42-
components.map((component) =>
41+
setComponentsList(
42+
componentsList.map((component) =>
4343
component.name === id
4444
? { ...component, isSelected: !component.isSelected }
4545
: component,
@@ -57,19 +57,19 @@ export const ComponentsSelection: React.FC<ComponentsSelectionProps> = ({
5757
const selectedOption = e.detail.selectedOption as HTMLElement;
5858
const name = selectedOption.dataset.name;
5959
const version = selectedOption.dataset.version;
60-
setSelectedComponents(
61-
components.map((component) =>
60+
setComponentsList(
61+
componentsList.map((component) =>
6262
component.name === name
6363
? { ...component, selectedVersion: version || '' }
6464
: component,
6565
),
6666
);
6767
};
6868

69-
const searchResults = components.filter(({ name }) =>
69+
const searchResults = componentsList.filter(({ name }) =>
7070
name.toLowerCase().includes(searchTerm.toLowerCase()),
7171
);
72-
const selectedComponents = getSelectedComponents(components);
72+
const selectedComponents = getSelectedComponents(componentsList);
7373

7474
return (
7575
<div>
@@ -88,7 +88,7 @@ export const ComponentsSelection: React.FC<ComponentsSelectionProps> = ({
8888
{searchResults.map((component) => {
8989
const isProviderDisabled =
9090
component.name?.includes('provider') &&
91-
components?.find(({ name }) => name === 'crossplane')
91+
componentsList?.find(({ name }) => name === 'crossplane')
9292
?.isSelected === false;
9393
return (
9494
<FlexBox

src/components/ComponentsSelection/ComponentsSelectionContainer.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import { sortVersions } from '../../utils/componentsVersions.ts';
77
import { ListManagedComponents } from '../../lib/api/types/crate/listManagedComponents.ts';
88
import useApiResource from '../../lib/api/useApiResource.ts';
99
import Loading from '../Shared/Loading.tsx';
10-
import { ComponentSelectionItem } from '../../lib/api/types/crate/createManagedControlPlane.ts';
10+
import { ComponentsListItem } from '../../lib/api/types/crate/createManagedControlPlane.ts';
11+
import { useTranslation } from 'react-i18next';
1112

1213
export interface ComponentsSelectionProps {
13-
selectedComponents: ComponentSelectionItem[];
14-
setSelectedComponents: (components: ComponentSelectionItem[]) => void;
14+
componentsList: ComponentsListItem[];
15+
setComponentsList: (components: ComponentsListItem[]) => void;
1516
}
1617

1718
// get selected components and when Crossplane is selected then also providers
18-
export const getSelectedComponents = (components: ComponentSelectionItem[]) =>
19+
export const getSelectedComponents = (components: ComponentsListItem[]) =>
1920
components.filter(
2021
(component) =>
2122
component.isSelected &&
@@ -28,13 +29,14 @@ export const getSelectedComponents = (components: ComponentSelectionItem[]) =>
2829

2930
export const ComponentsSelectionContainer: React.FC<
3031
ComponentsSelectionProps
31-
> = ({ setSelectedComponents, selectedComponents }) => {
32+
> = ({ setComponentsList, componentsList }) => {
3233
const {
3334
data: availableManagedComponentsListData,
3435
error,
3536
isLoading,
3637
} = useApiResource(ListManagedComponents());
3738
const [isReady, setIsReady] = useState(false);
39+
const { t } = useTranslation();
3840
useEffect(() => {
3941
if (
4042
availableManagedComponentsListData?.items.length === 0 ||
@@ -43,7 +45,7 @@ export const ComponentsSelectionContainer: React.FC<
4345
)
4446
return;
4547

46-
setSelectedComponents(
48+
setComponentsList(
4749
availableManagedComponentsListData?.items?.map((item) => {
4850
const versions = sortVersions(item.status.versions);
4951
return {
@@ -56,20 +58,20 @@ export const ComponentsSelectionContainer: React.FC<
5658
}) ?? [],
5759
);
5860
setIsReady(true);
59-
}, [availableManagedComponentsListData, isReady, setSelectedComponents]);
61+
}, [availableManagedComponentsListData, isReady, setComponentsList]);
6062
if (isLoading) {
6163
return <Loading />;
6264
}
6365
if (error) return <IllustratedError />;
6466
return (
6567
<>
66-
{selectedComponents.length > 0 ? (
68+
{componentsList.length > 0 ? (
6769
<ComponentsSelection
68-
components={selectedComponents}
69-
setSelectedComponents={setSelectedComponents}
70+
componentsList={componentsList}
71+
setComponentsList={setComponentsList}
7072
/>
7173
) : (
72-
<IllustratedError title={'Cannot load components list'} />
74+
<IllustratedError title={t('componentsSelection.cannotLoad')} />
7375
)}
7476
</>
7577
);

src/components/Dialogs/CreateWorkspaceDialogContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import { useTranslation } from 'react-i18next';
2323
import { zodResolver } from '@hookform/resolvers/zod';
2424
import { useForm } from 'react-hook-form';
2525
import { validationSchemaProjectWorkspace } from '../../lib/api/validations/schemas.ts';
26-
import { ComponentSelectionItem } from '../../lib/api/types/crate/createManagedControlPlane.ts';
26+
import { ComponentsListItem } from '../../lib/api/types/crate/createManagedControlPlane.ts';
2727

2828
export type CreateDialogProps = {
2929
name: string;
3030
displayName?: string;
3131
chargingTarget?: string;
3232
chargingTargetType?: string;
3333
members: Member[];
34-
selectedComponents?: ComponentSelectionItem[];
34+
selectedComponents?: ComponentsListItem[];
3535
};
3636

3737
export function CreateWorkspaceDialogContainer({

src/components/Wizards/CreateManagedControlPlaneWizardContainer.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import YamlViewer from '../Yaml/YamlViewer.tsx';
2727
import { stringify } from 'yaml';
2828
import { APIError } from '../../lib/api/error.ts';
2929
import {
30-
ComponentSelectionItem,
30+
ComponentsListItem,
3131
CreateManagedControlPlane,
3232
CreateManagedControlPlaneResource,
3333
CreateManagedControlPlaneType,
@@ -137,7 +137,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<
137137
const { trigger } = useApiResourceMutation<CreateManagedControlPlaneType>(
138138
CreateManagedControlPlaneResource(projectName, workspaceName),
139139
);
140-
const selectedComponents = watch('selectedComponents');
140+
const componentsList = watch('selectedComponents');
141141
const handleCreateManagedControlPlane = useCallback(
142142
async ({
143143
name,
@@ -154,7 +154,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<
154154
displayName,
155155
chargingTarget,
156156
members,
157-
selectedComponents,
157+
selectedComponents: componentsList,
158158
},
159159
idpPrefix,
160160
),
@@ -172,7 +172,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<
172172
return false;
173173
}
174174
},
175-
[trigger, projectName, workspaceName, selectedComponents],
175+
[trigger, projectName, workspaceName, componentsList],
176176
);
177177

178178
const handleStepChange = useCallback(
@@ -219,8 +219,8 @@ export const CreateManagedControlPlaneWizardContainer: FC<
219219
[setValue],
220220
);
221221

222-
const setSelectedComponents = useCallback(
223-
(components: ComponentSelectionItem[]) => {
222+
const setComponentsList = useCallback(
223+
(components: ComponentsListItem[]) => {
224224
setValue('selectedComponents', components, { shouldValidate: false });
225225
},
226226
[setValue],
@@ -335,8 +335,8 @@ export const CreateManagedControlPlaneWizardContainer: FC<
335335
disabled={isStepDisabled('componentSelection')}
336336
>
337337
<ComponentsSelectionContainer
338-
selectedComponents={selectedComponents ?? []}
339-
setSelectedComponents={setSelectedComponents}
338+
componentsList={componentsList ?? []}
339+
setComponentsList={setComponentsList}
340340
/>
341341
</WizardStep>
342342
<WizardStep
@@ -379,7 +379,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<
379379
</List>
380380
<br />
381381
<List headerText={t('common.components')}>
382-
{getSelectedComponents(selectedComponents ?? []).map(
382+
{getSelectedComponents(componentsList ?? []).map(
383383
(component) => (
384384
<ListItemStandard
385385
key={component.name}
@@ -401,7 +401,7 @@ export const CreateManagedControlPlaneWizardContainer: FC<
401401
chargingTarget: getValues('chargingTarget'),
402402
members: getValues('members'),
403403
selectedComponents: getSelectedComponents(
404-
selectedComponents ?? [],
404+
componentsList ?? [],
405405
),
406406
},
407407
idpPrefix,

src/lib/api/types/crate/createManagedControlPlane.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Member } from '../shared/members';
99
export type Annotations = Record<string, string>;
1010
export type Labels = Record<string, string>;
1111

12-
export interface ComponentSelectionItem {
12+
export interface ComponentsListItem {
1313
name: string;
1414
versions: string[];
1515
isSelected: boolean;
@@ -68,7 +68,7 @@ export const CreateManagedControlPlane = (
6868
chargingTarget?: string;
6969
chargingTargetType?: string;
7070
members?: Member[];
71-
selectedComponents?: ComponentSelectionItem[];
71+
selectedComponents?: ComponentsListItem[];
7272
},
7373
idpPrefix?: string,
7474
): CreateManagedControlPlaneType => {

0 commit comments

Comments
 (0)