diff --git a/src/components/navigation/TreeNav.tsx b/src/components/navigation/TreeNav.tsx index d853947604e4a36e7c0f1f26fdb418864bdd32c8..e8f15134ee22a41f213f7d9b98577c121ba8e343 100644 --- a/src/components/navigation/TreeNav.tsx +++ b/src/components/navigation/TreeNav.tsx @@ -108,8 +108,7 @@ export default function TreeNav({ const dataForRouteResult = useDataForRoute(useDataForRouteParams) const {fetch, loading} = dataForRouteResult const fetchedData = - dataForRouteResult.data && - getResponseForPath(splitPath, dataForRouteResult.data).at(-1) + dataForRouteResult.data && getResponseForPath(path, dataForRouteResult.data) const hasFetchedData = !!fetchedData const {index, fullMatch, navigate} = useRoute() @@ -132,9 +131,8 @@ export default function TreeNav({ const availableRouteData = useAvailableRouteData<JSONObject, JSONObject>() const currentAvailableRouteData = useMemo( () => - availableRouteData && - getResponseForPath(splitPath, availableRouteData, false).at(-1), - [availableRouteData, splitPath], + availableRouteData && getResponseForPath(path, availableRouteData, false), + [availableRouteData, path], ) const availableRouteDataIsSufficient = currentAvailableRouteData && diff --git a/src/components/routing/loader.test.ts b/src/components/routing/loader.test.ts index f3456506befd429873f2e7472806a1995c4fcfc1..83062565fc40236ca45605bfbcab8547588c38b7 100644 --- a/src/components/routing/loader.test.ts +++ b/src/components/routing/loader.test.ts @@ -20,21 +20,21 @@ describe('getIndexKey', () => { describe('getResponseForPath', () => { it('works with indexed key', () => { - const result = getResponseForPath(['key[0]'], {key: [{value: 'value'}]}) - expect(result).toEqual([{value: 'value'}]) + const result = getResponseForPath('key[0]', {key: [{value: 'value'}]}) + expect(result).toEqual({value: 'value'}) }) it('works with non indexed key', () => { - const result = getResponseForPath(['key'], {key: {value: 'value'}}) - expect(result).toEqual([{value: 'value'}]) + const result = getResponseForPath('key', {key: {value: 'value'}}) + expect(result).toEqual({value: 'value'}) }) it('works with nested keys', () => { - const result = getResponseForPath(['key', 'nested'], { + const result = getResponseForPath('key/nested', { key: {nested: {value: 'value'}}, }) - expect(result).toEqual([{nested: {value: 'value'}}, {value: 'value'}]) + expect(result).toEqual({value: 'value'}) }) it('throws error if key does not exist', () => { - expect(() => getResponseForPath(['key'], {})).toThrow( + expect(() => getResponseForPath('key', {})).toThrow( 'The entity with id key does not exist.', ) }) @@ -43,7 +43,7 @@ describe('getResponseForPath', () => { describe('getResonseForRoute', () => { it('works', () => { const result = getResponseForRoute( - [{route: {requestKey: 'key'}} as unknown as RouteMatch], + [{route: {requestKey: 'key', request: '*'}} as unknown as RouteMatch], {key: {value: 'value'}}, ) expect(result).toEqual([{value: 'value'}]) @@ -131,7 +131,7 @@ describe('createRequestForRoute', () => { }) it('works with requestPath function', () => { const result = createRequestForRoute( - [{route: {requestKey: () => 'some/path'}} as unknown as RouteMatch], + [{route: {requestPath: () => 'some/path'}} as unknown as RouteMatch], () => ({requestKey: 'value'}), ) expect(result).toEqual({some: {path: {requestKey: 'value'}}}) diff --git a/src/components/routing/loader.ts b/src/components/routing/loader.ts index c5c04bddd73477ca9ecc96edb5b6fb7ebd61e2b2..cc7196815f994af2ca20192ac267b8605ff633c6 100644 --- a/src/components/routing/loader.ts +++ b/src/components/routing/loader.ts @@ -30,12 +30,13 @@ export function getIndexedKey(key: string): [string, number | undefined] { } export function getResponseForPath( - path: string[], + path: string, response: JSONObject, raiseDoesNotExistError: boolean = true, -): (JSONObject | undefined)[] { +): JSONObject | undefined { let currentData: JSONObject | undefined = response - const result = path.map((pathSegment) => { + let result + path.split('/').forEach((pathSegment) => { const [key, index] = getIndexedKey(pathSegment) if (index !== undefined) { currentData = (currentData?.[key] as JSONObject[] | undefined)?.[index] @@ -45,7 +46,7 @@ export function getResponseForPath( if (currentData === undefined && raiseDoesNotExistError) { throw new DoesNotExistError(`The entity with id ${key} does not exist.`) } - return currentData + result = currentData }) return result } @@ -54,8 +55,21 @@ export function getResponseForRoute( match: RouteMatch[], response: JSONObject, ): (JSONObject | undefined)[] { - const path = match.map((match) => getRequestKey(match)) - return getResponseForPath(path, response) + let parentPath: string | undefined + const requestPaths: (string | undefined)[] = match.map((match) => { + const path = getRequestPath(match, parentPath) + if (path !== undefined) { + parentPath = path + } + return path + }) + return requestPaths.map((path, index) => { + if (path === undefined) { + return undefined + } + const hasRequest = match[index].route.request !== undefined + return getResponseForPath(path, response, hasRequest) + }) } function getRequestPath( diff --git a/src/pages/entry/EntryOverview.tsx b/src/pages/entry/EntryOverview.tsx index ba03cbd19b2f356fd193c2b72e83db4f9057d4f9..71b424053358916266ed677a10649191df7c6076 100644 --- a/src/pages/entry/EntryOverview.tsx +++ b/src/pages/entry/EntryOverview.tsx @@ -21,11 +21,12 @@ import { MSectionResponse, } from '../../models/graphResponseModels' import {Section} from '../../utils/metainfo' +import {assert} from '../../utils/utils' import UploadMetadataEditor from '../upload/UploadMetadataEditor' import EntryDataEditor from './EntryDataEditor' import EntryMetadataEditor from './EntryMetadataEditor' import EntrySubSectionTable from './EntrySubSectionTable' -import entryRoute, {archiveRequest, archiveRoute} from './entryRoute' +import entryRoute, {archiveRequest} from './entryRoute' function EntryOverviewEditor() { const {archive: archiveData} = useRouteData(entryRoute) @@ -91,6 +92,8 @@ function EntryOverviewEditor() { let content: React.ReactNode = '' if (error) { + // eslint-disable-next-line no-console + console.error(error) content = <ErrorMessage error={error} /> } else { if (isPage) { @@ -117,9 +120,17 @@ function EntryOverviewEditor() { export default function EntryOverview() { const {url} = useRoute() - const {upload_id, mainfile_path} = useRouteData(entryRoute) + const { + upload_id, + mainfile_path, + archive: rootSectionData, + } = useRouteData(entryRoute) + assert( + rootSectionData !== undefined, + 'An entry should always have a root section', + ) + const {isPage, isSelect} = useSelect() - const rootSectionData = useRouteData(archiveRoute) const {isScrolled} = usePage() const actions = (