Skip to content

Commit

Permalink
fix guided tour alert to not show up if skipped once
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesis09 committed Sep 23, 2020
1 parent 7f22abc commit 650afaf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const GuidedTourMastheadTrigger: React.FC<GuidedTourMastheadTriggerProps> = ({ c
const { tourDispatch, tour } = React.useContext(TourContext);
if (!tour) return null;
return (
<button className={className} type="button" onClick={() => tourDispatch(TourActions.start)}>
<button
className={className}
type="button"
onClick={() => tourDispatch({ type: TourActions.start })}
>
Guided Tour
</button>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ const StepComponent: React.FC<StepComponentProps> = ({
showStepBadge={showStepBadge}
nextButtonText={nextButtonText}
backButtonText={backButtonText}
onClose={() => tourDispatch(TourActions.complete)}
onClose={() => tourDispatch({ type: TourActions.complete })}
onNext={() =>
step > totalSteps ? tourDispatch(TourActions.complete) : tourDispatch(TourActions.next)
step > totalSteps
? tourDispatch({ type: TourActions.complete })
: tourDispatch({ type: TourActions.next })
}
onBack={() =>
step === 0 ? tourDispatch(TourActions.complete) : tourDispatch(TourActions.back)
step === 0
? tourDispatch({ type: TourActions.complete })
: tourDispatch({ type: TourActions.back })
}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('guided-tour-context', () => {
});

it('should return startTour as true for StartAction', () => {
const result = tourReducer(mockState, TourActions.start);
const result = tourReducer(mockState, { type: TourActions.start });
expect(result).toEqual({
startTour: true,
completedTour: false,
Expand All @@ -22,17 +22,17 @@ describe('guided-tour-context', () => {
});

it('should return increment in stepNumber for next action', () => {
const result = tourReducer(mockState, TourActions.next);
const result = tourReducer(mockState, { type: TourActions.next });
expect(result.stepNumber).toEqual(mockState.stepNumber + 1);
});

it('should return decrease in stepNumber for back action', () => {
const result = tourReducer(mockState, TourActions.back);
const result = tourReducer(mockState, { type: TourActions.back });
expect(result.stepNumber).toEqual(mockState.stepNumber - 1);
});

it('should return completedTour as true for complete action', () => {
const result = tourReducer(mockState, TourActions.complete);
const result = tourReducer(mockState, { type: TourActions.complete });
expect(result).toEqual({
startTour: false,
completedTour: true,
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/console-app/src/components/tour/const.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const TOUR_LOCAL_STORAGE_KEY = 'getting-started-tour';

export enum TourActions {
initialize = 'initialize',
start = 'start',
next = 'next',
back = 'back',
Expand Down
20 changes: 16 additions & 4 deletions frontend/packages/console-app/src/components/tour/tour-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, useReducer, Reducer, Dispatch, ReducerAction } from 'react';
import { createContext, useReducer, useRef, Reducer, Dispatch, ReducerAction } from 'react';
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version
import { useSelector } from 'react-redux';
Expand All @@ -16,9 +16,16 @@ import {
} from './utils';
import { TourDataType, Step } from './type';

export const tourReducer = (state: TourState, action: TourActions) => {
type TourStateAction = { type: TourActions; payload?: { completed?: boolean } };
export const tourReducer = (state: TourState, action: TourStateAction) => {
const { stepNumber } = state;
switch (action) {
switch (action.type) {
case TourActions.initialize:
return {
completedTour: action.payload.completed,
stepNumber: 0,
startTour: !action.payload.completed,
};
case TourActions.start:
return { startTour: true, completedTour: false, stepNumber: 0 };
case TourActions.next:
Expand All @@ -39,7 +46,7 @@ export const tourReducer = (state: TourState, action: TourActions) => {
}
};

type TourReducer = Reducer<TourState, TourActions>;
type TourReducer = Reducer<TourState, TourStateAction>;

type TourContextType = {
tourState?: TourState;
Expand Down Expand Up @@ -84,11 +91,16 @@ export const useTourValuesForContext = (): TourContextType => {
setTourCompletionLocalStorageDataForPerspective(activePerspective, true);
}
};
const activePerspectiveRef = useRef<string>(activePerspective);
const [tourState, tourDispatch] = useReducer<TourReducer>(tourReducer, {
completedTour: completed,
stepNumber: 0,
startTour: !completed,
});
if (activePerspective !== activePerspectiveRef.current) {
tourDispatch({ type: TourActions.initialize, payload: { completed } });
activePerspectiveRef.current = activePerspective;
}
if (!tour) return { tour: null };
const {
properties: {
Expand Down

0 comments on commit 650afaf

Please sign in to comment.