From 67b4687ccb3b21a143a10b0773e408e5828e8485 Mon Sep 17 00:00:00 2001 From: Brandon Farmer Date: Thu, 6 Dec 2018 11:13:18 -0800 Subject: [PATCH] Handling onboarding with react router --- ui/src/Setup.tsx | 37 +++--- ui/src/index.tsx | 12 +- ui/src/onboarding/actions/steps.ts | 33 +----- .../configureStep/ConfigureDataSourceStep.tsx | 59 ++++++---- .../selectionStep/SelectDataSourceStep.tsx | 42 ++++--- .../containers/OnboardingWizard.tsx | 23 ++-- .../containers/OnboardingWizardPage.tsx | 109 ++++++++++++++++++ ui/src/onboarding/reducers/steps.ts | 8 -- ui/src/onboarding/utils/index.ts | 3 + 9 files changed, 215 insertions(+), 111 deletions(-) create mode 100644 ui/src/onboarding/containers/OnboardingWizardPage.tsx create mode 100644 ui/src/onboarding/utils/index.ts diff --git a/ui/src/Setup.tsx b/ui/src/Setup.tsx index 41f88f6cf7..2eb06416ec 100644 --- a/ui/src/Setup.tsx +++ b/ui/src/Setup.tsx @@ -1,6 +1,7 @@ // Libraries import React, {ReactElement, PureComponent} from 'react' import {connect} from 'react-redux' +import {InjectedRouter} from 'react-router' // APIs import {getSetupStatus} from 'src/onboarding/apis' @@ -10,8 +11,9 @@ import {notify as notifyAction} from 'src/shared/actions/notifications' // Components import {ErrorHandling} from 'src/shared/decorators/errors' -import OnboardingWizard from 'src/onboarding/containers/OnboardingWizard' -import Notifications from 'src/shared/components/notifications/Notifications' + +// Utils +import {isOnboardingURL} from 'src/onboarding/utils' // Types import {Notification, NotificationFunc, RemoteDataState} from 'src/types' @@ -24,6 +26,7 @@ interface State { interface Props { links: Links + router: InjectedRouter children: ReactElement notify: (message: Notification | NotificationFunc) => void } @@ -40,35 +43,35 @@ export class Setup extends PureComponent { } public async componentDidMount() { - const {links} = this.props + const {links, router} = this.props + + if (isOnboardingURL()) { + this.setState({ + loading: RemoteDataState.Done, + }) + return + } + const isSetupAllowed = await getSetupStatus(links.setup) this.setState({ loading: RemoteDataState.Done, - isSetupComplete: !isSetupAllowed, }) + + if (!isSetupAllowed) { + return + } + + router.push('/onboarding/0') } public render() { - const {isSetupComplete} = this.state if (this.isLoading) { return
- } - if (!isSetupComplete) { - return ( -
- - -
- ) } else { return this.props.children && React.cloneElement(this.props.children) } } - public handleCompleteSetup = () => { - this.setState({isSetupComplete: true}) - } - private get isLoading(): boolean { const {loading} = this.state return ( diff --git a/ui/src/index.tsx b/ui/src/index.tsx index 07d177e70b..13546dd837 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -33,6 +33,8 @@ import GetLinks from 'src/shared/containers/GetLinks' import GetMe from 'src/shared/containers/GetMe' import SourcesPage from 'src/sources/components/SourcesPage' +import OnboardingWizardPage from 'src/onboarding/containers/OnboardingWizardPage' + // Actions import {disablePresentationMode} from 'src/shared/actions/app' @@ -77,6 +79,14 @@ class Root extends PureComponent { + + @@ -116,8 +126,8 @@ class Root extends PureComponent { - + ) diff --git a/ui/src/onboarding/actions/steps.ts b/ui/src/onboarding/actions/steps.ts index 76adb1faaa..ba4ae57347 100644 --- a/ui/src/onboarding/actions/steps.ts +++ b/ui/src/onboarding/actions/steps.ts @@ -4,12 +4,7 @@ import {StepStatus} from 'src/clockface/constants/wizard' // Types import {SetupParams} from 'src/onboarding/apis' -export type Action = - | SetSetupParams - | IncrementCurrentStepIndex - | DecrementCurrentStepIndex - | SetCurrentStepIndex - | SetStepStatus +export type Action = SetSetupParams | SetStepStatus interface SetSetupParams { type: 'SET_SETUP_PARAMS' @@ -21,32 +16,6 @@ export const setSetupParams = (setupParams: SetupParams): SetSetupParams => ({ payload: {setupParams}, }) -interface SetCurrentStepIndex { - type: 'SET_CURRENT_STEP_INDEX' - payload: {index: number} -} - -export const setCurrentStepIndex = (index: number): SetCurrentStepIndex => ({ - type: 'SET_CURRENT_STEP_INDEX', - payload: {index}, -}) - -interface IncrementCurrentStepIndex { - type: 'INCREMENT_CURRENT_STEP_INDEX' -} - -export const incrementCurrentStepIndex = (): IncrementCurrentStepIndex => ({ - type: 'INCREMENT_CURRENT_STEP_INDEX', -}) - -interface DecrementCurrentStepIndex { - type: 'DECREMENT_CURRENT_STEP_INDEX' -} - -export const decrementCurrentStepIndex = (): DecrementCurrentStepIndex => ({ - type: 'DECREMENT_CURRENT_STEP_INDEX', -}) - interface SetStepStatus { type: 'SET_STEP_STATUS' payload: {index: number; status: StepStatus} diff --git a/ui/src/onboarding/components/configureStep/ConfigureDataSourceStep.tsx b/ui/src/onboarding/components/configureStep/ConfigureDataSourceStep.tsx index b0addf893a..d621b5c9cc 100644 --- a/ui/src/onboarding/components/configureStep/ConfigureDataSourceStep.tsx +++ b/ui/src/onboarding/components/configureStep/ConfigureDataSourceStep.tsx @@ -1,6 +1,7 @@ // Libraries import React, {PureComponent} from 'react' import _ from 'lodash' +import {withRouter, WithRouterProps} from 'react-router' // Components import {ErrorHandling} from 'src/shared/decorators/errors' @@ -16,27 +17,44 @@ import ConfigureDataSourceSwitcher from 'src/onboarding/components/configureStep import {OnboardingStepProps} from 'src/onboarding/containers/OnboardingWizard' import {TelegrafPlugin, DataLoaderType} from 'src/types/v2/dataLoaders' -export interface Props extends OnboardingStepProps { +export interface OwnProps extends OnboardingStepProps { telegrafPlugins: TelegrafPlugin[] type: DataLoaderType } -interface State { - currentDataSourceIndex: number +interface RouterProps { + params: { + stepID: string + substepID: string + } } +type Props = OwnProps & WithRouterProps & RouterProps + @ErrorHandling -class ConfigureDataSourceStep extends PureComponent { +class ConfigureDataSourceStep extends PureComponent { constructor(props: Props) { super(props) + } - this.state = { - currentDataSourceIndex: 0, + public componentDidMount() { + const { + router, + params: {stepID, substepID}, + } = this.props + + if (substepID === undefined) { + router.replace(`/onboarding/${stepID}/0`) } } public render() { - const {telegrafPlugins, type, setupParams} = this.props + const { + telegrafPlugins, + type, + params: {substepID}, + setupParams, + } = this.props return (
@@ -44,8 +62,8 @@ class ConfigureDataSourceStep extends PureComponent { bucket={_.get(setupParams, 'bucket', '')} org={_.get(setupParams, 'org', '')} telegrafPlugins={telegrafPlugins} - currentIndex={this.state.currentDataSourceIndex} dataLoaderType={type} + currentIndex={+substepID} />