Skip to content

Commit 4aaddfc

Browse files
committed
refactored layouts url code
1 parent 9a750e7 commit 4aaddfc

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

src/features/form/layout/LayoutsContext.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useLayoutSetIdFromUrl } from 'src/features/form/layoutSets/useCurrentLa
1616
import { useInstanceDataQuery, useLaxInstanceId } from 'src/features/instance/InstanceContext';
1717
import { useProcessQuery } from 'src/features/instance/useProcessQuery';
1818
import { makeLikertChildId } from 'src/layout/Likert/Generator/makeLikertChildId';
19+
import { fetchLayoutsForInstance } from 'src/queries/queries';
1920
import type { QueryDefinition } from 'src/core/queries/usePrefetchQuery';
2021
import type { CompExternal, ILayoutCollection, ILayouts } from 'src/layout/layout';
2122
import type { IExpandedWidthLayouts, IHiddenLayoutsExternal } from 'src/types';
@@ -39,10 +40,14 @@ export function useLayoutQueryDef(
3940
return {
4041
queryKey: ['formLayouts', layoutSetId, enabled],
4142
queryFn: layoutSetId
42-
? () =>
43-
fetchLayouts(layoutSetId, features.addInstanceIdentifierToLayoutRequests ? instanceId : undefined).then(
44-
(layouts) => processLayouts(layouts, layoutSetId, defaultDataModelType),
45-
)
43+
? async () => {
44+
const shouldUseInstanceEndpoint = features.addInstanceIdentifierToLayoutRequests && instanceId;
45+
const layouts = shouldUseInstanceEndpoint
46+
? await fetchLayoutsForInstance(layoutSetId, instanceId)
47+
: await fetchLayouts(layoutSetId);
48+
49+
return processLayouts(layouts, layoutSetId, defaultDataModelType);
50+
}
4651
: skipToken,
4752
enabled: enabled && !!layoutSetId,
4853
};

src/queries/queries.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getFileUploadUrl,
2828
getFileUploadUrlOld,
2929
getFooterLayoutUrl,
30+
getInstanceLayoutsUrl,
3031
getInstantiateUrl,
3132
getJsonSchemaUrl,
3233
getLayoutSetsUrl,
@@ -290,8 +291,10 @@ export const fetchFooterLayout = (): Promise<IFooterLayout | null> => httpGet(ge
290291

291292
export const fetchLayoutSets = (): Promise<ILayoutSets> => httpGet(getLayoutSetsUrl());
292293

293-
export const fetchLayouts = (layoutSetId: string, instanceId?: string): Promise<ILayoutCollection> =>
294-
httpGet(getLayoutsUrl(layoutSetId, instanceId));
294+
export const fetchLayouts = (layoutSetId: string): Promise<ILayoutCollection> => httpGet(getLayoutsUrl(layoutSetId));
295+
296+
export const fetchLayoutsForInstance = (layoutSetId: string, instanceId: string): Promise<ILayoutCollection> =>
297+
httpGet(getInstanceLayoutsUrl(layoutSetId, instanceId));
295298

296299
export const fetchLayoutSettings = (layoutSetId: string): Promise<ILayoutSettings> =>
297300
httpGet(getLayoutSettingsUrl(layoutSetId));

src/test/renderWithProviders.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ const defaultQueryMocks: AppQueries = {
152152
fetchPostPlace: async () => ({ valid: true, result: 'OSLO' }),
153153
fetchLayoutSettings: async () => ({ pages: { order: [] } }),
154154
fetchLayouts: () => Promise.reject(new Error('fetchLayouts not mocked')),
155+
fetchLayoutsForInstance: () => Promise.reject(new Error('fetchLayoutsForInstance not mocked')),
155156
fetchBackendValidations: async () => [],
156157
fetchBackendValidationsForDataElement: async () => [],
157158
fetchPaymentInformation: async () => paymentResponsePayload,

src/utils/urls/appUrlHelper.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getEnvironmentLoginUrl,
66
getFetchFormDynamicsUrl,
77
getHostname,
8+
getInstanceLayoutsUrl,
89
getInstantiateUrl,
910
getLayoutSettingsUrl,
1011
getLayoutsUrl,
@@ -377,9 +378,11 @@ describe('Frontend urlHelper.ts', () => {
377378

378379
expect(result).toBe('https://local.altinn.cloud/ttd/test/api/layouts/custom-layout.json');
379380
});
381+
});
380382

383+
describe('getInstanceLayoutsUrl', () => {
381384
it('should include instance ID in layout URL when provided', () => {
382-
const result = getLayoutsUrl('custom-layout.json', 'instanceId-1234');
385+
const result = getInstanceLayoutsUrl('custom-layout.json', 'instanceId-1234');
383386
expect(result).toBe('https://local.altinn.cloud/ttd/test/instances/instanceId-1234/layouts/custom-layout.json');
384387
});
385388
});

src/utils/urls/appUrlHelper.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,9 @@ export const getLayoutSettingsUrl = (layoutSetId: string) => `${appPath}/api/lay
185185
export const getLayoutSetsUrl = () => `${appPath}/api/layoutsets`;
186186
export const getFooterLayoutUrl = () => `${appPath}/api/v1/footer`;
187187
export const getFetchFormDynamicsUrl = (layoutSetId: string) => `${appPath}/api/ruleconfiguration/${layoutSetId}`;
188-
export const getLayoutsUrl = (layoutSetId: string, instanceId?: string) => {
189-
if (instanceId) {
190-
return `${appPath}/instances/${instanceId}/layouts/${layoutSetId}`;
191-
}
192-
return `${appPath}/api/layouts/${layoutSetId}`;
193-
};
188+
export const getLayoutsUrl = (layoutSetId: string) => `${appPath}/api/layouts/${layoutSetId}`;
189+
export const getInstanceLayoutsUrl = (layoutSetId: string, instanceId: string) =>
190+
`${appPath}/instances/${instanceId}/layouts/${layoutSetId}`;
194191
export const getRulehandlerUrl = (layoutSet: string) => `${appPath}/api/rulehandler/${layoutSet}`;
195192
export const getActiveInstancesUrl = (partyId: number) => `${appPath}/instances/${partyId}/active`;
196193
export const getInstanceUiUrl = (instanceId: string) => `${appPath}#/instance/${instanceId}`;

0 commit comments

Comments
 (0)