diff --git a/frontend/src/component/context/ContextForm/ContextForm.tsx b/frontend/src/component/context/ContextForm/ContextForm.tsx index b52524a237e0..b5561395dd54 100644 --- a/frontend/src/component/context/ContextForm/ContextForm.tsx +++ b/frontend/src/component/context/ContextForm/ContextForm.tsx @@ -262,9 +262,7 @@ export const ContextForm: React.FC = ({ /> {stickiness ? 'On' : 'Off'} - {mode === 'Edit' ? ( - - ) : null} + {children} diff --git a/frontend/src/component/context/ContextList/ContextList/ContextList.tsx b/frontend/src/component/context/ContextList/ContextList/ContextList.tsx index 95c0a88eb07e..bccb95fb5e4d 100644 --- a/frontend/src/component/context/ContextList/ContextList/ContextList.tsx +++ b/frontend/src/component/context/ContextList/ContextList/ContextList.tsx @@ -33,15 +33,9 @@ const ContextList: FC = () => { const projectContextFieldsEnabled = useUiFlag('projectContextFields'); const [showDelDialogue, setShowDelDialogue] = useState(false); const [name, setName] = useState(); - const { context, refetchUnleashContext, loading } = useUnleashContext( - undefined, - projectId, - ); - const { removeContext } = useContextsApi(projectId); + const { context, refetchUnleashContext, loading } = useUnleashContext(); + const { removeContext } = useContextsApi(); const { setToastData, setToastApiError } = useToast(); - const editUrl = projectId - ? `/projects/${projectId}/context/${name}` - : `/context/edit/${name}`; const data = useMemo(() => { if (loading) { @@ -51,7 +45,14 @@ const ContextList: FC = () => { }); } - return context + const filteredContextFields = + projectId && projectContextFieldsEnabled + ? // @ts-expect-error project doesn't exist yet; todo: fix with flag projectContextFields + context.filter((c) => c.project === projectId) + : // @ts-expect-error project doesn't exist yet; todo: fix with flag projectContextFields + context.filter((c) => !c.project); + + return filteredContextFields .map( ({ name, @@ -88,7 +89,7 @@ const ContextList: FC = () => { }: any) => ( ), diff --git a/frontend/src/component/context/CreateUnleashContext/CreateUnleashContext.tsx b/frontend/src/component/context/CreateUnleashContext/CreateUnleashContext.tsx index eca1076f453f..68e8d502e73f 100644 --- a/frontend/src/component/context/CreateUnleashContext/CreateUnleashContext.tsx +++ b/frontend/src/component/context/CreateUnleashContext/CreateUnleashContext.tsx @@ -40,8 +40,8 @@ export const CreateUnleashContext = ({ setErrors, errors, } = useContextForm({ initialProject: projectId }); - const { createContext, loading } = useContextsApi(projectId); - const { refetchUnleashContext } = useUnleashContext(undefined, projectId); + const { createContext, loading } = useContextsApi(); + const { refetchUnleashContext } = useUnleashContext(); const handleSubmit = async (e: Event) => { e.preventDefault(); @@ -64,12 +64,8 @@ export const CreateUnleashContext = ({ } }; - const postTarget = projectId - ? `/api/admin/projects/${projectId}/context` - : '/api/admin/context'; - const formatApiCode = () => { - return `curl --location --request POST '${uiConfig.unleashUrl}${postTarget}' \\ + return `curl --location --request POST '${uiConfig.unleashUrl}/api/admin/context' \\ --header 'Authorization: INSERT_API_KEY' \\ --header 'Content-Type: application/json' \\ --data-raw '${JSON.stringify(getContextPayload(), undefined, 2)}'`; diff --git a/frontend/src/component/context/EditContext/EditContext.tsx b/frontend/src/component/context/EditContext/EditContext.tsx index 52229c4c2166..def8ec08b13c 100644 --- a/frontend/src/component/context/EditContext/EditContext.tsx +++ b/frontend/src/component/context/EditContext/EditContext.tsx @@ -29,8 +29,8 @@ export const EditContext: FC = ({ modal }) => { const { setToastData, setToastApiError } = useToast(); const projectId = useOptionalPathParam('projectId'); const name = useRequiredPathParam('name'); - const { context, refetch } = useContext({ name, project: projectId }); - const { updateContext, loading } = useContextsApi(projectId); + const { context, refetch } = useContext(name); + const { updateContext, loading } = useContextsApi(); const navigate = useNavigate(); const { contextName, @@ -53,13 +53,10 @@ export const EditContext: FC = ({ modal }) => { initialProject: projectId, }); - const apiUrl = projectId - ? `/projects/${projectId}/api/admin/context/${name}` - : `/api/admin/context/${name}`; const formatApiCode = () => { return `curl --location --request PUT '${ uiConfig.unleashUrl - }${apiUrl}' \\ + }/api/admin/context/${name}' \\ --header 'Authorization: INSERT_API_KEY' \\ --header 'Content-Type: application/json' \\ --data-raw '${JSON.stringify(getContextPayload(), undefined, 2)}'`; @@ -68,8 +65,8 @@ export const EditContext: FC = ({ modal }) => { const handleSubmit = async (e: Event) => { e.preventDefault(); const payload = getContextPayload(); - const navigationTarget = projectId - ? `/projects/${projectId}/settings/context-fields` + const navigationTarget = payload.project + ? `/projects/${payload.project}/settings/context-fields` : '/context'; try { diff --git a/frontend/src/component/context/hooks/useContextForm.ts b/frontend/src/component/context/hooks/useContextForm.ts index 75034fda6740..ef0e9b3ec64c 100644 --- a/frontend/src/component/context/hooks/useContextForm.ts +++ b/frontend/src/component/context/hooks/useContextForm.ts @@ -24,7 +24,7 @@ export const useContextForm = ({ const [stickiness, setStickiness] = useState(initialStickiness); const [project, setProject] = useState(initialProject); const [errors, setErrors] = useState({}); - const { validateContextName } = useContextsApi(project); + const { validateContextName } = useContextsApi(); useEffect(() => { setContextName(initialContextName); @@ -49,6 +49,7 @@ export const useContextForm = ({ description: contextDesc, legalValues, stickiness, + project, }; }; diff --git a/frontend/src/hooks/api/actions/useContextsApi/useContextsApi.ts b/frontend/src/hooks/api/actions/useContextsApi/useContextsApi.ts index fd3ff2299ff6..fe69450f8f58 100644 --- a/frontend/src/hooks/api/actions/useContextsApi/useContextsApi.ts +++ b/frontend/src/hooks/api/actions/useContextsApi/useContextsApi.ts @@ -1,13 +1,11 @@ import useAPI from '../useApi/useApi.js'; -const useContextsApi = (projectId?: string) => { +const useContextsApi = () => { const { makeRequest, createRequest, errors, loading } = useAPI({ propagateErrors: true, }); - const URI = projectId - ? `api/admin/projects/${projectId}/context` - : 'api/admin/context'; + const URI = 'api/admin/context'; const validateContextName = async (name: string) => { const path = `${URI}/validate`; diff --git a/frontend/src/hooks/api/getters/useContext/useContext.ts b/frontend/src/hooks/api/getters/useContext/useContext.ts index d885b95351b3..f5f9968c4fbe 100644 --- a/frontend/src/hooks/api/getters/useContext/useContext.ts +++ b/frontend/src/hooks/api/getters/useContext/useContext.ts @@ -3,21 +3,9 @@ import { useState, useEffect } from 'react'; import { formatApiPath } from 'utils/formatPath'; import handleErrorResponses from '../httpErrorResponseHandler.js'; -type ContextInfo = { - name: string; - project?: string; -}; - -const useContext = ( - { name, project }: ContextInfo, - options: SWRConfiguration = {}, -) => { - const uri = project - ? `api/admin/projects/${project}/context/${name}` - : `api/admin/context/${name}`; - +const useContext = (name: string, options: SWRConfiguration = {}) => { const fetcher = async () => { - const path = formatApiPath(uri); + const path = formatApiPath(`api/admin/context/${name}`); return fetch(path, { method: 'GET', }) @@ -25,7 +13,7 @@ const useContext = ( .then((res) => res.json()); }; - const FEATURE_CACHE_KEY = uri; + const FEATURE_CACHE_KEY = `api/admin/context/${name}`; const { data, error } = useSWR(FEATURE_CACHE_KEY, fetcher, { ...options, diff --git a/frontend/src/hooks/api/getters/useUnleashContext/useUnleashContext.ts b/frontend/src/hooks/api/getters/useUnleashContext/useUnleashContext.ts index ee3be3464682..0edf18407c75 100644 --- a/frontend/src/hooks/api/getters/useUnleashContext/useUnleashContext.ts +++ b/frontend/src/hooks/api/getters/useUnleashContext/useUnleashContext.ts @@ -16,14 +16,9 @@ const useUnleashContext = ( revalidateOnReconnect: true, revalidateIfStale: true, }, - projectId?: string, ): IUnleashContextOutput => { - const uri = projectId - ? formatApiPath(`api/admin/projects/${projectId}/context`) - : formatApiPath(`api/admin/context`); - const fetcher = () => { - const path = formatApiPath(uri); + const path = formatApiPath(`api/admin/context`); return fetch(path, { method: 'GET', }) @@ -31,7 +26,7 @@ const useUnleashContext = ( .then((res) => res.json()); }; - const CONTEXT_CACHE_KEY = uri; + const CONTEXT_CACHE_KEY = 'api/admin/context'; const { data, mutate, error, isValidating } = useSWR( CONTEXT_CACHE_KEY, diff --git a/src/lib/features/context/context-service.ts b/src/lib/features/context/context-service.ts index b069c73b9fc1..c0a655876a62 100644 --- a/src/lib/features/context/context-service.ts +++ b/src/lib/features/context/context-service.ts @@ -54,16 +54,6 @@ class ContextService { return this.contextFieldStore.getAll(); } - async getAllWithoutProject(): Promise { - const allFields = await this.contextFieldStore.getAll(); - return allFields.filter((field) => !field.project); - } - - async getAllForProject(projectId: string): Promise { - const allFields = await this.contextFieldStore.getAll(); - return allFields.filter((field) => field.project === projectId); - } - async getContextField(name: string): Promise { const field = await this.contextFieldStore.get(name); if (field === undefined) { diff --git a/src/lib/features/context/context.ts b/src/lib/features/context/context.ts index e2eaa44d2003..b5cbd7631cbe 100644 --- a/src/lib/features/context/context.ts +++ b/src/lib/features/context/context.ts @@ -41,7 +41,6 @@ import type { CreateContextFieldSchema } from '../../openapi/spec/create-context import { extractUserIdFromUser } from '../../util/index.js'; import type { LegalValueSchema } from '../../openapi/index.js'; import type { WithTransactional } from '../../db/transaction.js'; -import type { IFlagResolver } from '../../types/index.js'; interface ContextParam { contextField: string; @@ -59,8 +58,6 @@ export class ContextController extends Controller { private openApiService: OpenApiService; - private flagResolver: IFlagResolver; - constructor( config: IUnleashConfig, { @@ -77,7 +74,6 @@ export class ContextController extends Controller { this.transactionalContextService = transactionalContextService; const prefix = mode === 'global' ? '' : '/:projectId/context'; const beta = mode === 'project'; - this.flagResolver = config.flagResolver; this.route({ method: 'get', @@ -285,27 +281,14 @@ export class ContextController extends Controller { } async getContextFields( - req: Request<{ projectId?: string }>, + _req: Request, res: Response, ): Promise { - if (this.flagResolver.isEnabled('projectContextFields')) { - const { projectId } = req.params; - const getContextFields = projectId - ? this.transactionalContextService.getAllForProject(projectId) - : this.transactionalContextService.getAllWithoutProject(); - - res.status(200) - .json(serializeDates(await getContextFields)) - .end(); - } else { - res.status(200) - .json( - serializeDates( - await this.transactionalContextService.getAll(), - ), - ) - .end(); - } + res.status(200) + .json( + serializeDates(await this.transactionalContextService.getAll()), + ) + .end(); } async getContextField(