Skip to content

Commit

Permalink
fix(withalien): pass alienModule
Browse files Browse the repository at this point in the history
  • Loading branch information
aneurysmjs committed Jan 12, 2020
1 parent fde8b3b commit d25f368
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/app/components/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import withAlien from '~/store/config/alienStore/withAlien';
import HomeComponent from './Home';

const Home: FunctionComponent = () =>
withAlien(HomeComponent, () => import('~/store/modules/products/products.module'));
withAlien(HomeComponent, {
id: 'products',
getModule: () => import('~/store/modules/products/products.module'),
});

export default Home;
2 changes: 1 addition & 1 deletion src/app/store/config/alienStore/useAlien.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ReduxModule<T = {}> {

export type AlienResult = Omit<ReduxModule, 'reducers'>;

interface AlienModule<T> {
export interface AlienModule<T = {}> {
id: string;
getModule: () => Promise<ReduxModule<T>>;
initialActions?: Array<string>;
Expand Down
7 changes: 5 additions & 2 deletions src/app/store/config/alienStore/withAlien.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ describe('test "withAlien"', () => {
const Example = (): ReactElement => <div>some component</div>;

const getModule = (): Promise<typeof reduxModule> => Promise.resolve(reduxModule);

const { result, waitForNextUpdate } = renderHook(() => withAlien(Example, getModule), {
const alienModule = {
id: 'with',
getModule,
};
const { result, waitForNextUpdate } = renderHook(() => withAlien(Example, alienModule), {
wrapper,
});

Expand Down
16 changes: 4 additions & 12 deletions src/app/store/config/alienStore/withAlien.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import React, { ReactElement, ComponentType, ReactNode } from 'react';
import React, { ReactElement, ComponentType } from 'react';

import useAlien, { ReduxModule, AlienResult } from './useAlien';
import useAlien, { AlienResult, AlienModule } from './useAlien';

interface WithAlienProps {
children?: ReactNode;
actions: AlienResult['actions'];
}

function WithAlien<P extends object>(
Component: ComponentType<P>,
getModule: () => Promise<ReduxModule>,
alienModule: AlienModule,
): ReactElement<P & WithAlienProps> | null {
// it complains just beacause the function doesn't start with Capital case
// so it thinks it not a React component
// eslint-disable-next-line react-hooks/rules-of-hooks
const alienResult = useAlien({
getModule,
});
const alienResult = useAlien(alienModule);

if (alienResult) {
return <Component {...(alienResult as P)} />;
}

return null;
// return <Component {...((alienResult && alienResult) as P)} />;
// return typeof props.children === 'function' ? props.children() : props.children;
}

export default WithAlien;

0 comments on commit d25f368

Please sign in to comment.