Skip to content

Commit d58d7e4

Browse files
authored
Merge pull request #41 from navikt/version-latest
Versjonsvisning, siste versjon + ulik contentId
2 parents 2aa177e + b9f5814 commit d58d7e4

File tree

7 files changed

+48
-25
lines changed

7 files changed

+48
-25
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ npm run dev -C legacy-archive
4242
npm run dev -C xp-archive
4343
```
4444

45+
5. Relaterte applikasjoner
46+
Vi bruker tjenester i nav-enonicxp for å hente ut innholdsinformasjon og nav-enonicxp-frontend for å rendre innholdet, så de må kjøres opp lokalt for å få hentet informasjon.
47+
4548
### Troubleshooting
4649

4750
Hvis du har problemer med at css og js ikke laster ved første oppstart i dev-modus, prøv å start appen i prod-modus en gang og se om det løser problemet.
@@ -50,6 +53,8 @@ Hvis du har problemer med at css og js ikke laster ved første oppstart i dev-mo
5053
npm run start-local -C xp-archive
5154
```
5255

56+
Hvis iframes ikke vises, sjekk om du får cors-issues i srcdoc. Disse vil oppstå hvis du kjører nav-enonicxp-frontend i dev-modus og ikke i prod-modus.
57+
5358
## <a name="enonic-legacy"></a> Enonic CMS legacy arkiv (2006-2019)
5459

5560
### Oppsett for utvikling

xp-archive/client/contentHtmlView/ContentHtmlView.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const ContentHtmlView = ({ html }: Props) => {
1111
}
1212
return (
1313
<div className={style.content}>
14-
<iframe srcDoc={html} title="my-iframe" className={style.iframe}></iframe>
14+
<iframe srcDoc={html} className={style.iframe}></iframe>
1515
</div>
1616
);
1717
};

xp-archive/client/contentView/ContentView.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ const getDisplayComponent = (viewVariant: ViewVariant, data: ContentServiceRespo
2626
};
2727

2828
export const ContentView = () => {
29-
const { selectedContentId, selectedVersionId } = useAppState();
29+
const { selectedContentId, selectedVersion } = useAppState();
30+
31+
const fetchId = selectedVersion?.nodeId ?? selectedContentId;
3032

3133
const { data, isLoading } = useFetchContent({
32-
id: selectedContentId || '',
33-
versionId: selectedVersionId,
34+
id: fetchId || '',
35+
versionId: selectedVersion?.versionId ?? undefined,
3436
});
3537

3638
const [selectedView, setSelectedView] = useState<ViewVariant>('json');
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { createContext } from 'react';
2+
import { SelectedVersion } from './AppStateProvider';
23

34
export type AppState = {
45
selectedContentId?: string;
56
setSelectedContentId: (contentId: string) => void;
6-
selectedVersionId?: string;
7-
setSelectedVersionId: (versionId: string) => void;
8-
7+
selectedVersion?: SelectedVersion;
8+
setSelectedVersion: (selectedVersion: SelectedVersion | undefined) => void;
99
};
1010

11-
export const AppStateContext = createContext<AppState>({ setSelectedContentId: () => ({}), setSelectedVersionId: () => ({}) });
11+
export const AppStateContext = createContext<AppState>({
12+
setSelectedContentId: () => ({}),
13+
setSelectedVersion: () => ({}),
14+
});

xp-archive/client/context/appState/AppStateProvider.tsx

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
import React, { useCallback, useState } from 'react';
1+
import React, { useCallback, useMemo, useState } from 'react';
22
import { AppStateContext } from './AppStateContext';
33

44
type Props = {
55
children: React.ReactNode;
66
};
77

8+
export type SelectedVersion = {
9+
versionId: string;
10+
nodeId?: string;
11+
};
12+
813
export const AppStateProvider = ({ children }: Props) => {
914
const [selectedContentId, setSelectedContentId] = useState<string>();
10-
const [selectedVersionId, setSelectedVersionId] = useState<string>();
15+
const [selectedVersion, setSelectedVersion] = useState<SelectedVersion>();
1116

12-
const updateSelectedContentId = useCallback((selectedContentId: string) => {
13-
setSelectedVersionId(undefined);
14-
setSelectedContentId(selectedContentId);
15-
}, []);
16-
const setSelectedVersionIdMemoized = useCallback(setSelectedVersionId, [setSelectedVersionId]);
17+
const setSelectedContentIdMemoized = useCallback(setSelectedContentId, [setSelectedContentId]);
18+
const setSelectedVersionMemoized = useCallback(setSelectedVersion, [setSelectedVersion]);
19+
const selectedVersionMemoized = useMemo(() => selectedVersion, [selectedVersion]);
1720

1821
return (
1922
<AppStateContext.Provider
2023
value={{
2124
selectedContentId,
22-
setSelectedContentId: updateSelectedContentId,
23-
selectedVersionId,
24-
setSelectedVersionId: setSelectedVersionIdMemoized,
25+
setSelectedContentId: setSelectedContentIdMemoized,
26+
selectedVersion: selectedVersionMemoized,
27+
setSelectedVersion: setSelectedVersionMemoized,
2528
}}
2629
>
2730
{children}

xp-archive/client/context/appState/useAppState.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { useContext } from 'react';
22
import { AppStateContext } from './AppStateContext';
33

44
export const useAppState = () => {
5-
const { selectedContentId, setSelectedContentId, selectedVersionId, setSelectedVersionId } =
5+
const { selectedContentId, selectedVersion, setSelectedContentId, setSelectedVersion } =
66
useContext(AppStateContext);
77

88
return {
99
selectedContentId,
10+
selectedVersion,
1011
setSelectedContentId,
11-
selectedVersionId,
12-
setSelectedVersionId,
12+
setSelectedVersion,
1313
};
1414
};

xp-archive/client/versionSelector/VersionSelector.tsx

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { ChangeEvent } from 'react';
22
import { Select } from '@navikt/ds-react';
33
import { VersionReference } from 'shared/types';
44
import { pruneString } from '@common/shared/pruneString';
@@ -10,18 +10,28 @@ type Props = {
1010
};
1111

1212
export const VersionSelector = ({ versions }: Props) => {
13-
const { selectedVersionId, setSelectedVersionId } = useAppState();
13+
const { selectedVersion, setSelectedVersion } = useAppState();
1414

1515
if (versions.length === 0) {
1616
return null;
1717
}
1818

19+
const selectVersion = (e: ChangeEvent<HTMLSelectElement>) => {
20+
if (!e.target.value) {
21+
setSelectedVersion(undefined);
22+
}
23+
const versionId = e.target.value;
24+
const nodeId = versions.find((v) => v.versionId === versionId)?.nodeId;
25+
setSelectedVersion({ nodeId, versionId });
26+
};
27+
1928
return (
2029
<Select
2130
label={'Versjoner'}
22-
onChange={(e) => setSelectedVersionId(e.target.value)}
23-
value={selectedVersionId ?? ''}
31+
onChange={selectVersion}
32+
value={selectedVersion?.versionId ?? ''}
2433
>
34+
<option value={''}>Siste versjon</option>
2535
{versions.map((version) => (
2636
<option key={version.versionId} value={version.versionId}>
2737
{`${pruneString(version.displayName, 100)} - [${formatTimestamp(version.timestamp)}]`}

0 commit comments

Comments
 (0)