Skip to content

Commit

Permalink
fix: fix broken ImportOptions.tsx (#3657)
Browse files Browse the repository at this point in the history
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
Fixes ImportOptions.tsx

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
Co-authored-by: Prabodh Meshram <prabodh.meshram7@gmail.com>
  • Loading branch information
andreas-unleash and prabodhmeshram committed May 2, 2023
1 parent e901303 commit e61c452
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 32 deletions.
Expand Up @@ -50,7 +50,7 @@ const FeatureSettingsProjectConfirm = ({
const hasSameEnvironments: boolean = useMemo(() => {
return arraysHaveSameItems(
feature.environments.map(env => env.name),
project.environments
project.environments.map(projectEnv => projectEnv.environment)
);
}, [feature, project]);

Expand Down
Expand Up @@ -32,11 +32,13 @@ export const ImportOptions: FC<IImportOptionsProps> = ({
onChange,
}) => {
const { project: projectInfo } = useProject(project);
const environmentOptions = projectInfo.environments.map(environment => ({
key: environment,
label: environment,
title: environment,
}));
const environmentOptions = projectInfo.environments.map(
({ environment }) => ({
key: environment,
label: environment,
title: environment,
})
);

useEffect(() => {
if (environment === '' && environmentOptions[0]) {
Expand Down
Expand Up @@ -148,7 +148,9 @@ export const ProjectFeatureToggles = ({
const [searchParams, setSearchParams] = useSearchParams();
const { isChangeRequestConfigured } = useChangeRequestsEnabled(projectId);
const environments = useEnvironmentsRef(
loading ? ['a', 'b', 'c'] : newEnvironments
loading
? [{ environment: 'a' }, { environment: 'b' }, { environment: 'c' }]
: newEnvironments
);
const { refetch } = useProject(projectId);
const { setToastData, setToastApiError } = useToast();
Expand Down
Expand Up @@ -7,23 +7,14 @@ import { CreateFeatureStrategySchema } from 'openapi';

export type ProjectEnvironmentType = {
environment: string;
defaultStrategy: CreateFeatureStrategySchema | null;
defaultStrategy?: CreateFeatureStrategySchema;
};
export const useEnvironmentsRef = (
environments: Array<string | ProjectEnvironmentType> = []
environments: Array<ProjectEnvironmentType> = []
): string[] => {
let names: string[];
if (
environments &&
environments.length > 0 &&
typeof environments[0] !== 'string'
) {
names = environments.map(
env => (env as ProjectEnvironmentType).environment
);
} else {
names = environments as string[];
}
let names = environments.map(
env => (env as ProjectEnvironmentType).environment
);
const ref = useRef<Array<string>>(names);
if (names.join('') !== ref.current?.join('')) {
ref.current = names;
Expand Down
Expand Up @@ -73,9 +73,9 @@ const ProjectEnvironmentList = () => {
() =>
environments.map(environment => ({
...environment,
projectVisible: project?.environments.includes(
environment.name
),
projectVisible: project?.environments
.map(projectEnvironment => projectEnvironment.environment)
.includes(environment.name),
})),
[environments, project?.environments]
);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/interfaces/project.ts
@@ -1,5 +1,6 @@
import { ProjectStatsSchema } from 'openapi';
import { IFeatureToggleListItem } from './featureToggle';
import { ProjectEnvironmentType } from '../component/project/Project/ProjectFeatureToggles/hooks/useEnvironmentsRef';

export interface IProjectCard {
name: string;
Expand All @@ -18,7 +19,7 @@ export interface IProject {
version: string;
name: string;
description?: string;
environments: string[];
environments: Array<ProjectEnvironmentType>;
health: number;
stats: ProjectStatsSchema;
favorite: boolean;
Expand Down
14 changes: 7 additions & 7 deletions src/test/e2e/api/admin/state.e2e.test.ts
Expand Up @@ -428,11 +428,11 @@ test(`should not show environment on feature toggle, when environment is disable
.get('/api/admin/projects/default/features/my-feature')
.expect(200);

// sort to have predictable test results
const result = body.environments.sort((e1, e2) => e1.name < e2.name);
expect(result).toHaveLength(2);
expect(result[0].name).toBe('development');
expect(result[0].enabled).toBeTruthy();
expect(result[1].name).toBe('production');
expect(result[1].enabled).toBeFalsy();
const result = body.environments;
const sorted = result.sort((e1, e2) => e1.name[0] < e2.name[0]);
expect(sorted).toHaveLength(2);
expect(sorted[0].name).toBe('development');
expect(sorted[0].enabled).toBeTruthy();
expect(sorted[1].name).toBe('production');
expect(sorted[1].enabled).toBeFalsy();
});

0 comments on commit e61c452

Please sign in to comment.