diff --git a/frontend/src/component/environments/EnvironmentList/EnvironmentList.tsx b/frontend/src/component/environments/EnvironmentList/EnvironmentList.tsx index 500bfa604b..fdbe265b2b 100644 --- a/frontend/src/component/environments/EnvironmentList/EnvironmentList.tsx +++ b/frontend/src/component/environments/EnvironmentList/EnvironmentList.tsx @@ -26,7 +26,8 @@ const EnvironmentList = () => { enabled: true, protected: false, }; - const { environments, refetchEnvironments } = useEnvironments(); + const { environments, mutateEnvironments, refetchEnvironments } = + useEnvironments(); const { uiConfig } = useUiConfig(); const { refetch: refetchProjectRolePermissions } = useProjectRolePermissions(); @@ -52,7 +53,7 @@ const EnvironmentList = () => { const item = newEnvList.splice(dragIndex, 1)[0]; newEnvList.splice(hoverIndex, 0, item); - refetchEnvironments({ environments: newEnvList }, false); + mutateEnvironments(newEnvList); return newEnvList; }; diff --git a/frontend/src/component/project/ProjectEnvironment/ProjectEnvironment.tsx b/frontend/src/component/project/ProjectEnvironment/ProjectEnvironment.tsx index 9843887a83..b55ba70ebb 100644 --- a/frontend/src/component/project/ProjectEnvironment/ProjectEnvironment.tsx +++ b/frontend/src/component/project/ProjectEnvironment/ProjectEnvironment.tsx @@ -1,28 +1,23 @@ import { useEffect, useState } from 'react'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { useStyles } from './ProjectEnvironment.styles'; - -import useLoading from 'hooks/useLoading'; import { PageContent } from 'component/common/PageContent/PageContent'; import { PageHeader } from 'component/common/PageHeader/PageHeader'; import { UPDATE_PROJECT } from 'component/providers/AccessProvider/permissions'; - import ApiError from 'component/common/ApiError/ApiError'; import useToast from 'hooks/useToast'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import { useEnvironments } from 'hooks/api/getters/useEnvironments/useEnvironments'; import useProject from 'hooks/api/getters/useProject/useProject'; -import { FormControlLabel, FormGroup } from '@mui/material'; +import { FormControlLabel, FormGroup, Alert } from '@mui/material'; import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi'; import EnvironmentDisableConfirm from './EnvironmentDisableConfirm/EnvironmentDisableConfirm'; import { Link } from 'react-router-dom'; -import { Alert } from '@mui/material'; import PermissionSwitch from 'component/common/PermissionSwitch/PermissionSwitch'; import { IProjectEnvironment } from 'interfaces/environments'; import { getEnabledEnvs } from './helpers'; import StringTruncator from 'component/common/StringTruncator/StringTruncator'; import { useThemeStyles } from 'themes/themeStyles'; -import { isDescendantOrSelf } from '@testing-library/user-event/dist/types/utils'; interface IProjectEnvironmentListProps { projectId: string; @@ -45,7 +40,6 @@ const ProjectEnvironmentList = ({ // local state const [selectedEnv, setSelectedEnv] = useState(); const [confirmName, setConfirmName] = useState(''); - const ref = useLoading(loading); const { classes: styles } = useStyles(); const { isOss } = useUiConfig(); @@ -179,68 +173,60 @@ const ProjectEnvironmentList = ({ }; return ( -
- - } - > - - - - Important! In order for your application - to retrieve configured activation strategies for - a specific environment, the application -
must use an environment specific API key. - You can look up the environment-specific API - keys here. -
-
- Your administrator can configure an - environment-specific API key to be used in the - SDK. If you are an administrator you can{' '} - - create a new API key. - -
- No environments available.
} - elseShow={renderEnvironments()} - /> - - - } - elseShow={ - - This feature has not been Unleashed for you yet. - - } + - - + } + isLoading={loading} + > + + + + Important! In order for your application to + retrieve configured activation strategies for a + specific environment, the application +
must use an environment specific API key. You + can look up the environment-specific API keys{' '} + here. +
+
+ Your administrator can configure an + environment-specific API key to be used in the SDK. + If you are an administrator you can{' '} + create a new API key. +
+ No environments available.} + elseShow={renderEnvironments()} + /> + + + } + elseShow={ + + This feature has not been Unleashed for you yet. + + } + /> + ); }; diff --git a/frontend/src/hooks/api/getters/useEnvironments/useEnvironments.ts b/frontend/src/hooks/api/getters/useEnvironments/useEnvironments.ts index a49fa6aeac..c2fc537fcb 100644 --- a/frontend/src/hooks/api/getters/useEnvironments/useEnvironments.ts +++ b/frontend/src/hooks/api/getters/useEnvironments/useEnvironments.ts @@ -1,35 +1,49 @@ -import useSWR, { mutate } from 'swr'; -import { useCallback, useMemo } from 'react'; -import { IEnvironmentResponse } from 'interfaces/environments'; +import useSWR from 'swr'; +import { useMemo, useCallback } from 'react'; +import { IEnvironmentResponse, IEnvironment } from 'interfaces/environments'; import { formatApiPath } from 'utils/formatPath'; import handleErrorResponses from '../httpErrorResponseHandler'; -const PATH = formatApiPath(`api/admin/environments`); +interface IUseEnvironmentsOutput { + environments: IEnvironment[]; + loading: boolean; + error?: Error; + mutateEnvironments: (environments: IEnvironment[]) => Promise; + refetchEnvironments: () => Promise; +} -export const useEnvironments = () => { - const { data, error } = useSWR(PATH, fetcher); - - const refetchEnvironments = useCallback( - (data?: IEnvironmentResponse, revalidate?: boolean) => { - mutate(PATH, data, revalidate).catch(console.warn); - }, - [] +export const useEnvironments = (): IUseEnvironmentsOutput => { + const { data, error, mutate } = useSWR( + formatApiPath(`api/admin/environments`), + fetcher ); const environments = useMemo(() => { return data?.environments || []; }, [data]); + const refetchEnvironments = useCallback(async () => { + await mutate(); + }, [mutate]); + + const mutateEnvironments = useCallback( + async (environments: IEnvironment[]) => { + await mutate({ environments }, false); + }, + [mutate] + ); + return { environments, refetchEnvironments, + mutateEnvironments, loading: !error && !data, error, }; }; -const fetcher = (): Promise => { - return fetch(PATH) +const fetcher = (path: string): Promise => { + return fetch(path) .then(handleErrorResponses('Environments')) .then(res => res.json()); };