From 9c30eed3cf3cf997107a82801d4a4a944d7e7944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 21 Jun 2021 12:37:13 +0200 Subject: [PATCH] feat(Home): add optional rocUrl and database props --- src/components/home/Home.tsx | 17 ++++++++++-- src/components/home/HomeContext.tsx | 43 +++++++++++++++++++---------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/components/home/Home.tsx b/src/components/home/Home.tsx index 7c48d92..cda3d03 100644 --- a/src/components/home/Home.tsx +++ b/src/components/home/Home.tsx @@ -4,9 +4,22 @@ import HomeIframe from './HomeIframe'; import HomeNoSample from './HomeNoSample'; import HomeSamples from './HomeSamples'; -export function Home() { +export interface HomeProps { + /** + * URL of the rest-on-couch instance. + * @default 'http://localhost:3000/api/fake-roc' + */ + rocUrl?: string; + /** + * Name of the rest-on-couch database. + * @default 'eln' + */ + database?: string; +} + +export function Home(props: HomeProps) { return ( - +
diff --git a/src/components/home/HomeContext.tsx b/src/components/home/HomeContext.tsx index 1dd5940..a2b40af 100644 --- a/src/components/home/HomeContext.tsx +++ b/src/components/home/HomeContext.tsx @@ -21,13 +21,19 @@ interface HomeContextType { selectedSample: string | null; } -const initialHomeContext: HomeContextType = { - rocUrl: 'http://localhost:3000/api/fake-roc', - database: 'eln', - iframePage: (getItem('dev-home-iframePage') as string) || '/dev/base-page', - iframeMode: 'closed', - selectedSample: null, -}; +function getInitialHomeContext( + config: { rocUrl?: string; database?: string } = {}, +): HomeContextType { + const { rocUrl = 'http://localhost:3000/api/fake-roc', database = 'eln' } = + config; + return { + rocUrl, + database, + iframePage: (getItem('dev-home-iframePage') as string) || '/dev/base-page', + iframeMode: 'closed', + selectedSample: null, + }; +} type HomeContextAction = | ActionType<'SELECT_SAMPLE', string> @@ -54,22 +60,31 @@ const homeReducer: Reducer = produce( }, ); -const homeContext = createContext(initialHomeContext); +const homeContext = createContext(getInitialHomeContext()); const homeDispatchContext = createContext>(() => { // noop }); -export function HomeContextProvider(props: { children: ReactNode }) { - const [homeState, dispatch] = useReducer(homeReducer, initialHomeContext); +interface HomeContextProviderProps { + children: ReactNode; + // eslint-disable-next-line react/no-unused-prop-types + rocUrl?: string; + // eslint-disable-next-line react/no-unused-prop-types + database?: string; +} + +export function HomeContextProvider(props: HomeContextProviderProps) { + const [homeState, dispatch] = useReducer( + homeReducer, + props, + getInitialHomeContext, + ); useSaveToLocalStorage('dev-home-iframePage', homeState.iframePage); return ( - + {props.children}