From 39db87633acb606fc8670a14fc32be77f80eb606 Mon Sep 17 00:00:00 2001 From: Isha Venikar Date: Wed, 16 Oct 2024 23:00:07 +0530 Subject: [PATCH] Create separate form for adding environment variables --- .../id/settings/EnvironmentVariables.tsx | 119 ++---------------- .../id/settings/EnvironmentVariablesForm.tsx | 98 +++++++++++++++ 2 files changed, 105 insertions(+), 112 deletions(-) create mode 100644 packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx diff --git a/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariables.tsx b/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariables.tsx index a710c243..b3c9314d 100644 --- a/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariables.tsx +++ b/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariables.tsx @@ -1,22 +1,19 @@ -import { useCallback, useEffect, useMemo, useState } from 'react'; -import { useFieldArray, useForm } from 'react-hook-form'; +import { useCallback, useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; -import { Collapse, Checkbox } from '@snowballtools/material-tailwind-react-fork'; +import { Collapse } from '@snowballtools/material-tailwind-react-fork'; -import AddEnvironmentVariableRow from 'components/projects/project/settings/AddEnvironmentVariableRow'; import DisplayEnvironmentVariables from 'components/projects/project/settings/DisplayEnvironmentVariables'; import { useGQLClient } from 'context/GQLClientContext'; import { EnvironmentVariablesFormValues } from '../../../../../types'; import HorizontalLine from 'components/HorizontalLine'; import { Heading } from 'components/shared/Heading'; -import { Button } from 'components/shared/Button'; // import { Checkbox } from 'components/shared/Checkbox'; import { PlusIcon } from 'components/shared/CustomIcon'; -import { InlineNotification } from 'components/shared/InlineNotification'; import { ProjectSettingContainer } from 'components/projects/project/settings/ProjectSettingContainer'; import { useToast } from 'components/shared/Toast'; import { Environment, EnvironmentVariable } from 'gql-client'; +import EnvironmentVariablesForm from './EnvironmentVariablesForm'; export const EnvironmentVariablesTabPanel = () => { const { id } = useParams(); @@ -27,38 +24,8 @@ export const EnvironmentVariablesTabPanel = () => { EnvironmentVariable[] >([]); - const { - handleSubmit, - register, - control, - reset, - formState: { isSubmitSuccessful, errors }, - } = useForm({ - defaultValues: { - variables: [{ key: '', value: '' }], - environment: { - development: false, - preview: false, - production: false, - }, - }, - }); const [createNewVariable, setCreateNewVariable] = useState(false); - const { fields, append, remove } = useFieldArray({ - name: 'variables', - control, - rules: { - required: 'Add at least 1 environment variables', - }, - }); - - useEffect(() => { - if (isSubmitSuccessful) { - reset(); - } - }, [isSubmitSuccessful, reset, id]); - const getEnvironmentVariables = useCallback( (environment: Environment) => { return environmentVariables.filter( @@ -68,21 +35,6 @@ export const EnvironmentVariablesTabPanel = () => { [environmentVariables, id], ); - const isFieldEmpty = useMemo(() => { - if (errors.variables) { - return fields.some((_, index) => { - if ( - errors.variables![index]?.value?.type === 'required' || - errors.variables![index]?.key?.type === 'required' - ) { - return true; - } - }); - } - - return false; - }, [fields, errors.variables, id]); - const fetchEnvironmentVariables = useCallback( async (id: string | undefined) => { if (id) { @@ -99,7 +51,7 @@ export const EnvironmentVariablesTabPanel = () => { }, [id]); const createEnvironmentVariablesHandler = useCallback( - async (createFormData: EnvironmentVariablesFormValues) => { + async (createFormData: EnvironmentVariablesFormValues, reset: () => void) => { const environmentVariables = createFormData.variables.map((variable) => { return { key: variable.key, @@ -160,66 +112,9 @@ export const EnvironmentVariablesTabPanel = () => {
-
- {fields.map((field, index) => { - return ( - remove(index)} - isDeleteDisabled={fields.length === 1} - /> - ); - })} -
- - {/* TODO: Implement import environment varible functionality */} - -
- {isFieldEmpty && ( - - )} -
- - - -
-
- -
- -
+ createEnvironmentVariablesHandler(data, reset)} /> +
diff --git a/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx b/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx new file mode 100644 index 00000000..5c70c1f1 --- /dev/null +++ b/packages/frontend/src/pages/org-slug/projects/id/settings/EnvironmentVariablesForm.tsx @@ -0,0 +1,98 @@ +import React, { useEffect, useMemo } from 'react'; +import { useForm, useFieldArray } from 'react-hook-form'; + +import { Checkbox } from '@snowballtools/material-tailwind-react-fork'; + +import { Button } from 'components/shared/Button'; +import { InlineNotification } from 'components/shared/InlineNotification'; +import AddEnvironmentVariableRow from 'components/projects/project/settings/AddEnvironmentVariableRow'; +import { EnvironmentVariablesFormValues } from 'types/types'; + +interface EnvironmentVariablesFormProps { + onSubmit: (data: EnvironmentVariablesFormValues, reset: () => void) => void; +} + +const EnvironmentVariablesForm: React.FC = ({ onSubmit }) => { + const { + handleSubmit, + register, + control, + reset, + formState: { isSubmitSuccessful, errors }, + } = useForm({ + defaultValues: { + variables: [{ key: '', value: '' }], + environment: { + development: false, + preview: false, + production: false, + }, + }, + }); + + const { fields, append, remove } = useFieldArray({ + name: 'variables', + control, + rules: { + required: 'Add at least 1 environment variables', + }, + }); + + useEffect(() => { + if (isSubmitSuccessful) { + reset(); + } + }, [isSubmitSuccessful, reset]); + + const isFieldEmpty = useMemo(() => { + if (errors.variables) { + return fields.some((_, index) => { + if ( + errors.variables![index]?.value?.type === 'required' || + errors.variables![index]?.key?.type === 'required' + ) { + return true; + } + }); + } + + return false; + }, [fields, errors.variables]); + + return ( +
onSubmit(data, reset))}> + {fields.map((field, index) => ( + remove(index)} + isDeleteDisabled={fields.length === 1} + /> + ))} +
+ +
+ {isFieldEmpty && ( + + )} +
+ + + +
+
+ +
+ + ); +}; + +export default EnvironmentVariablesForm;