2022-05-05 13:42:18 +02:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2022-01-19 14:28:55 +01:00
|
|
|
import ProjectForm from '../ProjectForm/ProjectForm';
|
2023-03-22 19:04:02 +01:00
|
|
|
import useProjectForm, {
|
|
|
|
DEFAULT_PROJECT_STICKINESS,
|
|
|
|
} from '../hooks/useProjectForm';
|
2022-02-24 09:23:07 +01:00
|
|
|
import { CreateButton } from 'component/common/CreateButton/CreateButton';
|
2022-02-24 00:57:35 +01:00
|
|
|
import FormTemplate from 'component/common/FormTemplate/FormTemplate';
|
|
|
|
import { CREATE_PROJECT } from 'component/providers/AccessProvider/permissions';
|
|
|
|
import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi';
|
|
|
|
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
|
|
|
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
|
|
|
import useToast from 'hooks/useToast';
|
2022-03-25 12:34:20 +01:00
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
2022-08-04 13:57:25 +02:00
|
|
|
import { GO_BACK } from 'constants/navigate';
|
2023-03-22 19:04:02 +01:00
|
|
|
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
|
2023-07-11 08:47:38 +02:00
|
|
|
import { Button, styled } from '@mui/material';
|
2022-01-19 14:28:55 +01:00
|
|
|
|
2023-03-17 13:41:59 +01:00
|
|
|
const CREATE_PROJECT_BTN = 'CREATE_PROJECT_BTN';
|
|
|
|
|
2023-07-11 08:47:38 +02:00
|
|
|
const StyledButton = styled(Button)(({ theme }) => ({
|
|
|
|
marginLeft: theme.spacing(3),
|
|
|
|
}));
|
|
|
|
|
2022-01-19 14:28:55 +01:00
|
|
|
const CreateProject = () => {
|
|
|
|
const { setToastData, setToastApiError } = useToast();
|
2022-02-10 17:04:10 +01:00
|
|
|
const { refetchUser } = useAuthUser();
|
2022-01-19 14:28:55 +01:00
|
|
|
const { uiConfig } = useUiConfig();
|
2022-05-05 13:42:18 +02:00
|
|
|
const navigate = useNavigate();
|
2023-03-22 19:04:02 +01:00
|
|
|
const { trackEvent } = usePlausibleTracker();
|
2022-01-19 14:28:55 +01:00
|
|
|
const {
|
|
|
|
projectId,
|
|
|
|
projectName,
|
2023-03-16 15:29:52 +01:00
|
|
|
projectMode,
|
2022-01-19 14:28:55 +01:00
|
|
|
projectDesc,
|
2023-07-11 13:55:43 +02:00
|
|
|
featureLimit,
|
2023-09-04 13:53:33 +02:00
|
|
|
featureNamingPattern,
|
|
|
|
featureNamingExample,
|
2023-09-06 10:13:28 +02:00
|
|
|
featureNamingDescription,
|
2023-09-04 13:53:33 +02:00
|
|
|
setFeatureNamingExample,
|
|
|
|
setFeatureNamingPattern,
|
2023-09-06 10:13:28 +02:00
|
|
|
setFeatureNamingDescription,
|
2022-01-19 14:28:55 +01:00
|
|
|
setProjectId,
|
|
|
|
setProjectName,
|
|
|
|
setProjectDesc,
|
|
|
|
getProjectPayload,
|
|
|
|
clearErrors,
|
2022-03-28 23:45:41 +02:00
|
|
|
validateProjectId,
|
2022-01-19 14:28:55 +01:00
|
|
|
validateName,
|
2023-03-10 11:28:02 +01:00
|
|
|
setProjectStickiness,
|
2023-07-11 13:55:43 +02:00
|
|
|
setFeatureLimit,
|
2023-03-16 15:29:52 +01:00
|
|
|
setProjectMode,
|
2023-03-10 11:28:02 +01:00
|
|
|
projectStickiness,
|
2022-01-19 14:28:55 +01:00
|
|
|
errors,
|
|
|
|
} = useProjectForm();
|
|
|
|
|
2023-03-17 13:41:59 +01:00
|
|
|
const { createProject, loading } = useProjectApi();
|
2023-03-10 11:28:02 +01:00
|
|
|
|
2022-01-19 14:28:55 +01:00
|
|
|
const handleSubmit = async (e: Event) => {
|
|
|
|
e.preventDefault();
|
|
|
|
clearErrors();
|
|
|
|
const validName = validateName();
|
2022-03-28 23:45:41 +02:00
|
|
|
const validId = await validateProjectId();
|
2022-01-25 00:47:49 +01:00
|
|
|
|
2022-01-25 12:30:55 +01:00
|
|
|
if (validName && validId) {
|
2022-01-19 14:28:55 +01:00
|
|
|
const payload = getProjectPayload();
|
|
|
|
try {
|
|
|
|
await createProject(payload);
|
2022-02-10 17:04:10 +01:00
|
|
|
refetchUser();
|
2022-05-05 13:42:18 +02:00
|
|
|
navigate(`/projects/${projectId}`);
|
2022-01-19 14:28:55 +01:00
|
|
|
setToastData({
|
|
|
|
title: 'Project created',
|
|
|
|
text: 'Now you can add toggles to this project',
|
|
|
|
confetti: true,
|
|
|
|
type: 'success',
|
|
|
|
});
|
2023-03-22 19:04:02 +01:00
|
|
|
|
|
|
|
if (projectStickiness !== DEFAULT_PROJECT_STICKINESS) {
|
|
|
|
trackEvent('project_stickiness_set');
|
|
|
|
}
|
2022-02-25 10:55:39 +01:00
|
|
|
} catch (error: unknown) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
2022-01-19 14:28:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatApiCode = () => {
|
2023-10-02 14:25:46 +02:00
|
|
|
return `curl --location --request POST '${uiConfig.unleashUrl}/api/admin/projects' \\
|
2022-01-19 14:28:55 +01:00
|
|
|
--header 'Authorization: INSERT_API_KEY' \\
|
|
|
|
--header 'Content-Type: application/json' \\
|
|
|
|
--data-raw '${JSON.stringify(getProjectPayload(), undefined, 2)}'`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
2022-08-04 13:57:25 +02:00
|
|
|
navigate(GO_BACK);
|
2022-01-19 14:28:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormTemplate
|
|
|
|
loading={loading}
|
2023-10-02 14:25:46 +02:00
|
|
|
title='Create project'
|
|
|
|
description='Projects allows you to group feature toggles together in the management UI.'
|
|
|
|
documentationLink='https://docs.getunleash.io/reference/projects'
|
|
|
|
documentationLinkLabel='Projects documentation'
|
2022-01-19 14:28:55 +01:00
|
|
|
formatApiCode={formatApiCode}
|
|
|
|
>
|
|
|
|
<ProjectForm
|
|
|
|
errors={errors}
|
|
|
|
handleSubmit={handleSubmit}
|
|
|
|
projectId={projectId}
|
|
|
|
setProjectId={setProjectId}
|
|
|
|
projectName={projectName}
|
2023-03-16 15:29:52 +01:00
|
|
|
projectMode={projectMode}
|
2023-03-10 11:28:02 +01:00
|
|
|
projectStickiness={projectStickiness}
|
2023-07-11 13:55:43 +02:00
|
|
|
featureLimit={featureLimit}
|
2023-09-04 13:53:33 +02:00
|
|
|
featureNamingExample={featureNamingExample}
|
|
|
|
featureNamingPattern={featureNamingPattern}
|
2023-09-06 10:13:28 +02:00
|
|
|
setFeatureNamingPattern={setFeatureNamingPattern}
|
|
|
|
featureNamingDescription={featureNamingDescription}
|
|
|
|
setFeatureNamingDescription={setFeatureNamingDescription}
|
2023-09-04 13:53:33 +02:00
|
|
|
setFeatureNamingExample={setFeatureNamingExample}
|
2023-03-10 11:28:02 +01:00
|
|
|
setProjectStickiness={setProjectStickiness}
|
2023-07-11 13:55:43 +02:00
|
|
|
setFeatureLimit={setFeatureLimit}
|
2023-03-16 15:29:52 +01:00
|
|
|
setProjectMode={setProjectMode}
|
2022-01-19 14:28:55 +01:00
|
|
|
setProjectName={setProjectName}
|
|
|
|
projectDesc={projectDesc}
|
|
|
|
setProjectDesc={setProjectDesc}
|
2023-10-02 14:25:46 +02:00
|
|
|
mode='Create'
|
2022-01-19 14:28:55 +01:00
|
|
|
clearErrors={clearErrors}
|
2022-03-28 23:45:41 +02:00
|
|
|
validateProjectId={validateProjectId}
|
2022-01-25 00:47:49 +01:00
|
|
|
>
|
2023-03-17 13:41:59 +01:00
|
|
|
<CreateButton
|
2023-10-02 14:25:46 +02:00
|
|
|
name='project'
|
2023-03-17 13:41:59 +01:00
|
|
|
permission={CREATE_PROJECT}
|
|
|
|
data-testid={CREATE_PROJECT_BTN}
|
|
|
|
/>
|
2023-07-11 08:47:38 +02:00
|
|
|
<StyledButton onClick={handleCancel}>Cancel</StyledButton>
|
2022-01-25 00:47:49 +01:00
|
|
|
</ProjectForm>
|
2022-01-19 14:28:55 +01:00
|
|
|
</FormTemplate>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CreateProject;
|