2021-09-14 14:20:23 +02:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { FormControl, Button } from '@material-ui/core';
|
|
|
|
import HeaderTitle from '../../common/HeaderTitle';
|
|
|
|
import PageContent from '../../common/PageContent';
|
|
|
|
|
|
|
|
import { useStyles } from './CreateEnvironment.styles';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import useEnvironmentApi from '../../../hooks/api/actions/useEnvironmentApi/useEnvironmentApi';
|
|
|
|
import ConditionallyRender from '../../common/ConditionallyRender';
|
|
|
|
import CreateEnvironmentSuccess from './CreateEnvironmentSuccess/CreateEnvironmentSuccess';
|
|
|
|
import useLoading from '../../../hooks/useLoading';
|
|
|
|
import useToast from '../../../hooks/useToast';
|
|
|
|
import EnvironmentTypeSelector from '../form/EnvironmentTypeSelector/EnvironmentTypeSelector';
|
|
|
|
import Input from '../../common/Input/Input';
|
2021-10-08 09:39:25 +02:00
|
|
|
import useEnvironments from '../../../hooks/api/getters/useEnvironments/useEnvironments';
|
|
|
|
import { Alert } from '@material-ui/lab';
|
2022-01-14 15:50:02 +01:00
|
|
|
import useProjectRolePermissions from '../../../hooks/api/getters/useProjectRolePermissions/useProjectRolePermissions';
|
2021-09-14 14:20:23 +02:00
|
|
|
|
|
|
|
const NAME_EXISTS_ERROR = 'Error: Environment';
|
|
|
|
|
|
|
|
const CreateEnvironment = () => {
|
|
|
|
const [type, setType] = useState('development');
|
|
|
|
const [envName, setEnvName] = useState('');
|
|
|
|
const [nameError, setNameError] = useState('');
|
|
|
|
const [createSuccess, setCreateSucceess] = useState(false);
|
|
|
|
const history = useHistory();
|
|
|
|
const styles = useStyles();
|
|
|
|
const { validateEnvName, createEnvironment, loading } = useEnvironmentApi();
|
2021-10-08 09:39:25 +02:00
|
|
|
const { environments } = useEnvironments();
|
2021-09-14 14:20:23 +02:00
|
|
|
const ref = useLoading(loading);
|
2022-01-14 15:50:02 +01:00
|
|
|
const { setToastApiError } = useToast();
|
|
|
|
const { refetch } = useProjectRolePermissions();
|
2021-09-14 14:20:23 +02:00
|
|
|
|
|
|
|
const handleTypeChange = (event: React.FormEvent<HTMLInputElement>) => {
|
|
|
|
setType(event.currentTarget.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEnvNameChange = (e: React.FormEvent<HTMLInputElement>) => {
|
|
|
|
setEnvName(e.currentTarget.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const goBack = () => history.goBack();
|
|
|
|
|
2021-12-01 21:34:07 +01:00
|
|
|
const canCreateMoreEnvs = environments.length < 7;
|
2021-10-08 09:39:25 +02:00
|
|
|
|
2021-09-14 14:20:23 +02:00
|
|
|
const validateEnvironmentName = async () => {
|
|
|
|
if (envName.length === 0) {
|
|
|
|
setNameError('Environment Id can not be empty.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await validateEnvName(envName);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.toString().includes(NAME_EXISTS_ERROR)) {
|
|
|
|
setNameError('Name already exists');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearNameError = () => setNameError('');
|
|
|
|
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const validName = await validateEnvironmentName();
|
|
|
|
|
|
|
|
if (validName) {
|
|
|
|
const environment = {
|
|
|
|
name: envName,
|
|
|
|
type,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await createEnvironment(environment);
|
2022-01-14 15:50:02 +01:00
|
|
|
refetch();
|
2021-09-14 14:20:23 +02:00
|
|
|
setCreateSucceess(true);
|
|
|
|
} catch (e) {
|
2022-01-14 15:50:02 +01:00
|
|
|
setToastApiError(e.toString());
|
2021-09-14 14:20:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageContent headerContent={<HeaderTitle title="Create environment" />}>
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={createSuccess}
|
2022-01-14 15:50:02 +01:00
|
|
|
show={<CreateEnvironmentSuccess name={envName} type={type} />}
|
2021-09-14 14:20:23 +02:00
|
|
|
elseShow={
|
2022-01-14 15:50:02 +01:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={canCreateMoreEnvs}
|
|
|
|
show={
|
|
|
|
<div ref={ref}>
|
|
|
|
<p className={styles.helperText} data-loading>
|
|
|
|
Environments allow you to manage your
|
|
|
|
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.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<FormControl component="fieldset">
|
|
|
|
<h3
|
|
|
|
className={styles.formHeader}
|
|
|
|
data-loading
|
|
|
|
>
|
|
|
|
Environment Id and name
|
|
|
|
</h3>
|
|
|
|
|
|
|
|
<div
|
|
|
|
data-loading
|
|
|
|
className={
|
|
|
|
styles.environmentDetailsContainer
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<p>
|
|
|
|
Unique env name for SDK
|
|
|
|
configurations.
|
|
|
|
</p>
|
|
|
|
<Input
|
|
|
|
label="Environment Id"
|
|
|
|
onFocus={clearNameError}
|
|
|
|
placeholder="A unique name for your environment"
|
|
|
|
onBlur={validateEnvironmentName}
|
|
|
|
error={Boolean(nameError)}
|
|
|
|
errorText={nameError}
|
|
|
|
value={envName}
|
|
|
|
onChange={handleEnvNameChange}
|
|
|
|
className={styles.inputField}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<EnvironmentTypeSelector
|
|
|
|
onChange={handleTypeChange}
|
|
|
|
value={type}
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<div className={styles.btnContainer}>
|
|
|
|
<Button
|
|
|
|
className={styles.submitButton}
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
type="submit"
|
|
|
|
data-loading
|
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</Button>{' '}
|
|
|
|
<Button
|
|
|
|
className={styles.submitButton}
|
|
|
|
variant="outlined"
|
|
|
|
color="secondary"
|
|
|
|
onClick={goBack}
|
|
|
|
data-loading
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
elseShow={
|
|
|
|
<>
|
|
|
|
<Alert severity="error">
|
2021-09-14 14:20:23 +02:00
|
|
|
<p>
|
2022-01-14 15:50:02 +01:00
|
|
|
Currently Unleash does not support more
|
|
|
|
than 5 environments. If you need more
|
|
|
|
please reach out.
|
2021-09-14 14:20:23 +02:00
|
|
|
</p>
|
2022-01-14 15:50:02 +01:00
|
|
|
</Alert>
|
|
|
|
<br />
|
2021-09-14 14:20:23 +02:00
|
|
|
<Button
|
2022-01-14 15:50:02 +01:00
|
|
|
onClick={goBack}
|
2021-09-14 14:20:23 +02:00
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
>
|
2022-01-14 15:50:02 +01:00
|
|
|
Go back
|
2021-09-14 14:20:23 +02:00
|
|
|
</Button>
|
2022-01-14 15:50:02 +01:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
2021-09-14 14:20:23 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PageContent>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CreateEnvironment;
|