Skip to content

Commit f3ede76

Browse files
authored
chore: flip UI and backend validation for project env disabling (#9395)
1 parent 596577a commit f3ede76

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

frontend/src/component/project/ProjectEnvironment/ProjectEnvironment.tsx

+22-8
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,26 @@ const ProjectEnvironmentList = () => {
148148
}
149149
};
150150

151-
const envIsDisabled = (projectName: string) => {
152-
return isOss() && projectName === 'default';
151+
const envIsDisabled = (env: IProjectEnvironment) => {
152+
return (
153+
(isOss() && env.name === 'default') ||
154+
(env.projectVisible && onlyOneEnvEnabled())
155+
);
156+
};
157+
158+
const onlyOneEnvEnabled = (): boolean => {
159+
return (
160+
projectEnvironments.filter((env) => env.projectVisible).length === 1
161+
);
162+
};
163+
164+
const buildToolTip = (env: IProjectEnvironment): string => {
165+
if (env.projectVisible && onlyOneEnvEnabled()) {
166+
return 'Cannot disable, at least one environment must be visible in the project';
167+
}
168+
return env.projectVisible
169+
? 'Hide environment and disable feature flags'
170+
: 'Make it visible';
153171
};
154172

155173
const COLUMNS = useMemo(
@@ -182,13 +200,9 @@ const ProjectEnvironmentList = () => {
182200
Cell: ({ row: { original } }: any) => (
183201
<ActionCell>
184202
<PermissionSwitch
185-
tooltip={
186-
original.projectVisible
187-
? 'Hide environment and disable feature flags'
188-
: 'Make it visible'
189-
}
203+
tooltip={buildToolTip(original)}
190204
size='medium'
191-
disabled={envIsDisabled(original.name)}
205+
disabled={envIsDisabled(original)}
192206
projectId={projectId}
193207
permission={UPDATE_PROJECT}
194208
checked={original.projectVisible}

src/lib/features/project/project-service.e2e.test.ts

-7
Original file line numberDiff line numberDiff line change
@@ -2853,13 +2853,6 @@ describe('create project with environments', () => {
28532853
expect(created).toMatchObject(allEnabledEnvs);
28542854
});
28552855

2856-
test('an empty list throws an error', async () => {
2857-
// You shouldn't be allowed to pass an empty list via the API.
2858-
// This test checks what happens in the event that an empty
2859-
// list manages to sneak in.
2860-
await expect(createProjectWithEnvs([])).rejects.toThrow(BadDataError);
2861-
});
2862-
28632856
test('it only enables the envs it is asked to enable', async () => {
28642857
const selectedEnvs = ['development', 'production'];
28652858
const created = await createProjectWithEnvs(selectedEnvs);

src/lib/features/project/project-service.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,6 @@ export default class ProjectService {
315315

316316
async validateProjectEnvironments(environments: string[] | undefined) {
317317
if (environments) {
318-
if (environments.length === 0) {
319-
throw new BadDataError(
320-
'A project must always have at least one environment.',
321-
);
322-
}
323-
324318
await this.validateEnvironmentsExist(environments);
325319
}
326320
}
@@ -383,7 +377,7 @@ export default class ProjectService {
383377

384378
await this.projectStore.create(data);
385379

386-
const envsToEnable = newProject.environments?.length
380+
const envsToEnable = newProject.environments
387381
? newProject.environments
388382
: (
389383
await this.environmentStore.getAll({

0 commit comments

Comments
 (0)