Skip to content

Commit

Permalink
feat(Home): add optional rocUrl and database props
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jun 21, 2021
1 parent 10bcb99 commit 9c30eed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
17 changes: 15 additions & 2 deletions src/components/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<HomeContextProvider>
<HomeContextProvider rocUrl={props.rocUrl} database={props.database}>
<div className="flex flex-col w-screen h-screen">
<HomeHeader />
<div className="flex flex-row flex-1 mt-2 border-t border-neutral-300 ">
Expand Down
43 changes: 29 additions & 14 deletions src/components/home/HomeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -54,22 +60,31 @@ const homeReducer: Reducer<HomeContextType, HomeContextAction> = produce(
},
);

const homeContext = createContext(initialHomeContext);
const homeContext = createContext(getInitialHomeContext());
const homeDispatchContext = createContext<Dispatch<HomeContextAction>>(() => {
// 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 (
<homeContext.Provider value={homeState}>
<homeDispatchContext.Provider value={dispatch}>
<RocProvider
database={initialHomeContext.database}
url={initialHomeContext.rocUrl}
>
<RocProvider url={homeState.rocUrl} database={homeState.database}>
{props.children}
</RocProvider>
</homeDispatchContext.Provider>
Expand Down

0 comments on commit 9c30eed

Please sign in to comment.