Skip to content

Feature/tutorial from file #179

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 4 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

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
load json file
  • Loading branch information
ShMcK committed Apr 3, 2020
commit b4dff54e0e3f4b02b805cf84434c7def675690c2
4 changes: 2 additions & 2 deletions web-app/src/containers/SelectTutorial/LoadTutorialSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Loading from '../Loading'

interface Props {
url: string
onSetDataFromUrl(data: TT.Tutorial): void
onLoadSummary(data: TT.Tutorial): void
}

const LoadTutorialSummary = (props: Props) => {
Expand All @@ -20,7 +20,7 @@ const LoadTutorialSummary = (props: Props) => {
if (!data) {
return <div>No data returned for tutorial</div>
}
props.onSetDataFromUrl(data)
props.onLoadSummary(data)
return null
}

Expand Down
10 changes: 6 additions & 4 deletions web-app/src/containers/SelectTutorial/SelectTutorialForm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as TT from 'typings/tutorial'
import * as React from 'react'
import { Radio } from '@alifd/next'
import TutorialSelect from './forms/TutorialSelect'
Expand All @@ -16,7 +17,8 @@ interface Props {
tab: string
setTab(tab: 'list' | 'url'): void
url: string | null
onTutorialLoad(url: string): void
onTutorialLoadFromUrl(url: string): void
onLoadSummary(data: TT.Tutorial | null): void
}

const SelectTutorialForm = (props: Props) => {
Expand All @@ -35,9 +37,9 @@ const SelectTutorialForm = (props: Props) => {
</Radio.Group>
<br />
<br />
{props.tab === 'list' && <TutorialSelect onTutorialLoad={props.onTutorialLoad} />}
{props.tab === 'url' && <TutorialUrl onTutorialLoad={props.onTutorialLoad} defaultUrl={props.url || ''} />}
{props.tab === 'file' && <TutorialFile onTutorialLoad={props.onTutorialLoad} />}
{props.tab === 'list' && <TutorialSelect onTutorialLoad={props.onTutorialLoadFromUrl} />}
{props.tab === 'url' && <TutorialUrl onTutorialLoad={props.onTutorialLoadFromUrl} defaultUrl={props.url || ''} />}
{props.tab === 'file' && <TutorialFile onLoadSummary={props.onLoadSummary} />}
</div>
)
}
Expand Down
13 changes: 6 additions & 7 deletions web-app/src/containers/SelectTutorial/forms/TutorialFile.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import * as React from 'react'
import * as TT from 'typings/tutorial'
import { Form } from '@alifd/next'

const FormItem = Form.Item

interface Props {
onTutorialLoad(url: string): void
onLoadSummary(data: TT.Tutorial): void
}

const TutorialFile = (props: Props) => {
const [json, setJson] = React.useState<JSON | null>(null)

const onChange = (evt: any) => {
const files = evt.target.files

Expand All @@ -20,9 +19,9 @@ const TutorialFile = (props: Props) => {
const uploadedFile = files[0]
const reader = new FileReader()
reader.onload = (e: any) => {
// TODO: handle errors from bad JSON
const fileJson: JSON = JSON.parse(e.target.result)
setJson(fileJson)
// TODO: handle errors from invalid JSON
const fileJson: TT.Tutorial = JSON.parse(e.target.result)
props.onLoadSummary(fileJson)
}
reader.readAsText(uploadedFile)
}
Expand All @@ -32,7 +31,7 @@ const TutorialFile = (props: Props) => {
<FormItem label="Load coderoad config.json">
<input type="file" accept="application/json" onChange={onChange} />
</FormItem>
&nbsp;&nbsp;
<br />
</Form>
)
}
Expand Down
16 changes: 12 additions & 4 deletions web-app/src/containers/SelectTutorial/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const SelectTutorialPage = (props: Props) => {
},
})
}
const onTutorialLoad = (url: string) => {
const onTutorialLoadFromUrl = (url: string) => {
setUrl(url)
setPage('loading')
}
const onSetDataFromUrl = (d: TT.Tutorial) => {
const onLoadSummary = (d: TT.Tutorial) => {
setData(d)
setPage('summary')
}
Expand All @@ -48,8 +48,16 @@ const SelectTutorialPage = (props: Props) => {
}
return (
<div css={styles.page}>
{page === 'form' && <SelectTutorialForm url={url} onTutorialLoad={onTutorialLoad} tab={tab} setTab={setTab} />}
{page === 'loading' && url && <LoadTutorialSummary url={url} onSetDataFromUrl={onSetDataFromUrl} />}
{page === 'form' && (
<SelectTutorialForm
url={url}
onLoadSummary={onLoadSummary}
onTutorialLoadFromUrl={onTutorialLoadFromUrl}
tab={tab}
setTab={setTab}
/>
)}
{page === 'loading' && url && <LoadTutorialSummary url={url} onLoadSummary={onLoadSummary} />}
{page === 'summary' && data && <TutorialOverview onNext={onNext} tutorial={data} onClear={onClear} />}
</div>
)
Expand Down