Skip to content

Commit 745d4cc

Browse files
authored
Merge pull request #179 from ShMcK/feature/tutorial-from-file
Feature/tutorial from file
2 parents 82973bc + a1f554b commit 745d4cc

File tree

5 files changed

+101
-21
lines changed

5 files changed

+101
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import * as React from 'react'
22
import useFetch from '../../services/hooks/useFetch'
33
import * as TT from 'typings/tutorial'
4-
import TutorialOverview from '../../components/TutorialOverview'
54
import Loading from '../Loading'
65

76
interface Props {
87
url: string
9-
send: any
10-
onClear(): void
8+
onLoadSummary(data: TT.Tutorial): void
119
}
1210

1311
const LoadTutorialSummary = (props: Props) => {
@@ -22,15 +20,8 @@ const LoadTutorialSummary = (props: Props) => {
2220
if (!data) {
2321
return <div>No data returned for tutorial</div>
2422
}
25-
const onNext = () => {
26-
props.send({
27-
type: 'TUTORIAL_START',
28-
payload: {
29-
tutorial: data,
30-
},
31-
})
32-
}
33-
return <TutorialOverview onNext={onNext} tutorial={data} onClear={props.onClear} />
23+
props.onLoadSummary(data)
24+
return null
3425
}
3526

3627
export default LoadTutorialSummary

web-app/src/containers/SelectTutorial/SelectTutorialForm.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import * as TT from 'typings/tutorial'
12
import * as React from 'react'
23
import { Radio } from '@alifd/next'
34
import TutorialSelect from './forms/TutorialSelect'
45
import TutorialUrl from './forms/TutorialUrl'
6+
import TutorialFile from './forms/TutorialFile'
57

68
const styles = {
79
formWrapper: {
@@ -15,7 +17,8 @@ interface Props {
1517
tab: string
1618
setTab(tab: 'list' | 'url'): void
1719
url: string | null
18-
onTutorialLoad(url: string): void
20+
onTutorialLoadFromUrl(url: string): void
21+
onLoadSummary(data: TT.Tutorial | null): void
1922
}
2023

2124
const SelectTutorialForm = (props: Props) => {
@@ -30,12 +33,13 @@ const SelectTutorialForm = (props: Props) => {
3033
>
3134
<Radio value="list">List</Radio>
3235
<Radio value="url">URL</Radio>
33-
{/* <Radio value="file">File</Radio> */}
36+
<Radio value="file">File</Radio>
3437
</Radio.Group>
3538
<br />
3639
<br />
37-
{props.tab === 'list' && <TutorialSelect onTutorialLoad={props.onTutorialLoad} />}
38-
{props.tab === 'url' && <TutorialUrl onTutorialLoad={props.onTutorialLoad} defaultUrl={props.url || ''} />}
40+
{props.tab === 'list' && <TutorialSelect onTutorialLoad={props.onTutorialLoadFromUrl} />}
41+
{props.tab === 'url' && <TutorialUrl onTutorialLoad={props.onTutorialLoadFromUrl} defaultUrl={props.url || ''} />}
42+
{props.tab === 'file' && <TutorialFile onLoadSummary={props.onLoadSummary} />}
3943
</div>
4044
)
4145
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as React from 'react'
2+
import * as TT from 'typings/tutorial'
3+
import { Form } from '@alifd/next'
4+
5+
const FormItem = Form.Item
6+
7+
interface Props {
8+
onLoadSummary(data: TT.Tutorial): void
9+
}
10+
11+
const styles = {
12+
uploadFileButton: {
13+
padding: '0.3rem 0.5rem',
14+
outline: '1px dotted rgb(51, 51, 51)',
15+
borderRadius: '0.2rem',
16+
fontWeight: 400,
17+
fontFamily:
18+
'-apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;',
19+
},
20+
}
21+
22+
const TutorialFile = (props: Props) => {
23+
const onChange = (evt: any) => {
24+
evt.preventDefault()
25+
const files = evt.target.files
26+
27+
if (!files.length) {
28+
alert('No file select')
29+
return
30+
}
31+
const uploadedFile = files[0]
32+
const reader = new FileReader()
33+
reader.onload = (e: any) => {
34+
// TODO: handle errors from invalid JSON
35+
const fileJson: TT.Tutorial = JSON.parse(e.target.result)
36+
props.onLoadSummary(fileJson)
37+
}
38+
reader.readAsText(uploadedFile)
39+
}
40+
41+
return (
42+
<Form style={{ maxWidth: '600px' }}>
43+
<FormItem label="Load coderoad config.json">
44+
<br />
45+
<label style={styles.uploadFileButton}>
46+
Upload
47+
<input style={{ display: 'none' }} type="file" accept="application/json" onChange={onChange} />
48+
</label>
49+
<br />
50+
</FormItem>
51+
<br />
52+
</Form>
53+
)
54+
}
55+
56+
export default TutorialFile

web-app/src/containers/SelectTutorial/forms/TutorialUrl.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import { Button, Form, Radio, Input } from '@alifd/next'
2+
import { Button, Form, Input } from '@alifd/next'
33

44
const FormItem = Form.Item
55

web-app/src/containers/SelectTutorial/index.tsx

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as TT from 'typings/tutorial'
12
import * as React from 'react'
23
import SelectTutorialForm from './SelectTutorialForm'
4+
import TutorialOverview from '../../components/TutorialOverview'
35
import LoadTutorialSummary from './LoadTutorialSummary'
46

57
const styles = {
@@ -19,17 +21,44 @@ interface Props {
1921
}
2022

2123
const SelectTutorialPage = (props: Props) => {
22-
const [page, setPage] = React.useState<'form' | 'summary'>('form')
24+
const [data, setData] = React.useState<TT.Tutorial | null>()
25+
const [page, setPage] = React.useState<'form' | 'loading' | 'summary'>('form')
2326
const [tab, setTab] = React.useState<'list' | 'url'>('list')
2427
const [url, setUrl] = React.useState<string | null>(null)
25-
const onTutorialLoad = (url: string) => {
28+
29+
const onNext = () => {
30+
props.send({
31+
type: 'TUTORIAL_START',
32+
payload: {
33+
tutorial: data,
34+
},
35+
})
36+
}
37+
const onTutorialLoadFromUrl = (url: string) => {
2638
setUrl(url)
39+
setPage('loading')
40+
}
41+
const onLoadSummary = (d: TT.Tutorial) => {
42+
setData(d)
2743
setPage('summary')
2844
}
45+
const onClear = () => {
46+
setData(null)
47+
setPage('form')
48+
}
2949
return (
3050
<div css={styles.page}>
31-
{page === 'form' && <SelectTutorialForm url={url} onTutorialLoad={onTutorialLoad} tab={tab} setTab={setTab} />}
32-
{page === 'summary' && url && <LoadTutorialSummary url={url} send={props.send} onClear={() => setPage('form')} />}
51+
{page === 'form' && (
52+
<SelectTutorialForm
53+
url={url}
54+
onLoadSummary={onLoadSummary}
55+
onTutorialLoadFromUrl={onTutorialLoadFromUrl}
56+
tab={tab}
57+
setTab={setTab}
58+
/>
59+
)}
60+
{page === 'loading' && url && <LoadTutorialSummary url={url} onLoadSummary={onLoadSummary} />}
61+
{page === 'summary' && data && <TutorialOverview onNext={onNext} tutorial={data} onClear={onClear} />}
3362
</div>
3463
)
3564
}

0 commit comments

Comments
 (0)