mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-04 00:18:40 +01:00
fix: make api tokens ui consistent and remove check for deprecated envs. (#3410)
<!-- 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! ❤️ --> This PR removes the check for deprecated environments when validating api token environment. Unifies global and project level tokens allow selection of deprecated environments when creating an api token Adds 'deprecated' to the EnvironmentSelector when appropriate ## 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>
This commit is contained in:
parent
0a6f1ecf77
commit
9b6f5cd198
@ -5,38 +5,29 @@ import {
|
||||
StyledInputDescription,
|
||||
StyledSelectInput,
|
||||
} from '../ApiTokenForm.styles';
|
||||
import {
|
||||
IEnvironment,
|
||||
IProjectEnvironment,
|
||||
} from '../../../../../interfaces/environments';
|
||||
import { useEnvironments } from 'hooks/api/getters/useEnvironments/useEnvironments';
|
||||
|
||||
interface IEnvironmentSelectorProps {
|
||||
type: string;
|
||||
environment?: string;
|
||||
environments: IProjectEnvironment[] | IEnvironment[];
|
||||
setEnvironment: React.Dispatch<React.SetStateAction<string | undefined>>;
|
||||
}
|
||||
export const EnvironmentSelector = ({
|
||||
type,
|
||||
environment,
|
||||
setEnvironment,
|
||||
environments,
|
||||
}: IEnvironmentSelectorProps) => {
|
||||
const isProjectEnv = (
|
||||
environment: IEnvironment | IProjectEnvironment
|
||||
): environment is IProjectEnvironment => {
|
||||
return 'projectVisible' in environment;
|
||||
};
|
||||
const { environments } = useEnvironments();
|
||||
const selectableEnvs =
|
||||
type === TokenType.ADMIN
|
||||
? [{ key: '*', label: 'ALL' }]
|
||||
: environments.map(environment => ({
|
||||
key: environment.name,
|
||||
label: environment.name,
|
||||
label: `${environment.name.concat(
|
||||
!environment.enabled ? ' - deprecated' : ''
|
||||
)}`,
|
||||
title: environment.name,
|
||||
disabled: isProjectEnv(environment)
|
||||
? !environment.projectVisible
|
||||
: !environment.enabled,
|
||||
disabled: false,
|
||||
}));
|
||||
|
||||
return (
|
||||
|
@ -18,7 +18,6 @@ import { TokenInfo } from '../ApiTokenForm/TokenInfo/TokenInfo';
|
||||
import { TokenTypeSelector } from '../ApiTokenForm/TokenTypeSelector/TokenTypeSelector';
|
||||
import { ProjectSelector } from '../ApiTokenForm/ProjectSelector/ProjectSelector';
|
||||
import { EnvironmentSelector } from '../ApiTokenForm/EnvironmentSelector/EnvironmentSelector';
|
||||
import { useEnvironments } from '../../../../hooks/api/getters/useEnvironments/useEnvironments';
|
||||
|
||||
const pageTitle = 'Create API token';
|
||||
interface ICreateApiTokenProps {
|
||||
@ -30,7 +29,6 @@ export const CreateApiToken = ({ modal = false }: ICreateApiTokenProps) => {
|
||||
const navigate = useNavigate();
|
||||
const [showConfirm, setShowConfirm] = useState(false);
|
||||
const [token, setToken] = useState('');
|
||||
const { environments } = useEnvironments();
|
||||
|
||||
const {
|
||||
getApiTokenPayload,
|
||||
@ -127,7 +125,6 @@ export const CreateApiToken = ({ modal = false }: ICreateApiTokenProps) => {
|
||||
/>
|
||||
<EnvironmentSelector
|
||||
type={type}
|
||||
environments={environments}
|
||||
environment={environment}
|
||||
setEnvironment={setEnvironment}
|
||||
/>
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import FormTemplate from 'component/common/FormTemplate/FormTemplate';
|
||||
|
||||
@ -21,9 +21,6 @@ import { TokenTypeSelector } from 'component/admin/apiToken/ApiTokenForm/TokenTy
|
||||
import { ConfirmToken } from 'component/admin/apiToken/ConfirmToken/ConfirmToken';
|
||||
import { useProjectApiTokens } from 'hooks/api/getters/useProjectApiTokens/useProjectApiTokens';
|
||||
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
|
||||
import { useProjectEnvironments } from '../../../../../hooks/api/getters/useProjectEnvironments/useProjectEnvironments';
|
||||
import { IProjectEnvironment } from '../../../../../interfaces/environments';
|
||||
import useProject from '../../../../../hooks/api/getters/useProject/useProject';
|
||||
|
||||
const pageTitle = 'Create project API token';
|
||||
|
||||
@ -52,19 +49,7 @@ export const CreateProjectApiTokenForm = () => {
|
||||
useProjectApiTokensApi();
|
||||
const { refetch: refetchProjectTokens } = useProjectApiTokens(projectId);
|
||||
const { trackEvent } = usePlausibleTracker();
|
||||
const { environments } = useProjectEnvironments(projectId);
|
||||
const { project } = useProject(projectId);
|
||||
|
||||
const projectEnvironments = useMemo<IProjectEnvironment[]>(
|
||||
() =>
|
||||
environments.map(environment => ({
|
||||
...environment,
|
||||
projectVisible: project?.environments.includes(
|
||||
environment.name
|
||||
),
|
||||
})),
|
||||
[environments, project?.environments]
|
||||
);
|
||||
usePageTitle(pageTitle);
|
||||
|
||||
const PATH = `api/admin/project/${projectId}/api-tokens`;
|
||||
@ -146,7 +131,6 @@ export const CreateProjectApiTokenForm = () => {
|
||||
type={type}
|
||||
environment={environment}
|
||||
setEnvironment={setEnvironment}
|
||||
environments={projectEnvironments}
|
||||
/>
|
||||
</ApiTokenForm>
|
||||
<ConfirmToken
|
||||
|
@ -128,10 +128,4 @@ export const validateApiTokenEnvironment = (
|
||||
if (!selectedEnvironment) {
|
||||
throw new BadDataError(`Environment=${environment} does not exist`);
|
||||
}
|
||||
|
||||
if (!selectedEnvironment.enabled) {
|
||||
throw new BadDataError(
|
||||
'Client token cannot be scoped to disabled environments',
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -2,7 +2,7 @@ import { setupApp } from '../../helpers/test-helper';
|
||||
import dbInit from '../../helpers/database-init';
|
||||
import getLogger from '../../../fixtures/no-logger';
|
||||
import { ALL, ApiTokenType } from '../../../../lib/types/models/api-token';
|
||||
import { DEFAULT_ENV } from '../../../../lib/util/constants';
|
||||
import { DEFAULT_ENV } from '../../../../lib/util';
|
||||
|
||||
let db;
|
||||
let app;
|
||||
@ -332,7 +332,7 @@ test('client tokens cannot span all environments', async () => {
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
test('should not create token for disabled environment', async () => {
|
||||
test('should create token for disabled environment', async () => {
|
||||
await db.stores.environmentStore.create({
|
||||
name: 'disabledEnvironment',
|
||||
type: 'production',
|
||||
@ -347,5 +347,5 @@ test('should not create token for disabled environment', async () => {
|
||||
environment: 'disabledEnvironment',
|
||||
})
|
||||
.set('Content-Type', 'application/json')
|
||||
.expect(400);
|
||||
.expect(201);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user