Enhance navigation and return context in exercise and training module components
Deploy Development / deploy (push) Successful in 41s
Test Suite / pytest-backend (push) Successful in 37s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 12s
Test Suite / k6 /health Baseline (push) Successful in 33s
Test Suite / playwright-tests (push) Successful in 1m17s
Deploy Development / deploy (push) Successful in 41s
Test Suite / pytest-backend (push) Successful in 37s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 12s
Test Suite / k6 /health Baseline (push) Successful in 33s
Test Suite / playwright-tests (push) Successful in 1m17s
- Introduced a new `PageReturnLink` component for consistent back navigation across pages. - Updated `SaveSelectedExercisesAsModuleModal` and `SaveExercisesAsModuleModal` to utilize `navigateWithAppReturn`, preserving navigation context when redirecting after saving. - Enhanced `TrainingModuleEditPage` and `TrainingUnitEditPage` with improved return context handling, allowing users to navigate back to their previous locations seamlessly. - Added CSS styles for the new return link to improve visual consistency and user experience.
This commit is contained in:
1 parent
14b005e9b8
commit
6e6270b717
10 files changed
+440
-15
No files matched your search
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Einheitlicher Rücksprung-Kontext für PWA-Navigation (siehe NAV_RETURN_CONTEXT_SPEC.md).
|
||||
*/
|
||||
import {
|
||||
buildPlanningHubReturnState,
|
||||
planningHubPathFromReturnState,
|
||||
} from './planningUnitRoutes'
|
||||
|
||||
export const NAV_RETURN_STATE_KEY = 'appReturn'
|
||||
export const EXERCISES_LIST_PATH = '/exercises'
|
||||
export const TRAINING_MODULES_LIST_PATH = '/planning/training-modules'
|
||||
|
||||
export function buildNavReturnContext(opts) {
|
||||
const path = String(opts?.path || '').trim()
|
||||
const label = String(opts?.label || '').trim()
|
||||
if (!path || !label) return null
|
||||
const ctx = { v: 1, path, label }
|
||||
if (opts?.kind) ctx.kind = opts.kind
|
||||
if (opts?.payload && typeof opts.payload === 'object') {
|
||||
ctx.payload = opts.payload
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
export function buildExercisesListReturnContext() {
|
||||
return buildNavReturnContext({
|
||||
path: EXERCISES_LIST_PATH,
|
||||
label: 'Zurück zur Übungsliste',
|
||||
kind: 'exerciseList',
|
||||
})
|
||||
}
|
||||
|
||||
export function buildTrainingModulesListReturnContext() {
|
||||
return buildNavReturnContext({
|
||||
path: TRAINING_MODULES_LIST_PATH,
|
||||
label: 'Zurück zur Modul-Bibliothek',
|
||||
kind: 'trainingModulesList',
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Rücksprung zur aktuellen Route (z. B. Einheiten-Editor).
|
||||
*/
|
||||
export function buildCurrentLocationReturnContext(location, label) {
|
||||
const path = `${location?.pathname || ''}${location?.search || ''}`.trim()
|
||||
if (!path) return null
|
||||
return buildNavReturnContext({
|
||||
path,
|
||||
label: String(label || 'Zurück').trim(),
|
||||
kind: 'currentLocation',
|
||||
})
|
||||
}
|
||||
|
||||
/** @param {ReturnType<typeof buildPlanningHubReturnState>|object} hubState */
|
||||
export function buildPlanningHubReturnContext(hubState = {}) {
|
||||
const payload = buildPlanningHubReturnState(hubState)
|
||||
return buildNavReturnContext({
|
||||
path: planningHubPathFromReturnState(payload),
|
||||
label: 'Zurück zur Planung',
|
||||
kind: 'planningHub',
|
||||
payload,
|
||||
})
|
||||
}
|
||||
|
||||
/** Legacy planningReturn → appReturn */
|
||||
export function appReturnFromPlanningReturn(planningReturn) {
|
||||
if (!planningReturn || planningReturn.v !== 1) return null
|
||||
return buildPlanningHubReturnContext(planningReturn)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('react-router-dom').Location|{ state?: object }|null|undefined} location
|
||||
*/
|
||||
export function readNavReturnFromLocation(location) {
|
||||
const state = location?.state
|
||||
if (!state || typeof state !== 'object') return null
|
||||
const raw = state[NAV_RETURN_STATE_KEY]
|
||||
if (raw?.v === 1 && raw.path && raw.label) return raw
|
||||
if (state.planningReturn) return appReturnFromPlanningReturn(state.planningReturn)
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('react-router-dom').Location|{ state?: object }|null|undefined} location
|
||||
* @param {{ path: string, label: string }|null|undefined} fallback
|
||||
*/
|
||||
export function resolveNavReturnTarget(location, fallback) {
|
||||
const ctx = readNavReturnFromLocation(location)
|
||||
if (ctx?.path && ctx?.label) {
|
||||
return { path: ctx.path, label: ctx.label, fromContext: true }
|
||||
}
|
||||
if (fallback?.path && fallback?.label) {
|
||||
return { path: fallback.path, label: fallback.label, fromContext: false }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('react-router-dom').NavigateFunction} navigate
|
||||
* @param {import('react-router-dom').Location|{ state?: object }|null|undefined} location
|
||||
* @param {{ path?: string, label?: string }|null|undefined} fallback
|
||||
*/
|
||||
export function goNavReturn(navigate, location, fallback) {
|
||||
const target = resolveNavReturnTarget(location, fallback)
|
||||
if (target?.fromContext && target.path) {
|
||||
navigate(target.path)
|
||||
return
|
||||
}
|
||||
if (typeof window !== 'undefined' && window.history.length > 1) {
|
||||
navigate(-1)
|
||||
return
|
||||
}
|
||||
if (fallback?.path) {
|
||||
navigate(fallback.path)
|
||||
return
|
||||
}
|
||||
navigate('/')
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('react-router-dom').NavigateFunction} navigate
|
||||
* @param {string} to
|
||||
* @param {ReturnType<typeof buildNavReturnContext>|null|undefined} returnContext
|
||||
* @param {object} [options]
|
||||
*/
|
||||
export function navigateWithAppReturn(navigate, to, returnContext, options = {}) {
|
||||
const state = { ...(options.state || {}) }
|
||||
if (returnContext) state[NAV_RETURN_STATE_KEY] = returnContext
|
||||
navigate(to, { ...options, state })
|
||||
}
|
||||
|
||||
/**
|
||||
* Bestehenden appReturn (oder Legacy planningReturn) beim Weiterleiten erhalten.
|
||||
*/
|
||||
export function preserveAppReturnOnNavigate(navigate, location, to, options = {}) {
|
||||
const existing = readNavReturnFromLocation(location)
|
||||
const state = { ...(options.state || {}) }
|
||||
if (existing) state[NAV_RETURN_STATE_KEY] = existing
|
||||
navigate(to, { ...options, state })
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
appReturnFromPlanningReturn,
|
||||
buildExercisesListReturnContext,
|
||||
buildNavReturnContext,
|
||||
buildPlanningHubReturnContext,
|
||||
goNavReturn,
|
||||
readNavReturnFromLocation,
|
||||
resolveNavReturnTarget,
|
||||
} from './navReturnContext.js'
|
||||
|
||||
describe('navReturnContext', () => {
|
||||
it('buildNavReturnContext requires path and label', () => {
|
||||
expect(buildNavReturnContext({ path: '/x', label: 'Zurück' })).toEqual({
|
||||
v: 1,
|
||||
path: '/x',
|
||||
label: 'Zurück',
|
||||
})
|
||||
expect(buildNavReturnContext({ path: '', label: 'X' })).toBeNull()
|
||||
})
|
||||
|
||||
it('buildExercisesListReturnContext', () => {
|
||||
const ctx = buildExercisesListReturnContext()
|
||||
expect(ctx.path).toBe('/exercises')
|
||||
expect(ctx.kind).toBe('exerciseList')
|
||||
})
|
||||
|
||||
it('readNavReturnFromLocation prefers appReturn', () => {
|
||||
const ctx = buildExercisesListReturnContext()
|
||||
expect(readNavReturnFromLocation({ state: { appReturn: ctx } })).toEqual(ctx)
|
||||
})
|
||||
|
||||
it('readNavReturnFromLocation bridges planningReturn', () => {
|
||||
const loc = {
|
||||
state: {
|
||||
planningReturn: {
|
||||
v: 1,
|
||||
selectedGroupId: '3',
|
||||
planView: 'list',
|
||||
calendarMonthStr: '',
|
||||
startDate: '',
|
||||
endDate: '',
|
||||
planScope: 'group',
|
||||
assignedToMeOnly: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
const ctx = readNavReturnFromLocation(loc)
|
||||
expect(ctx?.kind).toBe('planningHub')
|
||||
expect(ctx?.path).toContain('/planning')
|
||||
expect(ctx?.label).toBe('Zurück zur Planung')
|
||||
})
|
||||
|
||||
it('appReturnFromPlanningReturn', () => {
|
||||
const ctx = appReturnFromPlanningReturn({
|
||||
v: 1,
|
||||
selectedGroupId: '',
|
||||
planView: 'calendar',
|
||||
calendarMonthStr: '2026-05',
|
||||
startDate: '',
|
||||
endDate: '',
|
||||
planScope: 'group',
|
||||
assignedToMeOnly: false,
|
||||
})
|
||||
expect(ctx?.path).toContain('month=2026-05')
|
||||
})
|
||||
|
||||
it('resolveNavReturnTarget uses fallback when no state', () => {
|
||||
const fb = { path: '/planning/training-modules', label: 'Zurück zur Modul-Bibliothek' }
|
||||
expect(resolveNavReturnTarget({ state: null }, fb)).toEqual({ ...fb, fromContext: false })
|
||||
})
|
||||
|
||||
it('goNavReturn navigates to context path first', () => {
|
||||
const navigate = vi.fn()
|
||||
const ctx = buildExercisesListReturnContext()
|
||||
goNavReturn(navigate, { state: { appReturn: ctx } }, null)
|
||||
expect(navigate).toHaveBeenCalledWith('/exercises')
|
||||
})
|
||||
|
||||
it('buildPlanningHubReturnContext', () => {
|
||||
const ctx = buildPlanningHubReturnContext({ selectedGroupId: '7', planView: 'list' })
|
||||
expect(ctx?.path).toContain('group=7')
|
||||
expect(ctx?.kind).toBe('planningHub')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user