8000 setup cond rendering route · benjaminthedev/coderoad-vscode@864a26e · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 864a26e

Browse files
committed
setup cond rendering route
1 parent 9c530c9 commit 864a26e

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

web-app/src/Routes.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as React from 'react'
22
import * as CR from 'typings'
3+
import NewPage from './components/New'
4+
import ContinuePage from './components/Continue'
5+
import Cond from './components/Cond'
36

47
interface ReceivedEvent {
58
data: CR.Action
@@ -24,8 +27,16 @@ const Routes = () => {
2427
window.removeEventListener(listener, handleEvent)
2528
}
2629
})
30+
2731
return (
28-
<div>State: {JSON.stringify(state)}</div>
32+
<div>
33+
<Cond state={state} path="SelectTutorial.NewTutorial">
34+
<NewPage onNew={() => console.log('new!')} />
35+
</Cond>
36+
<Cond state={state} path="SelectTutorial.ContinueTutorial">
37+
<ContinuePage onContinue={() => console.log('continue!')} tutorials={[]} />
38+
</Cond>
39+
</div>
2940
)
3041
}
3142

web-app/src/components/Cond/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as React from 'react'
2+
import { stateMatch } from './utils/state'
3+
4+
interface Props {
5+
state: any
6+
path: string
7+
children: React.ReactElement
8+
}
9+
10+
const Cond = (props: Props) => {
11+
if (!stateMatch(props.state, props.path)) {
12+
return null
13+
}
14+
return props.children
15+
}
16+
17+
export default Cond
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function stateMatch(state: any, statePath: string) {
2+
let current = state
3+
let paths = statePath.split('.')
4+
try {
5+
for (const p of paths) {
6+
current = current[p]
7+
}
8+
} catch (error) {
9+
return false
10+
}
11+
return current !== undefined
12+
}

0 commit comments

Comments
 (0)
0