@@ -7,6 +7,7 @@ import { useRouter } from "vue-router/composables";
77import { isAdminUser , isAnonymousUser } from " @/api" ;
88import type { TourRequirements , TourStep as TourStepType } from " @/api/tours" ;
99import { useHistoryStore } from " @/stores/historyStore" ;
10+ import { useTourStore } from " @/stores/tourStore" ;
1011import { useUserStore } from " @/stores/userStore" ;
1112import { errorMessageAsString } from " @/utils/simple-error" ;
1213
@@ -32,7 +33,7 @@ type TourStepWithActions = TourStepType & {
3233const props = defineProps <{
3334 steps: (TourStepType | TourStepWithActions )[];
3435 requirements: TourRequirements ;
35- tourId? : string ;
36+ tourId: string ;
3637 waitingOnElement? : string | null ;
3738 onBefore? : (step : TourStepType ) => Promise <void >;
3839 onNext? : (step : TourStepType ) => Promise <void >;
@@ -42,16 +43,29 @@ const emit = defineEmits(["end-tour"]);
4243
4344const router = useRouter ();
4445
45- const currentIndex = ref (- 1 );
4646const errorMessage = ref (" " );
4747const isPlaying = ref (false );
4848
4949// Store variables
5050const historyStore = useHistoryStore ();
5151const { currentHistory, historiesLoading } = storeToRefs (historyStore );
5252const { currentUser } = storeToRefs (useUserStore ());
53+ const tourStore = useTourStore ();
54+ const { currentTour } = storeToRefs (tourStore );
5355
54- // Step variables
56+ /** The current step index
57+ *
58+ * This is set and updated based on the currently active tour in the `tourStore`.
59+ * Is `-1` if no tour is currently active (or has ended).
60+ */
61+ const currentIndex = computed ({
62+ get : () => (currentTour .value ?.step !== undefined ? currentTour .value ?.step : - 1 ),
63+ set : (val : number ) => {
64+ tourStore .setTour (props .tourId , val );
65+ },
66+ });
67+
68+ // Local step variables
5569const currentStep = computed (() => props .steps [currentIndex .value ]);
5670const numberOfSteps = computed (() => props .steps .length );
5771const isFirst = computed (() => currentIndex .value === 0 );
@@ -101,7 +115,7 @@ const modalContents = computed<{
101115 ok : async () => {
102116 endTour ();
103117 if (router ) {
104- router .push (` /login/start${ props . tourId ? ` ? redirect=/tours/${props .tourId } ` : " " } ` );
118+ router .push (` /login/start? redirect=/tours/${props .tourId } ` );
105119 }
106120 },
107121 };
@@ -117,7 +131,7 @@ const modalContents = computed<{
117131 endTour ();
118132 if (router ) {
119133 if (isAnonymousUser (currentUser .value )) {
120- router .push (` /login/start${ props . tourId ? ` ? redirect=/tours/${props .tourId } ` : " " } ` );
134+ router .push (` /login/start? redirect=/tours/${props .tourId } ` );
121135 } else {
122136 router .push (" /" );
123137 }
@@ -172,7 +186,6 @@ onUnmounted(() => {
172186
173187function start() {
174188 window .addEventListener (" keyup" , handleKeyup );
175- currentIndex .value = 0 ;
176189}
177190
178191function play(isCurrentlyPlaying : boolean ) {
@@ -228,11 +241,10 @@ async function next() {
228241 * _In the case that_ `TourRunner` _is the parent, this will unmount the component._
229242 */
230243function endTour() {
231- currentIndex . value = - 1 ;
244+ tourStore . setTour ( undefined ) ;
232245 isPlaying .value = false ;
233246 errorMessage .value = " " ;
234247
235- // IMPORTANT: This is what unmounts the `TourRunner` component when that is the parent
236248 emit (" end-tour" );
237249}
238250
0 commit comments