Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions frontend/src/component/context/ContextForm/ContextForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ export const ContextForm: React.FC<IContextForm> = ({
/>
<Typography>{stickiness ? 'On' : 'Off'}</Typography>
</StyledSwitchContainer>
{mode === 'Edit' ? (
<ContextFieldUsage contextName={contextName} />
) : null}
<ContextFieldUsage contextName={contextName} />
</div>
<StyledButtonContainer>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ const ContextList: FC = () => {
const projectContextFieldsEnabled = useUiFlag('projectContextFields');
const [showDelDialogue, setShowDelDialogue] = useState(false);
const [name, setName] = useState<string>();
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) {
Expand All @@ -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,
Expand Down Expand Up @@ -88,7 +89,7 @@ const ContextList: FC = () => {
}: any) => (
<LinkCell
title={name}
to={editUrl}
to={`/context/edit/${name}`}
subtitle={description}
/>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)}'`;
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/component/context/EditContext/EditContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const EditContext: FC<EditContextProps> = ({ 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,
Expand All @@ -53,13 +53,10 @@ export const EditContext: FC<EditContextProps> = ({ 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)}'`;
Expand All @@ -68,8 +65,8 @@ export const EditContext: FC<EditContextProps> = ({ 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 {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/component/context/hooks/useContextForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -49,6 +49,7 @@ export const useContextForm = ({
description: contextDesc,
legalValues,
stickiness,
project,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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`;
Expand Down
18 changes: 3 additions & 15 deletions frontend/src/hooks/api/getters/useContext/useContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,17 @@ 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',
})
.then(handleErrorResponses('Context data'))
.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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,17 @@ 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',
})
.then(handleErrorResponses('Context variables'))
.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,
Expand Down
10 changes: 0 additions & 10 deletions src/lib/features/context/context-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ class ContextService {
return this.contextFieldStore.getAll();
}

async getAllWithoutProject(): Promise<IContextField[]> {
const allFields = await this.contextFieldStore.getAll();
return allFields.filter((field) => !field.project);
}

async getAllForProject(projectId: string): Promise<IContextField[]> {
const allFields = await this.contextFieldStore.getAll();
return allFields.filter((field) => field.project === projectId);
}

async getContextField(name: string): Promise<IContextField> {
const field = await this.contextFieldStore.get(name);
if (field === undefined) {
Expand Down
29 changes: 6 additions & 23 deletions src/lib/features/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -59,8 +58,6 @@ export class ContextController extends Controller {

private openApiService: OpenApiService;

private flagResolver: IFlagResolver;

constructor(
config: IUnleashConfig,
{
Expand All @@ -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',
Expand Down Expand Up @@ -285,27 +281,14 @@ export class ContextController extends Controller {
}

async getContextFields(
req: Request<{ projectId?: string }>,
_req: Request,
res: Response<ContextFieldsSchema>,
): Promise<void> {
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(
Expand Down
Loading