mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-10 17:53:36 +02:00
* refactor: fix child selector warnings * refactor: update react-router-dom * refactor: use BrowserRouter as in react-router docs * refactor: replace Redirect with Navigate * refactor: replace Switch with Routes * refactor: replace useHistory with useNavigate * refactor: replace useParams types with useRequiredPathParam * refactor: replace NavLink activeStyle with callback * refactor: fix matchPath arg order * refactor: Remove unused link state * refactor: delete broken snapshot test * refactor: render 404 page without redirect * refactor: normalize path parameter names * refactor: fix Route component usage
89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
import { useNavigate } from 'react-router-dom';
|
|
import useTagTypeForm from '../TagTypeForm/useTagTypeForm';
|
|
import TagTypeForm from '../TagTypeForm/TagTypeForm';
|
|
import { CreateButton } from 'component/common/CreateButton/CreateButton';
|
|
import FormTemplate from 'component/common/FormTemplate/FormTemplate';
|
|
import { UPDATE_TAG_TYPE } from 'component/providers/AccessProvider/permissions';
|
|
import useTagTypesApi from 'hooks/api/actions/useTagTypesApi/useTagTypesApi';
|
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
|
import useToast from 'hooks/useToast';
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
|
|
|
const CreateTagType = () => {
|
|
const { setToastData, setToastApiError } = useToast();
|
|
const { uiConfig } = useUiConfig();
|
|
const navigate = useNavigate();
|
|
const {
|
|
tagName,
|
|
tagDesc,
|
|
setTagName,
|
|
setTagDesc,
|
|
getTagPayload,
|
|
validateNameUniqueness,
|
|
errors,
|
|
clearErrors,
|
|
} = useTagTypeForm();
|
|
const { createTag, loading } = useTagTypesApi();
|
|
|
|
const handleSubmit = async (e: Event) => {
|
|
e.preventDefault();
|
|
clearErrors();
|
|
const validName = await validateNameUniqueness();
|
|
if (validName) {
|
|
const payload = getTagPayload();
|
|
try {
|
|
await createTag(payload);
|
|
navigate('/tag-types');
|
|
setToastData({
|
|
title: 'Tag type created',
|
|
confetti: true,
|
|
type: 'success',
|
|
});
|
|
} catch (error: unknown) {
|
|
setToastApiError(formatUnknownError(error));
|
|
}
|
|
}
|
|
};
|
|
|
|
const formatApiCode = () => {
|
|
return `curl --location --request POST '${
|
|
uiConfig.unleashUrl
|
|
}/api/admin/tag-types' \\
|
|
--header 'Authorization: INSERT_API_KEY' \\
|
|
--header 'Content-Type: application/json' \\
|
|
--data-raw '${JSON.stringify(getTagPayload(), undefined, 2)}'`;
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
navigate(-1);
|
|
};
|
|
|
|
return (
|
|
<FormTemplate
|
|
loading={loading}
|
|
title="Create tag type"
|
|
description="Tag types allow you to group tags together in the management UI"
|
|
documentationLink="https://docs.getunleash.io/advanced/tags"
|
|
documentationLinkLabel="Tags documentation"
|
|
formatApiCode={formatApiCode}
|
|
>
|
|
<TagTypeForm
|
|
errors={errors}
|
|
handleSubmit={handleSubmit}
|
|
handleCancel={handleCancel}
|
|
tagName={tagName}
|
|
setTagName={setTagName}
|
|
tagDesc={tagDesc}
|
|
setTagDesc={setTagDesc}
|
|
mode="Create"
|
|
clearErrors={clearErrors}
|
|
validateNameUniqueness={validateNameUniqueness}
|
|
>
|
|
<CreateButton name="type" permission={UPDATE_TAG_TYPE} />
|
|
</TagTypeForm>
|
|
</FormTemplate>
|
|
);
|
|
};
|
|
|
|
export default CreateTagType;
|