From b68059c490b9ca331ce636ae8199373a34b96587 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 20 Dec 2024 13:41:09 +0100 Subject: [PATCH] cors form frontend --- frontend/src/component/admin/cors/CorsForm.tsx | 10 ++++++++-- .../api/actions/useUiConfigApi/useUiConfigApi.ts | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/frontend/src/component/admin/cors/CorsForm.tsx b/frontend/src/component/admin/cors/CorsForm.tsx index 00fa8471bcbe..0f8e2d7ca4a3 100644 --- a/frontend/src/component/admin/cors/CorsForm.tsx +++ b/frontend/src/component/admin/cors/CorsForm.tsx @@ -7,23 +7,29 @@ import useToast from 'hooks/useToast'; import { formatUnknownError } from 'utils/formatUnknownError'; import { useId } from 'hooks/useId'; import { ADMIN, UPDATE_CORS } from '@server/types/permissions'; +import { useUiFlag } from 'hooks/useUiFlag'; interface ICorsFormProps { frontendApiOrigins: string[] | undefined; } export const CorsForm = ({ frontendApiOrigins }: ICorsFormProps) => { - const { setFrontendSettings } = useUiConfigApi(); + const { setFrontendSettings, setCors } = useUiConfigApi(); const { setToastData, setToastApiError } = useToast(); const [value, setValue] = useState(formatInputValue(frontendApiOrigins)); const inputFieldId = useId(); const helpTextId = useId(); + const isGranularPermissionsEnabled = useUiFlag('granularAdminPermissions'); const onSubmit = async (event: React.FormEvent) => { try { const split = parseInputValue(value); event.preventDefault(); - await setFrontendSettings(split); + if (isGranularPermissionsEnabled) { + await setCors(split); + } else { + await setFrontendSettings(split); + } setValue(formatInputValue(split)); setToastData({ text: 'Settings saved', type: 'success' }); } catch (error) { diff --git a/frontend/src/hooks/api/actions/useUiConfigApi/useUiConfigApi.ts b/frontend/src/hooks/api/actions/useUiConfigApi/useUiConfigApi.ts index 9853704beff7..d80c89761fbd 100644 --- a/frontend/src/hooks/api/actions/useUiConfigApi/useUiConfigApi.ts +++ b/frontend/src/hooks/api/actions/useUiConfigApi/useUiConfigApi.ts @@ -5,6 +5,9 @@ export const useUiConfigApi = () => { propagateErrors: true, }); + /** + * @deprecated remove when `granularAdminPermissions` flag is removed + */ const setFrontendSettings = async ( frontendApiOrigins: string[], ): Promise => { @@ -19,8 +22,18 @@ export const useUiConfigApi = () => { await makeRequest(req.caller, req.id); }; + const setCors = async (frontendApiOrigins: string[]): Promise => { + const req = createRequest( + 'api/admin/ui-config/cors', + { method: 'POST', body: JSON.stringify({ frontendApiOrigins }) }, + 'setCors', + ); + await makeRequest(req.caller, req.id); + }; + return { setFrontendSettings, + setCors, loading, errors, };