Skip to content

Commit 5b1745f

Browse files
committed
✨(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.
1 parent 0e55bf5 commit 5b1745f

File tree

7 files changed

+80
-1
lines changed

7 files changed

+80
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to
1515
- 🌐(frontend) Add German translation #255
1616
- ✨(frontend) Add a broadcast store #387
1717
- ✨(backend) whitelist pod's IP address #443
18+
- ✨(backend) config endpoint #425
19+
- ✨(frontend) config endpoint #424
1820

1921
## Changed
2022

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
test.describe('Config', () => {
4+
test('it checks the config api is called', async ({ page }) => {
5+
const responsePromise = page.waitForResponse(
6+
(response) =>
7+
response.url().includes('/config/') && response.status() === 200,
8+
);
9+
10+
await page.goto('/');
11+
12+
const response = await responsePromise;
13+
expect(response.ok()).toBeTruthy();
14+
15+
expect(await response.json()).toStrictEqual({
16+
COLLABORATION_SERVER_URL: 'ws://localhost:4444',
17+
ENVIRONMENT: 'development',
18+
FRONTEND_THEME: 'dsfr',
19+
MEDIA_BASE_URL: 'http://localhost:8083',
20+
LANGUAGES: [
21+
['en-us', 'English'],
22+
['fr-fr', 'French'],
23+
['de-de', 'German'],
24+
],
25+
LANGUAGE_CODE: 'en-us',
26+
SENTRY_DSN: null,
27+
});
28+
});
29+
});

src/frontend/apps/impress/src/core/AppProvider.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import '@/i18n/initI18n';
77
import { useResponsiveStore } from '@/stores/';
88

99
import { Auth } from './auth/';
10+
import { ConfigProvider } from './config/';
1011

1112
/**
1213
* QueryClient:
@@ -39,7 +40,9 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
3940
return (
4041
<QueryClientProvider client={queryClient}>
4142
<CunninghamProvider theme={theme}>
42-
<Auth>{children}</Auth>
43+
<ConfigProvider>
44+
<Auth>{children}</Auth>
45+
</ConfigProvider>
4346
</CunninghamProvider>
4447
</QueryClientProvider>
4548
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { PropsWithChildren } from 'react';
2+
3+
import { useConfig } from './api/useConfig';
4+
5+
export const ConfigProvider = ({ children }: PropsWithChildren) => {
6+
useConfig();
7+
8+
return children;
9+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useConfig';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
3+
import { APIError, errorCauses, fetchAPI } from '@/api';
4+
5+
interface ConfigResponse {
6+
SENTRY_DSN: string;
7+
COLLABORATION_SERVER_URL: string;
8+
ENVIRONMENT: string;
9+
FRONTEND_THEME: string;
10+
LANGUAGES: [string, string][];
11+
LANGUAGE_CODE: string;
12+
MEDIA_BASE_URL: string;
13+
}
14+
15+
export const getConfig = async (): Promise<ConfigResponse> => {
16+
const response = await fetchAPI(`config/`);
17+
18+
if (!response.ok) {
19+
throw new APIError('Failed to get the doc', await errorCauses(response));
20+
}
21+
22+
return response.json() as Promise<ConfigResponse>;
23+
};
24+
25+
export const KEY_CONFIG = 'config';
26+
27+
export function useConfig() {
28+
return useQuery<ConfigResponse, APIError, ConfigResponse>({
29+
queryKey: [KEY_CONFIG],
30+
queryFn: () => getConfig(),
31+
staleTime: Infinity,
32+
});
33+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './api/useConfig';
2+
export * from './ConfigProvider';

0 commit comments

Comments
 (0)