Skip to content

Feature/tutorial setup #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
setup cond rendering route
  • Loading branch information
ShMcK committed Jun 9, 2019
commit 864a26ed7732f8ad05c2c7a692d2db820d09f5f5
13 changes: 12 additions & 1 deletion web-app/src/Routes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as React from 'react'
import * as CR from 'typings'
import NewPage from './components/New'
import ContinuePage from './components/Continue'
import Cond from './components/Cond'

interface ReceivedEvent {
data: CR.Action
Expand All @@ -24,8 +27,16 @@ const Routes = () => {
window.removeEventListener(listener, handleEvent)
}
})

return (
<div>State: {JSON.stringify(state)}</div>
<div>
<Cond state={state} path="SelectTutorial.NewTutorial">
<NewPage onNew={() => console.log('new!')} />
</Cond>
<Cond state={state} path="SelectTutorial.ContinueTutorial">
<ContinuePage onContinue={() => console.log('continue!')} tutorials={[]} />
</Cond>
</div>
)
}

Expand Down
17 changes: 17 additions & 0 deletions web-app/src/components/Cond/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react'
import { stateMatch } from './utils/state'

interface Props {
state: any
path: string
children: React.ReactElement
}

const Cond = (props: Props) => {
if (!stateMatch(props.state, props.path)) {
return null
}
return props.children
}

export default Cond
12 changes: 12 additions & 0 deletions web-app/src/components/Cond/utils/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function stateMatch(state: any, statePath: string) {
let current = state
let paths = statePath.split('.')
try {
for (const p of paths) {
current = current[p]
}
} catch (error) {
return false
}
return current !== undefined
}