Skip to content

Commit

Permalink
feat: parse doc class to object
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 17, 2018
1 parent 05138bf commit e7f9cf0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 55 deletions.
4 changes: 2 additions & 2 deletions packages/docz-theme-default/src/components/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export const List = () => (
<Docs>
{docs => (
<Sidebar>
{docs.map(({ id, name, docRoute }) => (
{docs.map(({ id, name, route }) => (
<li key={id}>
<Link to={docRoute}>{name}</Link>
<Link to={route}>{name}</Link>
</li>
))}
</Sidebar>
Expand Down
4 changes: 2 additions & 2 deletions packages/docz-theme-default/src/components/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const Section = styled('div')`

export const View = () => (
<Preview>
{({ id, name, docDescription, sections }) => (
{({ id, name, description, sections }) => (
<Container key={id}>
<Title>{name}</Title>
{docDescription && <Description>{docDescription}</Description>}
{description && <Description>{description}</Description>}
{sections &&
sections.length > 0 &&
sections.map(section => (
Expand Down
27 changes: 14 additions & 13 deletions packages/docz/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ export interface Section {
render: () => ReactNode
}

export interface DocArgs {
name: string
}

export interface DocConstructorArgs {
name: string
}

export class Doc {
public id: string
public name: string
public sections: Section[]
public docDescription: string | null
public docRoute: string
public docOrder: number

public constructor({ name }: DocConstructorArgs)

public description(value: string): Doc
public section(...args: any[]): Doc
public route(value: string): Doc
public order(num: number): Doc
public toObject(): DocObj

public name: string
}

export interface DocObj {
readonly id: string
readonly name: string
readonly sections: Section[]
readonly description: string | null
readonly route: string
readonly order: number
}

/**
Expand All @@ -37,13 +38,13 @@ export class Doc {
export function createTheme(WrappedComponent: ComponentType): ComponentType

export interface PreviewProps {
children: (doc: Doc) => ReactNode
children: (doc: DocObj) => ReactNode
}

export const Preview: SFC<PreviewProps>

export interface DocsProps {
children: (docs: Doc[]) => ReactNode
children: (docs: DocObj[]) => ReactNode
}

export const Docs: SFC<DocsProps>
44 changes: 19 additions & 25 deletions packages/docz/src/Doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import { ulid } from 'ulid'
import kebabcase from 'lodash.kebabcase'

import { Section, DocConstructorArgs } from '../'
import { isFn } from './utils/helpers'
import { Section, DocConstructorArgs, DocObj } from '../'
import { docsContainer } from './DocsContainer'

const isFn = (value: any): boolean => typeof value === 'function'

export class Doc {
class Doc {
private _id: string
private _name: string
private _description: string | null
Expand All @@ -26,7 +25,9 @@ export class Doc {
return this
}

// setters
/**
* setters
*/

public description(value: string): Doc {
this._description = value
Expand Down Expand Up @@ -56,30 +57,23 @@ export class Doc {
return this
}

// getters

public get id(): string {
return this._id
}

public get name(): string {
return this._name
}

public get sections(): Section[] {
return this._sections
}

public get docDescription(): string | null {
return this._description
}

public get docRoute(): string {
return this._route
}

public get docOrder(): number {
return this._order
/**
* getters
*/

public toObject(): DocObj {
return {
description: this._description,
id: this._id,
name: this._name,
order: this._order,
route: this._route,
sections: this._sections,
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions packages/docz/src/components/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import * as React from 'react'
import { SFC } from 'react'
import { Subscribe } from 'unstated'

import { Doc, DocsProps } from '../../'
import { Doc, DocObj, DocsProps } from '../../'
import { DocsContainer } from '../DocsContainer'
import { isFn } from '../utils/helpers'

const sortByOrder = (a: Doc, b: Doc) => b.docOrder - a.docOrder
const sortByOrder = (a: DocObj, b: DocObj) => b.order - a.order

export const Docs: SFC<DocsProps> = ({ children }) => (
<Subscribe to={[DocsContainer]}>
{({ state }) => {
const docs: Doc[] = Array.from(state.docs.values())
const sortedDocs: Doc[] = docs.sort(sortByOrder)
const docsObj: DocObj[] = docs.map(doc => doc.toObject())
const sortedDocs: DocObj[] = docsObj.sort(sortByOrder)

return children(sortedDocs)
return isFn(children) && children(sortedDocs)
}}
</Subscribe>
)
24 changes: 15 additions & 9 deletions packages/docz/src/components/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Switch, Route } from 'react-router-dom'

import { PreviewProps, Doc } from '../../'
import { DocsContainer } from '../DocsContainer'
import { isFn } from '../utils/helpers'

export const Preview: SFC<PreviewProps> = ({ children }) => (
<Subscribe to={[DocsContainer]}>
Expand All @@ -13,15 +14,20 @@ export const Preview: SFC<PreviewProps> = ({ children }) => (

return (
<Switch>
{docs.length > 0 &&
docs.map(doc => (
<Route
exact
key={doc.id}
path={doc.docRoute}
render={() => children(doc)}
/>
))}
{docs &&
docs.length > 0 &&
docs.map(doc => {
const docObj = doc.toObject()

return (
<Route
exact
key={docObj.id}
path={docObj.route}
render={() => isFn(children) && children(docObj)}
/>
)
})}
</Switch>
)
}}
Expand Down
1 change: 1 addition & 0 deletions packages/docz/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isFn = (value: any): boolean => typeof value === 'function'

0 comments on commit e7f9cf0

Please sign in to comment.