(frontend) add config provider

Add a ConfigProvider to the frontend to provide
configuration to the app.
The configuration is loaded from the config
endpoint, we will use react-query cache capabilities
to store the configuration.
This commit is contained in:
Anthony LC
2024-11-15 09:33:08 +01:00
committed by Anthony LC
parent 0e55bf5c43
commit 5b1745f991
7 changed files with 80 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import '@/i18n/initI18n';
import { useResponsiveStore } from '@/stores/';
import { Auth } from './auth/';
import { ConfigProvider } from './config/';
/**
* QueryClient:
@@ -39,7 +40,9 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
return (
<QueryClientProvider client={queryClient}>
<CunninghamProvider theme={theme}>
<Auth>{children}</Auth>
<ConfigProvider>
<Auth>{children}</Auth>
</ConfigProvider>
</CunninghamProvider>
</QueryClientProvider>
);

View File

@@ -0,0 +1,9 @@
import { PropsWithChildren } from 'react';
import { useConfig } from './api/useConfig';
export const ConfigProvider = ({ children }: PropsWithChildren) => {
useConfig();
return children;
};

View File

@@ -0,0 +1 @@
export * from './useConfig';

View File

@@ -0,0 +1,33 @@
import { useQuery } from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api';
interface ConfigResponse {
SENTRY_DSN: string;
COLLABORATION_SERVER_URL: string;
ENVIRONMENT: string;
FRONTEND_THEME: string;
LANGUAGES: [string, string][];
LANGUAGE_CODE: string;
MEDIA_BASE_URL: string;
}
export const getConfig = async (): Promise<ConfigResponse> => {
const response = await fetchAPI(`config/`);
if (!response.ok) {
throw new APIError('Failed to get the doc', await errorCauses(response));
}
return response.json() as Promise<ConfigResponse>;
};
export const KEY_CONFIG = 'config';
export function useConfig() {
return useQuery<ConfigResponse, APIError, ConfigResponse>({
queryKey: [KEY_CONFIG],
queryFn: () => getConfig(),
staleTime: Infinity,
});
}

View File

@@ -0,0 +1,2 @@
export * from './api/useConfig';
export * from './ConfigProvider';