-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ConfigProvider to the frontend to provide configuration to the app. The configuration is loaded from the config endpoint and stored in a zustand store.
- Loading branch information
Showing
9 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Loader } from '@openfun/cunningham-react'; | ||
import { PropsWithChildren, useEffect } from 'react'; | ||
|
||
import { Box } from '@/components'; | ||
|
||
import { useConfigStore } from './useConfigStore'; | ||
|
||
export const ConfigProvider = ({ children }: PropsWithChildren) => { | ||
const { config, initConfig } = useConfigStore(); | ||
|
||
useEffect(() => { | ||
initConfig(); | ||
}, [initConfig]); | ||
|
||
if (!config) { | ||
return ( | ||
<Box $height="100vh" $width="100vw" $align="center" $justify="center"> | ||
<Loader /> | ||
</Box> | ||
); | ||
} | ||
|
||
return children; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { fetchAPI } from '@/api'; | ||
|
||
import { Config } from '../types'; | ||
|
||
export const getConfig = async (): Promise<Config> => { | ||
const response = await fetchAPI(`config/`); | ||
if (!response.ok) { | ||
throw new Error(`Couldn't fetch conf data: ${response.statusText}`); | ||
} | ||
return response.json() as Promise<Config>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './getConfig'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ConfigProvider'; | ||
export * from './useConfigStore'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Config { | ||
LANGUAGES: [string, string][]; | ||
FEATURES: { | ||
TEAMS: boolean; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { create } from 'zustand'; | ||
|
||
import { getConfig } from './api'; | ||
import { Config } from './types'; | ||
|
||
interface ConfStore { | ||
config?: Config; | ||
initConfig: () => void; | ||
} | ||
|
||
const initialState = { | ||
config: undefined, | ||
}; | ||
|
||
export const useConfigStore = create<ConfStore>((set) => ({ | ||
config: initialState.config, | ||
initConfig: () => { | ||
void getConfig() | ||
.then((config: Config) => { | ||
set({ config }); | ||
}) | ||
.catch(() => { | ||
console.error('Failed to fetch config data'); | ||
}); | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './AppProvider'; | ||
export * from './MainLayout'; | ||
export * from './PageLayout'; | ||
export * from './config'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
import MailDomains from './mail-domains'; | ||
import Teams from './teams'; | ||
import { useRouter as useNavigate } from 'next/navigation'; | ||
import { useEffect } from 'react'; | ||
|
||
export default process.env.NEXT_PUBLIC_FEATURE_TEAM === 'true' | ||
? Teams | ||
: MailDomains; | ||
import { useConfigStore } from '@/core/config'; | ||
import { NextPageWithLayout } from '@/types/next'; | ||
|
||
const Page: NextPageWithLayout = () => { | ||
const { config } = useConfigStore(); | ||
const router = useNavigate(); | ||
|
||
useEffect(() => { | ||
config?.FEATURES.TEAMS | ||
? router.push('/teams/') | ||
: router.push('mail-domains/'); | ||
}, [config?.FEATURES.TEAMS, router]); | ||
|
||
return null; | ||
}; | ||
|
||
export default Page; |