Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the behaviour of the new project field for Kind installation #1417

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import React from 'react';
import { useForkliftTranslation } from 'src/utils';

import { FormGroupWithHelpText, TypeaheadSelect, TypeaheadSelectOption } from '@kubev2v/common';
import {
K8sResourceKind,
useFlag,
useK8sWatchResource,
} from '@openshift-console/dynamic-plugin-sdk';
import { Popover } from '@patternfly/react-core';
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon';

Expand Down Expand Up @@ -46,3 +51,20 @@ export const ProjectNameSelect: React.FC<ProjectNameSelectProps> = ({
</FormGroupWithHelpText>
);
};

export const useProjectNameSelectOptions = (defaultProject: string): TypeaheadSelectOption[] => {
const isUseProjects = useFlag('OPENSHIFT'); // OCP or Kind installations

const [projects, projectsLoaded, projectsLoadError] = useK8sWatchResource<K8sResourceKind[]>({
kind: isUseProjects ? 'Project' : 'Namespace',
isList: true,
});

return projects.length === 0 || !projectsLoaded || projectsLoadError
? // In case of an error or an empty list, returns the active namespace
[{ value: defaultProject, content: defaultProject }]
: projects.map((project) => ({
value: project.metadata?.name,
content: project.metadata?.name,
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ProvidersCreatePage: React.FC<{
const [activeNamespace, setActiveNamespace] = useActiveNamespace();
const [providerNames] = useK8sWatchProviderNames({ namespace });
const defaultNamespace = process?.env?.DEFAULT_NAMESPACE || 'default';
const projectName = activeNamespace === '#ALL_NS#' ? 'openshift-mtv' : activeNamespace;
const projectName = activeNamespace === '#ALL_NS#' ? defaultNamespace : activeNamespace;
const initialNamespace = namespace || projectName || defaultNamespace;

const initialState: ProvidersCreatePageState = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { useReducer } from 'react';
import { Base64 } from 'js-base64';
import { ProjectNameSelect } from 'src/modules/Plans/views/create/components/ProjectNameSelect';
import {
ProjectNameSelect,
useProjectNameSelectOptions,
} from 'src/modules/Plans/views/create/components/ProjectNameSelect';
import { ModalHOC } from 'src/modules/Providers/modals';
import { validateK8sName, ValidationMsg } from 'src/modules/Providers/utils';
import { SelectableCard } from 'src/modules/Providers/utils/components/Gallery/SelectableCard';
import { SelectableGallery } from 'src/modules/Providers/utils/components/Gallery/SelectableGallery';
import { ForkliftTrans, useForkliftTranslation } from 'src/utils/i18n';

import { FormGroupWithHelpText } from '@kubev2v/common';
import {
IoK8sApiCoreV1Secret,
K8sResourceCommon,
ProviderType,
V1beta1Provider,
} from '@kubev2v/types';
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
import { IoK8sApiCoreV1Secret, ProviderType, V1beta1Provider } from '@kubev2v/types';
import {
Flex,
FlexItem,
Expand Down Expand Up @@ -50,14 +47,7 @@ export const ProvidersCreateForm: React.FC<ProvidersCreateFormProps> = ({
}) => {
const { t } = useForkliftTranslation();

const [projects, projectsLoaded] = useK8sWatchResource<K8sResourceCommon[]>({
groupVersionKind: {
version: 'v1',
group: 'project.openshift.io',
kind: 'Project',
},
isList: true,
});
const projects = useProjectNameSelectOptions(projectName);

const initialState = {
validation: {
Expand Down Expand Up @@ -126,11 +116,7 @@ export const ProvidersCreateForm: React.FC<ProvidersCreateFormProps> = ({
<Form isWidthLimited className="forklift-section-secret-edit">
<ProjectNameSelect
value={projectName}
options={projects.map((project) => ({
value: project.metadata?.name,
content: project.metadata?.name,
}))}
isDisabled={!projectsLoaded || !projects.length}
options={projects}
onSelect={onProjectNameChange}
popoverHelpContent={
<ForkliftTrans>The project that your provider will be created in.</ForkliftTrans>
Expand Down
Loading