Skip to content

Validate tutorial url #351

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 2 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions web-app/src/containers/SelectTutorial/LoadTutorialSummary.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import * as React from 'react'
import { Dialog } from '@alifd/next'
import useFetch from '../../services/hooks/useFetch'
import * as TT from 'typings/tutorial'
import LoadingPage from '../Loading'

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

const LoadTutorialSummary = (props: Props) => {
const { data, error, loading } = useFetch<TT.Tutorial>(props.url)
if (!data) {
return (
<Dialog
title="Tutorial Not Found"
visible={true}
closeable={false}
footerActions={['ok']}
onOk={props.onReturnToSelection}
>
No data returned for tutorial
</Dialog>
)
}
if (loading) {
return <LoadingPage text="Loading tutorial summary..." />
}
if (error) {
console.log(`Failed to load tutorial summary: ${error}`)
return <div>Error loading summary</div>
}
if (!data) {
return <div>No data returned for tutorial</div>
}
props.onLoadSummary(data)
return null
}
Expand Down
14 changes: 12 additions & 2 deletions web-app/src/containers/SelectTutorial/forms/TutorialUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ interface Props {
onTutorialLoad(url: string): void
}

const urlRegex = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)\.json/

const TutorialUrl = (props: Props) => {
const [url, setUrl] = React.useState(props.defaultUrl)
const [inputState, setInputState] = React.useState<undefined | 'success' | 'error'>()

const onSubmit = (e: any) => {
e.preventDefault()
logger(`Tutorial url: ${url}`)
props.onTutorialLoad(url)
}

const handleChange = (text: string) => {
setUrl(text)
!!text.match(urlRegex) ? setInputState('success') : setInputState('error')
}

return (
// @ts-ignore seems to be an onSubmit event ts error in lib
<Form style={{ maxWidth: '600px' }} onSubmit={onSubmit}>
Expand All @@ -25,11 +34,12 @@ const TutorialUrl = (props: Props) => {
size="large"
placeholder="https://raw.githubusercontent.com/coderoad/fcc-learn-npm/master/coderoad-config.json"
defaultValue={props.defaultUrl}
onChange={setUrl}
onChange={handleChange}
state={inputState}
aria-label="input url path to coderoad config.json"
/>
</FormItem>
<Button htmlType="submit" type="primary">
<Button htmlType="submit" type="primary" disabled={inputState !== 'success'}>
Load
</Button>{' '}
&nbsp;&nbsp;
Expand Down
4 changes: 3 additions & 1 deletion web-app/src/containers/SelectTutorial/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ const SelectTutorialPage = (props: Props) => {
setTab={setTab}
/>
)}
{page === 'loading' && url && <LoadTutorialSummary url={url} onLoadSummary={onLoadSummary} />}
{page === 'loading' && url && (
<LoadTutorialSummary url={url} onLoadSummary={onLoadSummary} onReturnToSelection={() => setPage('form')} />
)}
{page === 'summary' && data && <TutorialOverview onNext={onNext} tutorial={data} onClear={onClear} />}
</div>
)
Expand Down