11import * as React from 'react'
22import useFetch from '../../services/hooks/useFetch'
33import { Form , Select } from '@alifd/next'
4+ import { TUTORIAL_URL } from '../../environment'
45
56const FormItem = Form . Item
67const Option = Select . Option
78
8- // configurable url to tutorials
9- const tutorialUrl = 'https://raw.githubusercontent.com/coderoad/tutorials/master/tutorials.json'
9+ const styles = {
10+ formWrapper :{
11+ padding :'1rem' ,
12+ width :'100%' ,
13+ height :'auto' ,
14+ backgroundColor :'yellow' ,
15+ } ,
16+ }
1017
1118type TutorialList = Array < { id :string ; title :string ; configUrl :string } >
1219
@@ -15,28 +22,25 @@ interface Props {
1522}
1623
1724const SelectTutorialForm = ( props :Props ) => {
18- const { data, error, loading} = useFetch < TutorialList > ( tutorialUrl )
19- if ( loading ) {
20- return < div > Loading...</ div >
21- }
22- if ( error ) {
23- return < div > { JSON . stringify ( error ) } </ div >
24- }
25- if ( ! data ) {
26- return < div > No data returned</ div >
27- }
25+ // load tutorial from a path to a tutorial list json
26+ const { data, error, loading} = useFetch < TutorialList > ( TUTORIAL_URL )
27+ // TODO: display errors
28+ const selectState = loading ?'loading' :error || ! data ?'error' :undefined
2829return (
29- < Form style = { { maxWidth :'500px' } } >
30- < FormItem label = "Select Tutorial:" >
31- < Select onChange = { props . onUrlChange } style = { { width :'100%' } } placeholder = "Tutorials..." >
32- { data . map ( ( tutorial ) => (
33- < Option key = { tutorial . id } value = { tutorial . configUrl } >
34- { tutorial . title }
35- </ Option >
36- ) ) }
37- </ Select >
38- </ FormItem >
39- </ Form >
30+ < div css = { styles . formWrapper } >
31+ < Form style = { { maxWidth :'500px' } } >
32+ < FormItem label = "Select Tutorial:" >
33+ < Select onChange = { props . onUrlChange } style = { { width :'100%' } } placeholder = "Tutorials..." state = { selectState } >
34+ { data &&
35+ data . map ( ( tutorial ) => (
36+ < Option key = { tutorial . id } value = { tutorial . configUrl } >
37+ { tutorial . title }
38+ </ Option >
39+ ) ) }
40+ </ Select >
41+ </ FormItem >
42+ </ Form >
43+ </ div >
4044)
4145}
4246