Skip to content

Commit

Permalink
refactor: remove hidden and hiddenExpr wheren posible
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelrss committed Dec 5, 2023
1 parent babad1b commit 8b5b090
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 118 deletions.
2 changes: 0 additions & 2 deletions src/__mocks__/getUiConfigStateMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { IUiConfig } from 'src/types';
export const getUiConfigStateMock = (customStates?: Partial<IUiConfig>): IUiConfig => ({
focus: null,
pageOrderConfig: {
hidden: [],
hiddenExpr: {},
order: ['FormLayout'],
},
hiddenFields: [],
Expand Down
1 change: 0 additions & 1 deletion src/features/form/layout/LayoutsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function processLayouts({ input, dispatch, layoutSetId }: LegacyProcessProps) {
dispatch(
FormLayoutActions.fetchFulfilled({
layouts,
hiddenLayoutsExpressions,
layoutSetId: layoutSetId || null,
}),
);
Expand Down
15 changes: 1 addition & 14 deletions src/features/form/layout/formLayoutSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export const initialState: ILayoutState = {
receiptLayoutName: undefined,
currentView: 'FormLayout',
pageOrderConfig: {
hidden: [],
hiddenExpr: {},
order: null,
},
pageTriggers: [],
Expand Down Expand Up @@ -60,10 +58,8 @@ export const formLayoutSlice = () => {
actions: {
fetchFulfilled: mkAction<LayoutTypes.IFetchLayoutFulfilled>({
reducer: (state, action) => {
const { layouts, hiddenLayoutsExpressions, layoutSetId } = action.payload;
const { layouts, layoutSetId } = action.payload;
state.layouts = layouts;
state.uiConfig.pageOrderConfig.order = Object.keys(layouts);
state.uiConfig.pageOrderConfig.hiddenExpr = hiddenLayoutsExpressions;
state.uiConfig.repeatingGroups = null;
state.layoutSetId = layoutSetId;
},
Expand All @@ -88,10 +84,6 @@ export const formLayoutSlice = () => {
state.uiConfig.receiptLayoutName = settings?.receiptLayoutName;
if (settings && settings.pages) {
updateCommonPageSettings(state, settings.pages);
const order = settings.pages.order;
if (order) {
state.uiConfig.pageOrderConfig.order = order;
}
}

state.uiConfig.pdfLayoutName = settings?.pages.pdfLayoutName;
Expand Down Expand Up @@ -159,11 +151,6 @@ export const formLayoutSlice = () => {
}
},
}),
updateHiddenLayouts: mkAction<LayoutTypes.IHiddenLayoutsUpdate>({
reducer: (state, action) => {
state.uiConfig.pageOrderConfig.hidden = action.payload.hiddenLayouts;
},
}),
initRepeatingGroups: mkAction<LayoutTypes.IInitRepeatingGroups>({
takeEvery: initRepeatingGroupsSaga,
saga: () =>
Expand Down
27 changes: 1 addition & 26 deletions src/features/form/layout/formLayoutTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IFormData } from 'src/features/formData';
import type { Triggers } from 'src/layout/common.generated';
import type { ILayouts } from 'src/layout/layout';
import type { IHiddenLayoutsExternal, ILayoutSets, ILayoutSettings, TriggersPageValidation } from 'src/types';
import type { ILayoutSets, ILayoutSettings } from 'src/types';

export interface IFormLayoutActionRejected {
error: Error | null;
Expand All @@ -10,7 +10,6 @@ export interface IFormLayoutActionRejected {

export interface IFetchLayoutFulfilled {
layouts: ILayouts;
hiddenLayoutsExpressions: IHiddenLayoutsExternal;
layoutSetId: string | null;
}

Expand All @@ -22,26 +21,6 @@ export interface IFetchLayoutSettingsFulfilled {
settings: ILayoutSettings | null;
}

export interface IUpdateCurrentView {
newView: string;
returnToView?: string;
runValidations?: TriggersPageValidation;
skipPageCaching?: boolean;
focusComponentId?: string;
keepScrollPos?: IComponentScrollPos;
allowNavigationToHidden?: boolean;
}

export interface IUpdateCurrentViewFulfilled {
newView: string;
returnToView?: string;
focusComponentId?: string;
}

export interface IUpdateCurrentViewRejected extends IFormLayoutActionRejected {
keepScrollPos?: IComponentScrollPos;
}

export interface IUpdateHiddenComponents {
componentsToHide: string[];
newlyHidden: string[];
Expand Down Expand Up @@ -72,10 +51,6 @@ export interface IComponentScrollPos {
offsetTop: number | undefined;
}

export interface IHiddenLayoutsUpdate {
hiddenLayouts: string[];
}

export interface IInitRepeatingGroups {
changedFields?: IFormData;
}
Expand Down
2 changes: 0 additions & 2 deletions src/features/form/layout/update/updateFormLayoutSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ import type { IRuntimeState } from 'src/types';
export const selectFormLayoutState = (state: IRuntimeState) => state.formLayout;
export const selectFormData = (state: IRuntimeState) => state.formData.formData;
export const selectFormLayouts = (state: IRuntimeState) => state.formLayout.layouts;
export const selectAllLayouts = (state: IRuntimeState) => state.formLayout.uiConfig.pageOrderConfig.order;
export const selectCurrentLayout = (state: IRuntimeState) => state.formLayout.uiConfig.currentView;
20 changes: 12 additions & 8 deletions src/hooks/usePdfPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useMemo } from 'react';

import { usePageNavigationContext } from 'src/features/form/layout/PageNavigationContext';
import { usePdfFormatQuery } from 'src/features/pdf/usePdfFormatQuery';
import { useAppSelector } from 'src/hooks/useAppSelector';
import { useNavigatePage } from 'src/hooks/useNavigatePage';
import { getLayoutComponentObject } from 'src/layout';
import { useExprContext } from 'src/utils/layout/ExprContext';
import { dataSourcesFromState } from 'src/utils/layout/hierarchy';
Expand All @@ -10,19 +12,20 @@ import type { IPdfFormat } from 'src/features/pdf/types';
import type { CompInstanceInformationExternal } from 'src/layout/InstanceInformation/config.generated';
import type { HierarchyDataSources, ILayout } from 'src/layout/layout';
import type { CompSummaryExternal } from 'src/layout/Summary/config.generated';
import type { IPageOrderConfig, IRepeatingGroups } from 'src/types';
import type { IRepeatingGroups } from 'src/types';
import type { LayoutPage } from 'src/utils/layout/LayoutPage';
import type { LayoutPages } from 'src/utils/layout/LayoutPages';

const PDF_LAYOUT_NAME = '__pdf__';

export const usePdfPage = (): LayoutPage | null => {
const { hidden } = usePageNavigationContext();
const { order } = useNavigatePage();

const layoutPages = useExprContext();
const dataSources = useAppSelector(dataSourcesFromState);
const pageOrderConfig = useAppSelector((state) => state.formLayout.uiConfig.pageOrderConfig);
const repeatingGroups = useAppSelector((state) => state.formLayout.uiConfig.repeatingGroups);
const pdfLayoutName = useAppSelector((state) => state.formLayout.uiConfig.pdfLayoutName);

const customPdfPage = pdfLayoutName ? layoutPages?.[pdfLayoutName] : undefined;
const method = customPdfPage ? 'custom' : 'auto';

Expand All @@ -32,10 +35,10 @@ export const usePdfPage = (): LayoutPage | null => {

const automaticPdfPage = useMemo(() => {
if (readyForPrint && method === 'auto') {
return generateAutomaticPage(pdfFormat!, pageOrderConfig!, layoutPages!, dataSources, repeatingGroups!);
return generateAutomaticPage(pdfFormat!, layoutPages!, dataSources, repeatingGroups!, order, hidden);
}
return null;
}, [readyForPrint, method, pdfFormat, pageOrderConfig, layoutPages, dataSources, repeatingGroups]);
}, [order, hidden, readyForPrint, method, pdfFormat, layoutPages, dataSources, repeatingGroups]);

if (!readyForPrint) {
return null;
Expand All @@ -50,10 +53,11 @@ export const usePdfPage = (): LayoutPage | null => {

function generateAutomaticPage(
pdfFormat: IPdfFormat,
pageOrderConfig: IPageOrderConfig,
layoutPages: LayoutPages,
dataSources: HierarchyDataSources,
repeatingGroups: IRepeatingGroups,
order: string[],
hidden: string[],
): LayoutPage {
const automaticPdfLayout: ILayout = [];

Expand All @@ -75,8 +79,8 @@ function generateAutomaticPage(

const excludedPages = new Set(pdfFormat?.excludedPages);
const excludedComponents = new Set(pdfFormat?.excludedComponents);
const hiddenPages = new Set(pageOrderConfig.hidden);
const pageOrder = pageOrderConfig.order;
const hiddenPages = new Set(hidden);
const pageOrder = order;

// Iterate over all pages, and add all components that should be included in the automatic PDF as summary components
Object.entries(layoutPages.all())
Expand Down
2 changes: 0 additions & 2 deletions src/layout/Group/RepeatingGroupsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const getLayout = (group: CompGroupRepeatingExternal, components: CompOrGroupExt
focus: undefined,
pageOrderConfig: {
order: ['FormLayout'],
hidden: [],
hiddenExpr: {},
},
excludePageFromPdf: [],
excludeComponentFromPdf: [],
Expand Down
2 changes: 0 additions & 2 deletions src/layout/Likert/RepeatingGroupsLikertContainerTestUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ const createLayout = (
focus: null,
pageOrderConfig: {
order: null,
hidden: [],
hiddenExpr: {},
},
pageTriggers: [],
excludePageFromPdf: [],
Expand Down
2 changes: 0 additions & 2 deletions src/layout/NavigationBar/NavigationBarComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const render = async () => {
uiConfig: {
pageOrderConfig: {
order: ['page1', 'page2', 'page3'],
hiddenExpr: {},
hidden: [],
},
currentView: 'page1',
focus: 'focus',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ describe('NavigationButtons', () => {
repeatingGroups: {},
pageOrderConfig: {
order: ['layout1', 'layout2'],
hidden: [],
hiddenExpr: {},
},
excludePageFromPdf: [],
excludeComponentFromPdf: [],
Expand Down
23 changes: 0 additions & 23 deletions src/selectors/getLayoutOrder.test.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/selectors/getLayoutOrder.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export interface IUiConfig {
autoSaveBehavior?: 'onChangePage' | 'onChangeFormData';
receiptLayoutName?: string;
currentView: string;
returnToView?: string;
focus: string | null | undefined;
hiddenFields: string[];
repeatingGroups: IRepeatingGroups | null;
Expand All @@ -102,21 +101,8 @@ export interface IPageOrderConfig {
* is not in this list, it should be considered hidden. It will be null until layouts have been fetched.
*
* Do NOT use this directly, as it will not respect layouts hidden using expressions!
* @see getLayoutOrderFromPageOrderConfig
* @see selectLayoutOrder
*/
order: string[] | null;

/**
* This state contains the results from calculating `hiddenExpr` (expressions to decide if a certain layout should
* be hidden or not). If a layout is in this list, is should also not be displayed.
*/
hidden: string[];

/**
* List of expressions containing logic used to show/hide certain layouts.
*/
hiddenExpr: IHiddenLayoutsExternal;
}

export enum ProcessTaskType {
Expand Down

0 comments on commit 8b5b090

Please sign in to comment.