2021-09-14 14:20:23 +02:00
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-01-18 11:23:24 +01:00
|
|
|
import useEnvironmentForm from '../hooks/useEnvironmentForm';
|
|
|
|
import EnvironmentForm from '../EnvironmentForm/EnvironmentForm';
|
|
|
|
import FormTemplate from '../../common/FormTemplate/FormTemplate';
|
2021-10-08 09:39:25 +02:00
|
|
|
import { Alert } from '@material-ui/lab';
|
2022-01-18 11:23:24 +01:00
|
|
|
import { Button } from '@material-ui/core';
|
2022-02-24 09:23:07 +01:00
|
|
|
import { CreateButton } from 'component/common/CreateButton/CreateButton';
|
2022-02-24 00:57:35 +01:00
|
|
|
import useEnvironmentApi from 'hooks/api/actions/useEnvironmentApi/useEnvironmentApi';
|
|
|
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
|
|
|
import useToast from 'hooks/useToast';
|
|
|
|
import useEnvironments from 'hooks/api/getters/useEnvironments/useEnvironments';
|
|
|
|
import useProjectRolePermissions from 'hooks/api/getters/useProjectRolePermissions/useProjectRolePermissions';
|
|
|
|
import ConditionallyRender from 'component/common/ConditionallyRender';
|
|
|
|
import PageContent from 'component/common/PageContent/PageContent';
|
|
|
|
import { ADMIN } from 'component/providers/AccessProvider/permissions';
|
|
|
|
import HeaderTitle from 'component/common/HeaderTitle/HeaderTitle';
|
2022-02-25 15:28:38 +01:00
|
|
|
import { formatUnknownError } from 'utils/format-unknown-error';
|
2021-09-14 14:20:23 +02:00
|
|
|
|
|
|
|
const CreateEnvironment = () => {
|
2022-01-18 11:23:24 +01:00
|
|
|
const { setToastApiError, setToastData } = useToast();
|
|
|
|
const { uiConfig } = useUiConfig();
|
2021-09-14 14:20:23 +02:00
|
|
|
const history = useHistory();
|
2021-10-08 09:39:25 +02:00
|
|
|
const { environments } = useEnvironments();
|
2021-12-01 21:34:07 +01:00
|
|
|
const canCreateMoreEnvs = environments.length < 7;
|
2022-01-18 11:23:24 +01:00
|
|
|
const { createEnvironment, loading } = useEnvironmentApi();
|
|
|
|
const { refetch } = useProjectRolePermissions();
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
setName,
|
|
|
|
type,
|
|
|
|
setType,
|
|
|
|
getEnvPayload,
|
|
|
|
validateEnvironmentName,
|
|
|
|
clearErrors,
|
|
|
|
errors,
|
|
|
|
} = useEnvironmentForm();
|
|
|
|
|
|
|
|
const handleSubmit = async (e: Event) => {
|
2021-09-14 14:20:23 +02:00
|
|
|
e.preventDefault();
|
2022-01-18 11:23:24 +01:00
|
|
|
clearErrors();
|
2021-09-14 14:20:23 +02:00
|
|
|
const validName = await validateEnvironmentName();
|
|
|
|
if (validName) {
|
2022-01-18 11:23:24 +01:00
|
|
|
const payload = getEnvPayload();
|
2021-09-14 14:20:23 +02:00
|
|
|
try {
|
2022-01-18 11:23:24 +01:00
|
|
|
await createEnvironment(payload);
|
2022-01-14 15:50:02 +01:00
|
|
|
refetch();
|
2022-01-18 11:23:24 +01:00
|
|
|
setToastData({
|
|
|
|
title: 'Environment created',
|
|
|
|
type: 'success',
|
|
|
|
confetti: true,
|
|
|
|
});
|
|
|
|
history.push('/environments');
|
2022-02-25 10:55:39 +01:00
|
|
|
} catch (error: unknown) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
2021-09-14 14:20:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-18 11:23:24 +01:00
|
|
|
const formatApiCode = () => {
|
|
|
|
return `curl --location --request POST '${
|
|
|
|
uiConfig.unleashUrl
|
|
|
|
}/api/admin/environments' \\
|
|
|
|
--header 'Authorization: INSERT_API_KEY' \\
|
|
|
|
--header 'Content-Type: application/json' \\
|
|
|
|
--data-raw '${JSON.stringify(getEnvPayload(), undefined, 2)}'`;
|
|
|
|
};
|
2022-01-14 15:50:02 +01:00
|
|
|
|
2022-01-18 11:23:24 +01:00
|
|
|
const handleCancel = () => {
|
|
|
|
history.goBack();
|
|
|
|
};
|
2022-01-14 15:50:02 +01:00
|
|
|
|
2022-01-18 11:23:24 +01:00
|
|
|
return (
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={canCreateMoreEnvs}
|
|
|
|
show={
|
|
|
|
<FormTemplate
|
|
|
|
loading={loading}
|
|
|
|
title="Create Environment"
|
2022-02-09 13:39:18 +01:00
|
|
|
description="Environments allow you to manage your
|
2022-01-18 11:23:24 +01:00
|
|
|
product lifecycle from local development
|
|
|
|
through production. Your projects and
|
|
|
|
feature toggles are accessible in all your
|
|
|
|
environments, but they can take different
|
|
|
|
configurations per environment. This means
|
|
|
|
that you can enable a feature toggle in a
|
|
|
|
development or test environment without
|
|
|
|
enabling the feature toggle in the
|
|
|
|
production environment."
|
|
|
|
documentationLink="https://docs.getunleash.io/user_guide/environments"
|
|
|
|
formatApiCode={formatApiCode}
|
|
|
|
>
|
|
|
|
<EnvironmentForm
|
|
|
|
errors={errors}
|
|
|
|
handleSubmit={handleSubmit}
|
|
|
|
handleCancel={handleCancel}
|
|
|
|
validateEnvironmentName={validateEnvironmentName}
|
|
|
|
name={name}
|
|
|
|
type={type}
|
|
|
|
setName={setName}
|
|
|
|
setType={setType}
|
|
|
|
mode="Create"
|
|
|
|
clearErrors={clearErrors}
|
|
|
|
>
|
2022-02-24 09:23:07 +01:00
|
|
|
<CreateButton name="environment" permission={ADMIN} />
|
2022-01-18 11:23:24 +01:00
|
|
|
</EnvironmentForm>
|
|
|
|
</FormTemplate>
|
|
|
|
}
|
|
|
|
elseShow={
|
|
|
|
<>
|
|
|
|
<PageContent
|
|
|
|
headerContent={
|
|
|
|
<HeaderTitle title="Create environment" />
|
2022-01-14 15:50:02 +01:00
|
|
|
}
|
2022-01-18 11:23:24 +01:00
|
|
|
>
|
|
|
|
<Alert severity="error">
|
|
|
|
<p>
|
|
|
|
Currently Unleash does not support more than 7
|
|
|
|
environments. If you need more please reach out.
|
|
|
|
</p>
|
|
|
|
</Alert>
|
|
|
|
<br />
|
|
|
|
<Button
|
|
|
|
onClick={handleCancel}
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
>
|
|
|
|
Go back
|
|
|
|
</Button>
|
|
|
|
</PageContent>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
2021-09-14 14:20:23 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CreateEnvironment;
|