Skip to content

Commit

Permalink
Move record page perisistence to wdk-client package (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfalke authored Nov 20, 2024
1 parent 42431de commit aff9a57
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 201 deletions.
46 changes: 43 additions & 3 deletions packages/libs/wdk-client/src/Actions/RecordActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Action =
| RecordLoadingAction
| RecordErrorAction
| SectionVisibilityAction
| UpdateTableStateAction
| SetCollapsedSectionsAction
| AllFieldVisibilityAction
| NavigationVisibilityAction
Expand All @@ -43,6 +44,7 @@ export type RecordReceivedAction = {
record: RecordInstance;
recordClass: RecordClass;
categoryTree: CategoryTreeNode;
defaultExpandedSections?: string[];
};
};

Expand Down Expand Up @@ -209,6 +211,30 @@ export function setCollapsedSections(

//==============================================================================

export const TABLE_STATE_UPDATED = 'record-view/table-state-updated';

export type UpdateTableStateAction = {
type: typeof TABLE_STATE_UPDATED;
payload: {
tableName: string;
tableState: {
searchTerm: string;
selectedRow?: number;
expandedRows: number[];
};
};
};

export const updateTableState = (
tableName: string,
tableState: UpdateTableStateAction['payload']['tableState']
): UpdateTableStateAction => ({
type: TABLE_STATE_UPDATED,
payload: { tableName, tableState },
});

//==============================================================================

export const ALL_FIELD_VISIBILITY = 'record-view/all-field-visibility-changed';

export type AllFieldVisibilityAction = {
Expand Down Expand Up @@ -325,6 +351,12 @@ interface RequestRequestOptionsGetter {
): RecordRequestOptions[];
}

interface DefaultExpandedSectionsGetter {
(recordClass: RecordClass, categoryTree: CategoryTreeNode):
| string[]
| undefined;
}

interface CategoryTreePruner {
(recordClass: RecordClass, categoryTree: CategoryTreeNode): CategoryTreeNode;
}
Expand All @@ -334,14 +366,16 @@ export function loadRecordData(
recordClass: string,
primaryKeyValues: string[],
getRecordRequestOptions: RequestRequestOptionsGetter,
pruneCategoryTree: CategoryTreePruner
pruneCategoryTree: CategoryTreePruner,
getDefaultExpandedSections: DefaultExpandedSectionsGetter
): ActionThunk<LoadRecordAction | UserAction | EmptyAction> {
return function run({ wdkService }) {
return setActiveRecord(
recordClass,
primaryKeyValues,
getRecordRequestOptions,
pruneCategoryTree
pruneCategoryTree,
getDefaultExpandedSections
);
};
}
Expand All @@ -357,7 +391,8 @@ function setActiveRecord(
recordClassUrlSegment: string,
primaryKeyValues: string[],
getRecordRequestOptions: RequestRequestOptionsGetter,
pruneCategoryTree: CategoryTreePruner
pruneCategoryTree: CategoryTreePruner,
getDefaultExpandedSections: DefaultExpandedSectionsGetter
): ActionThunk<LoadRecordAction | UserAction | EmptyAction> {
return ({ wdkService }) => {
const id = uniqueId('recordViewId');
Expand All @@ -381,13 +416,18 @@ function setActiveRecord(
{ name: '__', tree: prunedCategoryTree },
isNotInternalNode
);
const defaultExpandedSections = getDefaultExpandedSections(
recordClass,
categoryTree
);
const initialAction$ = wdkService
.getRecord(recordClass.urlSegment, primaryKey, initialOptions)
.then((record) =>
recordReceived(id, {
record,
recordClass,
categoryTree,
defaultExpandedSections,
})
);
const additionalActions = additionalOptions.map((options) =>
Expand Down
12 changes: 11 additions & 1 deletion packages/libs/wdk-client/src/Controllers/RecordController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
updateNavigationVisibility,
updateSectionVisibility,
requestPartialRecord,
updateTableState,
} from '../Actions/RecordActions';

import {
Expand All @@ -42,6 +43,7 @@ const ActionCreators = {
loadRecordData,
updateSectionVisibility,
updateNavigationQuery,
updateTableState,
updateAllFieldVisibility,
updateNavigationCategoryExpansion,
updateNavigationVisibility,
Expand Down Expand Up @@ -96,6 +98,13 @@ class RecordController extends PageController<Props> {
];
}

getDefaultExpandedSections(
recordClass: RecordClass,
categoryTree: CategoryTreeNode
): string[] | undefined {
return undefined;
}

pruneCategoryTree(
recordClass: RecordClass,
categoryTree: CategoryTreeNode
Expand Down Expand Up @@ -176,7 +185,8 @@ class RecordController extends PageController<Props> {
recordClass,
pkValues,
this.getRecordRequestOptions.bind(this),
this.pruneCategoryTree.bind(this)
this.pruneCategoryTree.bind(this),
this.getDefaultExpandedSections.bind(this)
);
}
}
Expand Down
Loading

0 comments on commit aff9a57

Please sign in to comment.