Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/__mocks__/hierarchyMock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getInitialStateMock } from 'src/__mocks__/initialStateMock';
import type { IProfileState } from 'src/features/profile';
import type { HierarchyDataSources } from 'src/utils/layout/hierarchy.types';

export function getHierarchyDataSourcesMock(): HierarchyDataSources {
Expand All @@ -10,5 +11,7 @@ export function getHierarchyDataSourcesMock(): HierarchyDataSources {
authContext: null,
validations: {},
devTools: getInitialStateMock().devTools,
textResources: [],
profile: {} as IProfileState,
};
}
5 changes: 4 additions & 1 deletion src/features/expressions/ExprContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ExprRuntimeError, NodeNotFound, NodeNotFoundWithoutContext } from 'src/
import { prettyErrors, prettyErrorsToConsole } from 'src/features/expressions/prettyErrors';
import type { ExprConfig, Expression } from 'src/features/expressions/types';
import type { IFormData } from 'src/features/formData';
import type { IApplicationSettings, IAuthContext, IInstanceContext } from 'src/types/shared';
import type { IProfileState } from 'src/features/profile';
import type { IApplicationSettings, IAuthContext, IInstanceContext, ITextResource } from 'src/types/shared';
import type { LayoutNode } from 'src/utils/layout/LayoutNode';
import type { LayoutPage } from 'src/utils/layout/LayoutPage';

Expand All @@ -14,6 +15,8 @@ export interface ContextDataSources {
formData: IFormData;
authContext: Partial<IAuthContext> | null;
hiddenFields: Set<string>;
textResources: ITextResource[];
profile: IProfileState;
}

export interface PrettyErrorsOptions {
Expand Down
15 changes: 8 additions & 7 deletions src/features/expressions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export interface EvalExprInObjArgs<T> {
deleteNonExpressions?: boolean;
}

const altinnWindow = window as unknown as IAltinnWindow;

/**
* Magic key used to indicate a config value for all possible values in an object
*/
Expand Down Expand Up @@ -533,16 +531,19 @@ export const ExprFunctions = {
if (key === null) {
throw new LookupNotFound(this, `"Key" parameter cannot be null.`);
}
const state = altinnWindow.reduxStore.getState();
return getTextResourceByKey(key, state.textResources.resources);

return getTextResourceByKey(key, this.dataSources.textResources);
},
args: [ExprVal.String] as const,
returns: ExprVal.String,
}),
language: defineFunc({
impl(): string {
const state = altinnWindow.reduxStore.getState();
return state.profile.selectedAppLanguage || state.profile.profile.profileSettingPreference.language;
impl(): string | null {
const selectedLanguage =
this.dataSources.profile?.selectedAppLanguage ||
this.dataSources.profile?.profile?.profileSettingPreference?.language;

return selectedLanguage || null;
},
args: [] as const,
returns: ExprVal.String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Should return profileSettingPreference",
"expression": ["language"],
"expects": "en",
"profile": {
"profile": {
"profileSettingPreference": {
"language": "en"
}
},
"selectedAppLanguage": null
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"name": "Should return the selected language",
"name": "Should return profile selectedAppLanguage",
"expression": ["language"],
"expects": "nb",
"frontendSettings": {
"selectedLanguage": "nb"
"profile": {
"profile": {
"profileSettingPreference": {
"language": "en"
}
},
"selectedAppLanguage": "nb"
}
}
5 changes: 5 additions & 0 deletions src/features/expressions/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { _private } from 'src/utils/layout/hierarchy';
import { generateEntireHierarchy, generateHierarchy } from 'src/utils/layout/HierarchyGenerator';
import type { FunctionTest, SharedTestContext, SharedTestContextList } from 'src/features/expressions/shared';
import type { Expression } from 'src/features/expressions/types';
import type { IProfileState } from 'src/features/profile';
import type { IRepeatingGroups } from 'src/types';
import type { IApplicationSettings } from 'src/types/shared';
import type { HierarchyDataSources } from 'src/utils/layout/hierarchy.types';
Expand Down Expand Up @@ -61,13 +62,17 @@ describe('Expressions shared function tests', () => {
instance,
permissions,
frontendSettings,
textResources,
profile,
}) => {
const dataSources: HierarchyDataSources = {
...getHierarchyDataSourcesMock(),
formData: dataModel ? dot.dot(dataModel) : {},
instanceContext: buildInstanceContext(instance),
applicationSettings: frontendSettings || ({} as IApplicationSettings),
authContext: buildAuthContext(permissions),
textResources: textResources || [],
profile: profile || ({} as IProfileState),
};

const _layouts = convertLayouts(layouts);
Expand Down
4 changes: 4 additions & 0 deletions src/features/expressions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import fs from 'node:fs';

import type { Expression } from 'src/features/expressions/types';
import type { IProcessPermissions } from 'src/features/process';
import type { IProfileState } from 'src/features/profile';
import type { ILayout, ILayouts } from 'src/layout/layout';
import type { ITextResource } from 'src/types';
import type { IApplicationSettings, IInstance } from 'src/types/shared';

export interface Layouts {
Expand All @@ -22,6 +24,8 @@ export interface SharedTest {
instance?: IInstance;
permissions?: IProcessPermissions;
frontendSettings?: IApplicationSettings;
textResources?: ITextResource[];
profile?: IProfileState;
}

export interface SharedTestContext {
Expand Down
2 changes: 1 addition & 1 deletion src/layout/Group/DisplayGroupContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import classes from 'src/layout/Group/DisplayGroupContainer.module.css';
import { pageBreakStyles, selectComponentTexts } from 'src/utils/formComponentUtils';
import { LayoutNode } from 'src/utils/layout/LayoutNode';
import { getTextFromAppOrDefault } from 'src/utils/textResource';
import type { HGroups } from 'src/utils/layout/hierarchy.types';
import type { HGroups } from 'src/layout/Group/types';

const H = ({ level, children, ...props }) => {
switch (level) {
Expand Down
16 changes: 15 additions & 1 deletion src/utils/layout/hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export function dataSourcesFromState(state: IRuntimeState): HierarchyDataSources
authContext: buildAuthContext(state.process),
validations: state.formValidations.validations,
devTools: state.devTools,
textResources: state.textResources.resources,
profile: state.profile,
};
}

Expand Down Expand Up @@ -170,8 +172,20 @@ function useResolvedExpressions() {
hiddenFields: new Set(hiddenFields),
validations,
devTools,
textResources,
profile: state.profile,
}),
[formData, applicationSettings, instance, process, hiddenFields, validations, devTools],
[
formData,
applicationSettings,
instance,
process,
hiddenFields,
validations,
devTools,
textResources,
state.profile,
],
);

return useMemo(
Expand Down