fix(dashboard): open upgrade modal without triggering unsaved-changes prompt (#919)
* fix(dashboard): open the upgrade modal without navigating Opening the upgrade modal set `?upgrade=true` via `goto`, which SvelteKit treats as a navigation. On pages guarded by UnsavedChanges (the exercise editor, course settings, certificates) that triggered the native "You have unsaved changes" confirm before the modal could appear, and cancelling blocked it entirely. The modal now lives in app state instead of the URL, so opening and closing it never navigates. `?upgrade=true` is still honoured for billing emails and the post-checkout return URL: it is consumed once on load and stripped from the address bar with shallow routing. Because checkout is an external redirect that unloads the page, editors can register a handoff to persist in-progress work first. The exercise editor stashes its draft, and the checkout return URL now points back at the page the user upgraded from, so their questions are restored instead of lost. Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com> * fix(dashboard): close question type select before opening upgrade modal The select dropdown sits at z-250 while the upgrade dialog is z-200, so opening the modal while the menu was still mounted left it covering the dialog. Close the select first, wait for its close animation, then open the upgrade modal. Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com> * fix(dashboard): close question-type select before opening upgrade modal The premium-type button left the select open, and its z-menu-elevated content sat in front of the upgrade dialog. Close the select first and wait for the close animation before opening the modal. Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com> --------- Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>
This commit is contained in:
co-authored by
Rotimi Best
parent
564b0735f0
commit
4cd2f5f295
@@ -0,0 +1,79 @@
|
||||
import type { QuestionnaireState } from '$features/course/components/exercise/store';
|
||||
import { questionnaire } from '$features/course/components/exercise/store';
|
||||
|
||||
const EXERCISE_DRAFT_STORAGE_PREFIX = 'classroomio:exercise-draft';
|
||||
|
||||
/** A draft only bridges a redirect out of the app and back, so it goes stale quickly. */
|
||||
const EXERCISE_DRAFT_MAX_AGE_MS = 60 * 60 * 1000;
|
||||
|
||||
interface StoredExerciseDraft {
|
||||
savedAt: number;
|
||||
questionnaire: QuestionnaireState;
|
||||
}
|
||||
|
||||
function getStorageKey(courseId: string, exerciseId: string) {
|
||||
return `${EXERCISE_DRAFT_STORAGE_PREFIX}:${courseId}:${exerciseId}`;
|
||||
}
|
||||
|
||||
export function clearExerciseDraft(courseId: string, exerciseId: string) {
|
||||
try {
|
||||
localStorage.removeItem(getStorageKey(courseId, exerciseId));
|
||||
} catch (error) {
|
||||
console.error('clearExerciseDraft error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stashes the editor state before the browser leaves the app (e.g. for plan checkout), so the
|
||||
* teacher's in-progress questions survive the round trip.
|
||||
*/
|
||||
export function saveExerciseDraft(courseId: string, exerciseId: string, state: QuestionnaireState) {
|
||||
if (!courseId || !exerciseId) return;
|
||||
|
||||
const draft: StoredExerciseDraft = { savedAt: Date.now(), questionnaire: state };
|
||||
|
||||
try {
|
||||
localStorage.setItem(getStorageKey(courseId, exerciseId), JSON.stringify(draft));
|
||||
} catch (error) {
|
||||
console.error('saveExerciseDraft error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function loadExerciseDraft(courseId: string, exerciseId: string): QuestionnaireState | null {
|
||||
if (!courseId || !exerciseId) return null;
|
||||
|
||||
try {
|
||||
const stored = localStorage.getItem(getStorageKey(courseId, exerciseId));
|
||||
if (!stored) return null;
|
||||
|
||||
const draft = JSON.parse(stored) as StoredExerciseDraft | null;
|
||||
const isExpired = Date.now() - (draft?.savedAt ?? 0) >= EXERCISE_DRAFT_MAX_AGE_MS;
|
||||
|
||||
if (!draft?.questionnaire || isExpired) {
|
||||
clearExerciseDraft(courseId, exerciseId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return draft.questionnaire;
|
||||
} catch (error) {
|
||||
console.error('loadExerciseDraft error:', error);
|
||||
clearExerciseDraft(courseId, exerciseId);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces freshly hydrated server data with a stashed draft, if one is waiting. Must run after
|
||||
* `hydrateExercisePageData`. Returns whether a draft was restored.
|
||||
*/
|
||||
export function restoreExerciseDraft(courseId: string, exerciseId: string) {
|
||||
const draft = loadExerciseDraft(courseId, exerciseId);
|
||||
|
||||
if (!draft) return false;
|
||||
|
||||
clearExerciseDraft(courseId, exerciseId);
|
||||
questionnaire.set(draft);
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user