Skip to content

Commit 5a464a6

Browse files
committed
Handle loading of data in a wrapper component
1 parent 9248c38 commit 5a464a6

4 files changed

Lines changed: 54 additions & 29 deletions

File tree

markdown/container.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react'
2+
import { loadMarkdown, saveMarkdown } from './service'
3+
import { View } from './view'
4+
import { ContainerProps } from '../types'
5+
6+
export const Container: React.FC<ContainerProps> = (props) => {
7+
const [markdown, setMarkdown] = React.useState<undefined | null | string>()
8+
9+
React.useEffect(() => {
10+
loadMarkdown(props.store, props.subject.uri)
11+
.then((markdown) => setMarkdown(markdown))
12+
.catch(() => setMarkdown(null))
13+
})
14+
15+
if (typeof markdown === 'undefined') {
16+
return <section aria-busy={true}>Loading&hellip;</section>
17+
}
18+
if (markdown === null) {
19+
return <section>Error loading markdown :(</section>
20+
}
21+
22+
const saveHandler = (newMarkdown: string) => saveMarkdown(props.store, props.subject.uri, newMarkdown)
23+
24+
return (
25+
<section>
26+
<View
27+
markdown={markdown}
28+
onSave={saveHandler}
29+
/>
30+
</section>
31+
)
32+
}

markdown/index.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as ReactDOM from 'react-dom'
33
import { PaneDefinition, NewPaneOptions } from '../types'
44
import $rdf from 'rdflib'
55
import solidUi from 'solid-ui'
6-
import { saveMarkdown, loadMarkdown } from './service'
7-
import { View } from './view'
6+
import { saveMarkdown } from './service'
7+
import { Container } from './container'
88

99
const { icons, store } = solidUi
1010

@@ -26,16 +26,8 @@ export const Pane: PaneDefinition = {
2626
},
2727
render: (subject) => {
2828
const container = document.createElement('div')
29+
ReactDOM.render(<Container store={store} subject={subject}/>, container)
2930

30-
loadMarkdown(store, subject.uri).then((markdown) => {
31-
const view = (
32-
<View
33-
markdown={markdown}
34-
onSave={(newMarkdown) => saveMarkdown(store, subject.uri, newMarkdown)}
35-
/>
36-
)
37-
ReactDOM.render(view, container)
38-
})
3931
return container
4032
}
4133
}

markdown/view.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,35 @@ interface Props {
77
}
88

99
export const View: React.FC<Props> = (props) => {
10-
const [phase, setPhase] = React.useState<'loading' | 'rendering' | 'editing'>('rendering')
10+
const [phase, setPhase] = React.useState<'saving' | 'rendering' | 'editing'>('rendering')
1111
const [rawText, setRawText] = React.useState(props.markdown)
1212

1313
function storeMarkdown () {
14-
setPhase('loading')
14+
setPhase('saving')
1515
props.onSave(rawText).then(() => {
1616
setPhase('rendering')
1717
})
1818
}
1919

20-
if (phase === 'loading') {
21-
return <section aria-busy={true}>Loading&hellip;</section>
20+
if (phase === 'saving') {
21+
return <span aria-busy={true}>Loading&hellip;</span>
2222
}
2323

2424
if (phase === 'editing') {
2525
return (
26-
<section>
27-
<form onSubmit={(e) => { e.preventDefault(); storeMarkdown() }}>
28-
<textarea
29-
onChange={(e) => { setRawText(e.target.value) }}
30-
defaultValue={rawText}/>
31-
<button type="submit">RENDER</button>,
32-
</form>
33-
</section>
26+
<form onSubmit={(e) => { e.preventDefault(); storeMarkdown() }}>
27+
<textarea
28+
onChange={(e) => { setRawText(e.target.value) }}
29+
defaultValue={rawText}/>
30+
<button type="submit">RENDER</button>,
31+
</form>
3432
)
3533
}
3634

3735
return (
38-
<section>
39-
<form onSubmit={(event) => { event.preventDefault(); setPhase('editing') }}>
40-
<Markdown source={rawText}/>
41-
<button type="submit">EDIT</button>
42-
</form>
43-
</section>
36+
<form onSubmit={(event) => { event.preventDefault(); setPhase('editing') }}>
37+
<Markdown source={rawText}/>
38+
<button type="submit">EDIT</button>
39+
</form>
4440
)
4541
}

types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ export interface RevampPaneDefinition {
3939
view: (params: ViewParams) => void;
4040
label: (subject: NamedNode, store: IndexedFormula) => string | null;
4141
};
42+
43+
export interface ContainerProps {
44+
store: IndexedFormula;
45+
subject: NamedNode;
46+
}

0 commit comments

Comments
 (0)