Skip to content

Commit

Permalink
fix(user-onboarding): pause video on next/prev, add tourStepChange ob…
Browse files Browse the repository at this point in the history
…servable, change names to kebab case for models (sourcefuse#474)

gh-0
  • Loading branch information
barleendhaliwal authored Jan 19, 2022
1 parent 34a17b1 commit 076d12e
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
TourButton,
TourStep,
TourState,
filterFunction,
FilterFunction,
Props,
Status,
TourStepChange,
TourComplete,
} from '../models';
import {Router, NavigationEnd, Event as NavigationEvent} from '@angular/router';
import {interval, Subject} from 'rxjs';
Expand All @@ -18,12 +20,10 @@ import {filter, map, take, takeWhile, tap} from 'rxjs/operators';
providedIn: 'root',
})
export class TourServiceService {
private readonly tourComplete = new Subject<{
index: number;
tour: Shepherd.Tour;
tourId: string;
}>();
private readonly tourComplete = new Subject<TourComplete>();
tourComplete$ = this.tourComplete.asObservable();
private readonly tourStepChange = new Subject<TourStepChange>();
tourStepChange$ = this.tourStepChange.asObservable();
private readonly interval = 100;

private tour = new Shepherd.Tour({
Expand Down Expand Up @@ -71,23 +71,49 @@ export class TourServiceService {
}
}

private navigationCheckNext(event: NavigationEvent, e: TourStep): void {
private navigationCheckNext(
event: NavigationEvent,
e: TourStep,
tourInstance: Tour,
currentStepId: string,
previousStepId: string,
): void {
if (event instanceof NavigationEnd && event.url === e.currentRoute) {
this.waitForElement(e.attachTo.element, 0)
.then(() => {
this.tour.next();
this.tourStepChange.next({
tourId: tourInstance.tourId,
currentStepId,
previousStepId,
moveForward: true,
});
this.pauseAllVideos();
})
.catch(() => {
throw new Error('Error detected in loading');
});
}
}

private navigationCheckBack(event: NavigationEvent, e: TourStep): void {
private navigationCheckBack(
event: NavigationEvent,
e: TourStep,
tourInstance: Tour,
currentStepId: string,
previousStepId: string,
): void {
if (event instanceof NavigationEnd && event.url === e.currentRoute) {
this.waitForElement(e.attachTo.element, 0)
.then(() => {
this.tour.back();
this.tourStepChange.next({
tourId: tourInstance.tourId,
currentStepId,
previousStepId,
moveForward: false,
});
this.pauseAllVideos();
})
.catch(() => {
throw new Error('Error detected in loading');
Expand Down Expand Up @@ -166,7 +192,13 @@ export class TourServiceService {
const nextStep = tourInstance.tourSteps.filter(
ts => ts.id === e.nextStepId,
)[0];
this.navigationCheckNext(event, nextStep);
this.navigationCheckNext(
event,
nextStep,
tourInstance,
e.nextStepId,
e.id,
);
});
};
const wrapperPrev = () => {
Expand All @@ -186,7 +218,14 @@ export class TourServiceService {
const prevStep = tourInstance.tourSteps.filter(
ts => ts.id === e.prevStepId,
)[0];
this.navigationCheckBack(event, prevStep);

this.navigationCheckBack(
event,
prevStep,
tourInstance,
e.prevStepId,
e.id,
);
});
};
const wrapperNormalNext = () => {
Expand All @@ -202,6 +241,13 @@ export class TourServiceService {
})
.subscribe();
this.tour.next();
this.tourStepChange.next({
tourId: tourInstance.tourId,
currentStepId: e.nextStepId,
previousStepId: e.id,
moveForward: true,
});
this.pauseAllVideos();
};
const wrapperNormalPrev = () => {
this.tourStoreService
Expand All @@ -216,6 +262,13 @@ export class TourServiceService {
})
.subscribe();
this.tour.back();
this.tourStepChange.next({
tourId: tourInstance.tourId,
currentStepId: e.prevStepId,
previousStepId: e.id,
moveForward: true,
});
this.pauseAllVideos();
};
this.actionAssignment(
e,
Expand Down Expand Up @@ -266,7 +319,13 @@ export class TourServiceService {
.subscribe();
this.router.navigate([er.nextRoute]);
this.router.events.subscribe((event: NavigationEvent) => {
this.navigationCheckNext(event, er);
this.navigationCheckNext(
event,
er,
tourInstance,
er.nextStepId,
er.id,
);
});
};
const wrapperPrevRemoved = () => {
Expand All @@ -283,7 +342,13 @@ export class TourServiceService {
.subscribe();
this.router.navigate([er.prevRoute]);
this.router.events.subscribe((event: NavigationEvent) => {
this.navigationCheckBack(event, er);
this.navigationCheckBack(
event,
er,
tourInstance,
er.prevStepId,
er.id,
);
});
};
const wrapperNormalNextRemoved = () => {
Expand All @@ -299,6 +364,13 @@ export class TourServiceService {
})
.subscribe();
this.tour.next();
this.tourStepChange.next({
tourId: tourInstance.tourId,
currentStepId: er.nextStepId,
previousStepId: er.id,
moveForward: true,
});
this.pauseAllVideos();
};
const wrapperNormalPrevRemoved = () => {
this.tourStoreService
Expand All @@ -313,6 +385,13 @@ export class TourServiceService {
})
.subscribe();
this.tour.back();
this.tourStepChange.next({
tourId: tourInstance.tourId,
currentStepId: er.nextStepId,
previousStepId: er.id,
moveForward: false,
});
this.pauseAllVideos();
};
this.actionAssignment(
er,
Expand All @@ -337,7 +416,7 @@ export class TourServiceService {
tourId: string,
params?: {[key: string]: string},
props?: Props,
filterFn?: filterFunction,
filterFn?: FilterFunction,
): void {
this.tourStoreService
.loadTour({
Expand All @@ -346,14 +425,17 @@ export class TourServiceService {
})
.subscribe(tourInstance => {
if (tourInstance && tourInstance.tourSteps.length > 0) {
let steps = JSON.stringify(tourInstance.tourSteps);
Object.keys(params).forEach(key => {
steps = steps.replace(
new RegExp(`\\{\\{${key}\\}\\}`),
params[key],
);
});
tourInstance.tourSteps = JSON.parse(steps);
if (params) {
let steps = JSON.stringify(tourInstance.tourSteps);

Object.keys(params).forEach(key => {
steps = steps.replace(
new RegExp(`\\{\\{${key}\\}\\}`),
params[key],
);
});
tourInstance.tourSteps = JSON.parse(steps);
}
this.tour = new Shepherd.Tour({
useModalOverlay: true,
defaultStepOptions: {
Expand Down Expand Up @@ -431,4 +513,7 @@ export class TourServiceService {
return Status.InProgress;
}
}
private pauseAllVideos() {
document.querySelectorAll('video').forEach(vid => vid.pause());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {TourStep} from './tour-step';

export type FilterFunction = (steps: TourStep[]) => TourStep[];

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export * from './stateParameters';
export * from './state-parameters';
export * from './tour';
export * from './tourButton';
export * from './tourParameters';
export * from './tourStep';
export * from './parameterModel';
export * from './videoModel';
export * from './filterFunction';
export * from './tour-button';
export * from './tour-parameters';
export * from './tour-step';
export * from './parameter-model';
export * from './video-model';
export * from './filter-function';
export * from './props';
export * from './status';
export * from './tour-step-change';
export * from './tour-complete';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Shepherd from 'shepherd.js';
export type TourComplete = {
index: number;
tour: Shepherd.Tour;
tourId: string;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TourStep} from './tourStep';
import {TourStep} from './tour-step';

export interface SaveTourParameters {
tourId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type TourStepChange = {
tourId: string;
currentStepId: string;
previousStepId: string;
moveForward: boolean;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TourButton} from './tourButton';
import {TourButton} from './tour-button';

export interface TourStep {
id: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Props} from './props';
import {Status} from './status';
import {TourStep} from './tourStep';
import {TourStep} from './tour-step';
export interface TourState {
sessionId: string;
status: Status;
Expand Down

0 comments on commit 076d12e

Please sign in to comment.