Skip to content

Commit

Permalink
cors form frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Dec 20, 2024
1 parent 4bb80d2 commit b68059c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions frontend/src/component/admin/cors/CorsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/hooks/api/actions/useUiConfigApi/useUiConfigApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const useUiConfigApi = () => {
propagateErrors: true,
});

/**
* @deprecated remove when `granularAdminPermissions` flag is removed
*/
const setFrontendSettings = async (
frontendApiOrigins: string[],
): Promise<void> => {
Expand All @@ -19,8 +22,18 @@ export const useUiConfigApi = () => {
await makeRequest(req.caller, req.id);
};

const setCors = async (frontendApiOrigins: string[]): Promise<void> => {
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,
};
Expand Down

0 comments on commit b68059c

Please sign in to comment.