1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-10 01:19:53 +01:00
unleash.unleash/frontend/src/component/context/EditContext/EditContext.tsx
Thomas Heartman b2c58102dd
chore(unl-204): remove uses of toast text and confetti (#8941)
As of PR #8935, we no longer support both text and title, and confetti
has been removed.

This PR:
- removes `confetti` from the toast interface
- merges `text` and `title` into `text` and updates its uses across the
codebase.
- readjusts the text where necessary.
2024-12-10 13:38:04 +00:00

109 lines
3.8 KiB
TypeScript

import FormTemplate from 'component/common/FormTemplate/FormTemplate';
import { UpdateButton } from 'component/common/UpdateButton/UpdateButton';
import { UPDATE_CONTEXT_FIELD } from 'component/providers/AccessProvider/permissions';
import useContextsApi from 'hooks/api/actions/useContextsApi/useContextsApi';
import useContext from 'hooks/api/getters/useContext/useContext';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import useToast from 'hooks/useToast';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { scrollToTop } from 'component/common/util';
import { formatUnknownError } from 'utils/formatUnknownError';
import { ContextForm } from '../ContextForm/ContextForm';
import { useContextForm } from '../hooks/useContextForm';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
import { GO_BACK } from 'constants/navigate';
export const EditContext = () => {
useEffect(() => {
scrollToTop();
}, []);
const { uiConfig } = useUiConfig();
const { setToastData, setToastApiError } = useToast();
const name = useRequiredPathParam('name');
const { context, refetch } = useContext(name);
const { updateContext, loading } = useContextsApi();
const navigate = useNavigate();
const {
contextName,
contextDesc,
legalValues,
stickiness,
setContextName,
setContextDesc,
setLegalValues,
setStickiness,
getContextPayload,
clearErrors,
setErrors,
errors,
} = useContextForm(
context?.name,
context?.description,
context?.legalValues,
context?.stickiness,
);
const formatApiCode = () => {
return `curl --location --request PUT '${
uiConfig.unleashUrl
}/api/admin/context/${name}' \\
--header 'Authorization: INSERT_API_KEY' \\
--header 'Content-Type: application/json' \\
--data-raw '${JSON.stringify(getContextPayload(), undefined, 2)}'`;
};
const handleSubmit = async (e: Event) => {
e.preventDefault();
const payload = getContextPayload();
try {
await updateContext(payload);
refetch();
navigate('/context');
setToastData({
text: 'Context information updated',
type: 'success',
});
} catch (e: unknown) {
setToastApiError(formatUnknownError(e));
}
};
const onCancel = () => {
navigate(GO_BACK);
};
return (
<FormTemplate
loading={loading}
title='Edit context'
description='Context fields are a basic building block used in Unleash to control roll-out.
They can be used together with strategy constraints as part of the activation strategy evaluation.'
documentationLink='https://docs.getunleash.io/reference/unleash-context#custom-context-fields'
documentationLinkLabel='Context fields documentation'
formatApiCode={formatApiCode}
>
<ContextForm
errors={errors}
handleSubmit={handleSubmit}
onCancel={onCancel}
contextName={contextName}
setContextName={setContextName}
contextDesc={contextDesc}
setContextDesc={setContextDesc}
legalValues={legalValues}
setLegalValues={setLegalValues}
stickiness={stickiness}
setStickiness={setStickiness}
mode='Edit'
setErrors={setErrors}
clearErrors={clearErrors}
>
<UpdateButton permission={UPDATE_CONTEXT_FIELD} />
</ContextForm>
</FormTemplate>
);
};