Skip to content

Feature/load from GitHub #174

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 17 commits into from
Apr 2, 2020
Prev Previous commit
Next Next commit
replace select tutorial page
  • Loading branch information
ShMcK committed Mar 31, 2020
commit c2068994bc4dfffa555c7d3d7ff247257184ae11
52 changes: 0 additions & 52 deletions web-app/src/containers/SelectTutorial/SelectTutorial.tsx

This file was deleted.

72 changes: 0 additions & 72 deletions web-app/src/containers/SelectTutorial/TutorialItem.tsx

This file was deleted.

67 changes: 60 additions & 7 deletions web-app/src/containers/SelectTutorial/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useQuery } from '@apollo/react-hooks'
import * as React from 'react'
import * as T from 'typings'
import * as TT from 'typings/tutorial'
import ErrorView from '../../components/Error'
import LoadingPage from '../Loading'
import SelectTutorial from './SelectTutorial'
import { Form, Select } from '@alifd/next'
import useFetch from '../../services/hooks/useFetch'

const FormItem = Form.Item
const Option = Select.Option

interface ContainerProps {
send(action: T.Action): void
Expand All @@ -15,8 +16,60 @@ interface TutorialsData {
tutorials: TT.Tutorial[]
}

const SelectPageContainer = (props: ContainerProps) => {
return <div>SelectTutorial</div>
interface GitHubFetchProps {
url: string
}

const GitHubFetch = ({ url }: GitHubFetchProps) => {
const { data, error, loading } = useFetch(url)
if (loading) {
return <div>Loading...</div>
}
if (error) {
return <div>{JSON.stringify(error)}</div>
}
return <div>{JSON.stringify(data)}</div>
}

const tutorials = [
{
id: '1',
title: 'Basic Node & Express',
configUrl: 'https://raw.githubusercontent.com/coderoad/fcc-basic-node-and-express/master/coderoad-config.json',
},
{
id: '2',
title: 'Learn NPM',
configUrl: 'https://raw.githubusercontent.com/coderoad/fcc-learn-npm/master/coderoad-config.json',
},
]

interface Props {
send: any
context: any
}

const SelectTutorialPage = ({ send }: Props) => {
const [url, setUrl] = React.useState<string | null>(null)
const handleUrlChange = (value: string) => {
setUrl(value)
}
return (
<div>
<Form style={{ maxWidth: '500px' }}>
<FormItem label="Select Tutorial:">
<Select onChange={handleUrlChange} style={{ width: '100%' }}>
{tutorials.map((tutorial) => (
<Option key={tutorial.id} value={tutorial.configUrl}>
{tutorial.title}
</Option>
))}
</Select>
</FormItem>
</Form>
{url && <GitHubFetch url={url} />}
</div>
)
}

export default SelectPageContainer
export default SelectTutorialPage
23 changes: 23 additions & 0 deletions web-app/src/services/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react'

const useFetch = (url: string, options?: object) => {
const [data, setData] = React.useState(null)
const [error, setError] = React.useState(null)
const [loading, setLoading] = React.useState(true)
React.useEffect(() => {
const fetchData = async () => {
try {
const res = await fetch(url, options)
setLoading(false)
const json = await res.json()
setData(json)
} catch (error) {
setError(error)
}
}
fetchData()
}, [url])
return { data, error, loading }
}

export default useFetch
81 changes: 2 additions & 79 deletions web-app/stories/GitHubFetch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { storiesOf } from '@storybook/react'
import { action } from '@storybook/addon-actions'
import React from 'react'
import { css, jsx } from '@emotion/core'
import SelectWorkspace from '../src/containers/Check/SelectWorkspace'
import SideBarDecorator from './utils/SideBarDecorator'
import { Form, Input, Select } from '@alifd/next'

const FormItem = Form.Item
const Option = Select.Option
import SelectTutorialPage from '../src/containers/SelectTutorial'

const styles = {
container: {
Expand All @@ -16,81 +12,8 @@ const styles = {
},
}

const useFetch = (url: string, options?: object) => {
const [data, setData] = React.useState(null)
const [error, setError] = React.useState(null)
const [loading, setLoading] = React.useState(true)
React.useEffect(() => {
const fetchData = async () => {
try {
const res = await fetch(url, options)
setLoading(false)
const json = await res.json()
setData(json)
} catch (error) {
setError(error)
}
}
fetchData()
}, [url])
return { data, error, loading }
}

const GitHubFetch = ({ url }) => {
if (!url) {
return null
}
const { data, error, loading } = useFetch(url)
if (loading) {
return <div>Loading...</div>
}
if (error) {
return <div>{JSON.stringify(error)}</div>
}
return <div>{JSON.stringify(data)}</div>
}

const tutorials = [
{
id: '1',
title: 'Basic Node & Express',
configUrl: 'https://raw.githubusercontent.com/coderoad/fcc-basic-node-and-express/master/coderoad-config.json',
},
{
id: '2',
title: 'Learn NPM',
configUrl: 'https://raw.githubusercontent.com/coderoad/fcc-learn-npm/master/coderoad-config.json',
},
]

const SelectTutorial = () => {
const [url, setUrl] = React.useState(null)
const handleUrlChange = (value) => {
setUrl(value)
}
return (
<div>
<Form style={{ maxWidth: '500px' }}>
<FormItem label="Select Tutorial:">
<Select onChange={handleUrlChange} style={{ width: '100%' }}>
{tutorials.map((tutorial) => (
<Option key={tutorial.id} value={tutorial.configUrl}>
{tutorial.title}
</Option>
))}
</Select>
</FormItem>
</Form>
<GitHubFetch url={url} />
</div>
)
}

storiesOf('GitHub Fetch', module)
.addDecorator(SideBarDecorator)
.add('Request', () => {
return <GitHubFetch url={tutorials[0].configUrl} />
})
.add('Select Tutorial', () => {
return <SelectTutorial />
return <SelectTutorialPage send={action('send')} context={{}} />
})