mirror of https://github.com/ctk-hq/ctk
feat(frontend): moved environment form to general tab
parent
fafe71caaa
commit
72302c68da
@ -1,101 +0,0 @@
|
||||
import { useCallback } from "react";
|
||||
import { PlusIcon } from "@heroicons/react/outline";
|
||||
import { Button, styled } from "@mui/joy";
|
||||
import { useFormikContext } from "formik";
|
||||
import Record from "../../Record";
|
||||
import { IEditServiceForm } from "../../../types";
|
||||
|
||||
const Root = styled("div")`
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
`;
|
||||
|
||||
const Records = styled("div")`
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
row-gap: ${({ theme }: { theme: any }) => theme.spacing(1)};
|
||||
`;
|
||||
|
||||
const AddButton = styled(Button)`
|
||||
margin-top: ${({ theme }) => theme.spacing(1)};
|
||||
`;
|
||||
|
||||
const Description = styled("p")`
|
||||
margin-top: ${({ theme }) => theme.spacing(1)};
|
||||
text-align: center;
|
||||
color: #7a7a7a;
|
||||
font-size: 14px;
|
||||
`;
|
||||
|
||||
const Environment = () => {
|
||||
const formik = useFormikContext<IEditServiceForm>();
|
||||
const { environmentVariables } = formik.values;
|
||||
|
||||
const handleNewEnvironmentVariable = useCallback(() => {
|
||||
formik.setFieldValue(
|
||||
`environmentVariables[${environmentVariables.length}]`,
|
||||
{
|
||||
key: "",
|
||||
value: ""
|
||||
}
|
||||
);
|
||||
}, [formik]);
|
||||
|
||||
const handleRemoveEnvironmentVariable = useCallback(
|
||||
(index: number) => {
|
||||
const newEnvironmentVariables = environmentVariables.filter(
|
||||
(_: unknown, currentIndex: number) => currentIndex != index
|
||||
);
|
||||
formik.setFieldValue("environmentVariables", newEnvironmentVariables);
|
||||
},
|
||||
[formik]
|
||||
);
|
||||
|
||||
const emptyEnvironmentVariables =
|
||||
environmentVariables && environmentVariables.length === 0 ? true : false;
|
||||
|
||||
return (
|
||||
<Root>
|
||||
{!emptyEnvironmentVariables && (
|
||||
<Records>
|
||||
{environmentVariables.map((_: unknown, index: number) => (
|
||||
<Record
|
||||
key={index}
|
||||
index={index}
|
||||
fields={[
|
||||
{
|
||||
name: `environmentVariables[${index}].key`,
|
||||
placeholder: "Key",
|
||||
required: true,
|
||||
type: "text"
|
||||
},
|
||||
{
|
||||
name: `environmentVariables[${index}].value`,
|
||||
placeholder: "Value",
|
||||
required: false,
|
||||
type: "text"
|
||||
}
|
||||
]}
|
||||
onRemove={handleRemoveEnvironmentVariable}
|
||||
/>
|
||||
))}
|
||||
</Records>
|
||||
)}
|
||||
|
||||
{emptyEnvironmentVariables && (
|
||||
<Description>No environment variables.</Description>
|
||||
)}
|
||||
|
||||
<AddButton
|
||||
size="sm"
|
||||
variant="plain"
|
||||
onClick={handleNewEnvironmentVariable}
|
||||
>
|
||||
<PlusIcon className="h-4 w-4 mr-2" />
|
||||
New variable
|
||||
</AddButton>
|
||||
</Root>
|
||||
);
|
||||
};
|
||||
export default Environment;
|
||||
Loading…
Reference in New Issue