Skip to content

Feature/progress #390

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 5 commits into from
Jul 17, 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
progress indicator continue
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jul 17, 2020
commit e192e4371d775c6d63e8cfc795da2f8a2761dc46
51 changes: 38 additions & 13 deletions web-app/src/containers/Tutorial/components/Continue.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
import * as React from 'react'
import { Dialog } from '@alifd/next'
import { css, jsx } from '@emotion/core'
import Button from '../../../components/Button'
import ProgressPie from './ProgressPie'

const styles = {
content: {
display: 'flex' as 'flex',
flexDirection: 'column',
justifyContent: 'center',
},
message: {
textAlign: 'center' as 'center',
},
}

interface Props {
title: string
current: number // level index
max: number // level count
onContinue(): void
}

const Continue = (props: Props) => {
const [modalState, setModalState] = React.useState<'none' | 'continue'>('none')
const [modalState, setModalState] = React.useState<'closed' | 'open'>('closed')

const onClose = () => {
setModalState('none')
setModalState('closed')
}

const onOpen = () => {
setModalState('open')
}

const onOk = () => {
setModalState('continue')
const onContinue = () => {
props.onContinue()
return setTimeout(() => {
setModalState('none')
}, 3000)
onClose()
}

return (
<>
<Button type="primary" size="medium" onClick={() => setModalState('continue')}>
<Button type="primary" size="medium" onClick={onOpen}>
Continue
</Button>
<Dialog
visible={modalState === 'continue'}
onOk={onOk}
onCancel={onClose}
title="Level Complete!"
visible={modalState === 'open'}
onClose={onClose}
footerActions={['ok']}
footer={false}
css={{ padding: '1rem' }}
>
<ProgressPie />
<div css={styles.content}>
<ProgressPie current={props.current} max={props.max} />
<div css={styles.message}>
<h3>{props.title}</h3>
<br />
<Button type="primary" size="large" onClick={onContinue}>
Continue
</Button>
</div>
</div>
</Dialog>
</>
)
Expand Down
29 changes: 20 additions & 9 deletions web-app/src/containers/Tutorial/components/ProgressPie.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import * as React from 'react'
import { Progress, Icon } from '@alifd/next'

const ProgressPie = () => {
interface Props {
current: number
max: number
}

const ProgressPie = (props: Props) => {
const [progress, setProgress] = React.useState(0)

React.useEffect(() => {
if (progress < 100) {
const intervals = [10, 20]
const randomInteval = intervals[Math.floor(Math.random() * intervals.length)]
setTimeout(() => {
setProgress(progress + randomInteval)
}, 200)
let timeout: any
if (progress < props.current) {
timeout = setTimeout(() => {
setProgress(progress + 1)
}, 100)
}
return () => {
if (timeout) {
clearTimeout(timeout)
}
}
}, [progress])

const progressPercent = Math.floor((progress / props.max) * 100)

return (
<Progress
percent={progress}
percent={progressPercent}
shape="circle"
textRender={() => (progress === 100 ? <Icon type="select" size="xl" /> : null)}
textRender={() => (progressPercent === 100 ? <Icon type="select" size="xl" /> : `${progressPercent}%`)}
/>
)
}
Expand Down
3 changes: 2 additions & 1 deletion web-app/src/containers/Tutorial/formatLevels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Input {
type Output = {
level: T.LevelUI
levels: T.LevelUI[]
levelIndex: number
stepIndex: number
}

Expand Down Expand Up @@ -89,7 +90,7 @@ const formatLevels = ({ progress, position, levels, testStatus }: Input): Output
if (stepIndex === -1) {
stepIndex = levels[levelIndex].steps.length
}
return { level: levelUI, levels: levelsUI, stepIndex }
return { level: levelUI, levels: levelsUI, levelIndex, stepIndex }
}

export default formatLevels
12 changes: 8 additions & 4 deletions web-app/src/containers/Tutorial/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DISPLAY_RUN_TEST_BUTTON } from '../../environment'
import formatLevels from './formatLevels'
// import SettingsPage from './containers/Settings'
import Reset from './components/Reset'
import Continue from './components/Continue'

const styles = {
header: {
Expand Down Expand Up @@ -103,7 +104,7 @@ const TutorialPage = (props: PageProps) => {
const [page, setPage] = React.useState<'level' | 'settings' | 'review'>('level')

// format level code with status for easy rendering
const { level, levels, stepIndex } = formatLevels({
const { level, levels, levelIndex, stepIndex } = formatLevels({
progress,
position,
levels: tutorial.levels,
Expand Down Expand Up @@ -154,9 +155,12 @@ const TutorialPage = (props: PageProps) => {
{/* Right */}
<div css={{ flex: 1, display: 'flex', justifyContent: 'flex-end' }}>
{level.status === 'COMPLETE' || !level.steps.length ? (
<Button style={{ marginRight: '1rem' }} type="primary" onClick={onContinue}>
Continue
</Button>
<Continue
onContinue={onContinue}
current={levelIndex + 1}
max={level.steps.length}
title={tutorial.summary.title}
/>
) : (
<StepProgress current={stepIndex} max={level.steps.length} />
)}
Expand Down
5 changes: 4 additions & 1 deletion web-app/stories/Modals.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ storiesOf('Modals', module)
.addDecorator(SideBarDecorator)
.addDecorator(withKnobs)
.add('Reset', () => <Reset onReset={action('onReset')} />)
.add('Continue', () => <Continue onContinue={action('onContinue')} />)
.add('Continue', () => <Continue title="Tutorial Title" current={9} max={11} onContinue={action('onContinue')} />)
.add('Continue Complete', () => (
<Continue title="Tutorial Title" current={11} max={11} onContinue={action('onContinue')} />
))