-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
781c2bf
commit 04ff0d6
Showing
4 changed files
with
143 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as React from 'react' | ||
import { ReactNode, ComponentType, Component } from 'react' | ||
|
||
import { isFn } from './Docs' | ||
|
||
interface Props { | ||
as: ComponentType<any> | ||
getInitialData?: (props: any) => any | ||
} | ||
|
||
interface State { | ||
loading: boolean | ||
data: any | ||
error: any | ||
} | ||
|
||
export class AsyncComponent extends Component<Props, State> { | ||
public state = { | ||
loading: false, | ||
error: null, | ||
data: {}, | ||
} | ||
|
||
public componentDidMount(): void { | ||
this.fetch() | ||
} | ||
|
||
public render(): ReactNode { | ||
const { as: Comp = 'div', getInitialData, ...props } = this.props | ||
const { data, loading, error } = this.state | ||
|
||
return <Comp {...props} data={{ ...data, loading, error }} /> | ||
} | ||
|
||
private async fetch(): Promise<void> { | ||
const { getInitialData } = this.props | ||
|
||
if (getInitialData && isFn(getInitialData)) { | ||
this.setState({ loading: true }) | ||
try { | ||
const data = await getInitialData(this.props) | ||
this.setState({ | ||
data, | ||
error: null, | ||
loading: false, | ||
}) | ||
} catch (error) { | ||
this.setState({ | ||
error, | ||
data: {}, | ||
loading: false, | ||
}) | ||
} | ||
} | ||
} | ||
} |
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,67 @@ | ||
import * as React from 'react' | ||
import { SFC } from 'react' | ||
import { withMDXComponents } from '@mdx-js/tag/dist/mdx-provider' | ||
import importedComponent from 'react-imported-component' | ||
|
||
import { ImportMap, EntryMap } from '../state' | ||
import { ComponentsMap } from './DocPreview' | ||
import { AsyncComponent } from './AsyncComponent' | ||
|
||
interface AsyncRouteProps { | ||
components: ComponentsMap | ||
path: string | ||
imports: ImportMap | ||
entries: EntryMap | ||
} | ||
|
||
export const AsyncRoute: SFC<AsyncRouteProps> = ({ | ||
components, | ||
path, | ||
imports, | ||
entries, | ||
...routeProps | ||
}) => { | ||
const Page: any = components.page | ||
const LoadingComponent: any = components.loading | ||
const entry = entries && entries[path] | ||
const props = { ...routeProps, doc: entry } | ||
|
||
const loadImport = async () => { | ||
const { default: Component, getInitialData } = await imports[path]() | ||
|
||
const ExportedComponent: any = (props: any) => <Component {...props} /> | ||
ExportedComponent.getInitialData = getInitialData | ||
return ExportedComponent | ||
} | ||
|
||
const Component = withMDXComponents( | ||
importedComponent(loadImport, { | ||
LoadingComponent, | ||
render(Component: any, state, defaultProps: any): React.ReactNode { | ||
const { history, match, location, doc } = defaultProps | ||
const { LoadingComponent: Loading } = defaultProps | ||
const props = { history, match, location, doc } | ||
|
||
if (state === 'done') { | ||
return ( | ||
<AsyncComponent | ||
{...props} | ||
as={Component} | ||
getInitialData={Component.getInitialData} | ||
/> | ||
) | ||
} | ||
|
||
return <Loading {...props} /> | ||
}, | ||
}) | ||
) | ||
|
||
return Page ? ( | ||
<Page {...props}> | ||
<Component {...props} /> | ||
</Page> | ||
) : ( | ||
<Component {...props} /> | ||
) | ||
} |
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