Skip to content

Commit

Permalink
Extract the pattern from recursive function
Browse files Browse the repository at this point in the history
  • Loading branch information
p3rcypj committed Jun 3, 2024
1 parent 11d9b01 commit addc479
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
39 changes: 17 additions & 22 deletions src/domain/entities/LandingNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ export const buildOrderedLandingNodes = (nodes: LandingNode[]): OrderedLandingNo
}));
};

export const updateLandingNodes = (
nodes: LandingNode[],
permissions: LandingPagePermission[],
user: User
): LandingNode[] => {
function updateLandingNodes(nodes: LandingNode[], permissions: LandingPagePermission[], user: User): LandingNode[] {
const updatedNodes = _(nodes)
.map(node => {
const pagePermission = permissions?.find(permission => permission.id === node.id);
Expand All @@ -98,25 +94,24 @@ export const updateLandingNodes = (
.compact()
.value();

return updatedNodes.map(node => applyFavicon(node));
};
return updatedNodes;
}

const applyFavicon = (parent: LandingNode): LandingNode => {
const spreadFaviconToChildren = (children: LandingNode[], favicon: string): LandingNode[] => {
return _.map(children, child => {
return {
...child,
favicon: favicon,
children: spreadFaviconToChildren(child.children, favicon),
};
});
};
function applyFavicon(children: LandingNode[], favicon: string): LandingNode[] {
return _.map(children, child => {
return {
...child,
favicon: favicon,
children: applyFavicon(child.children, favicon),
};
});
}

return {
...parent,
children: spreadFaviconToChildren(parent.children, parent.favicon),
};
};
export function updateLandings(nodes: LandingNode[], permissions: LandingPagePermission[], user: User): LandingNode[] {
const landings = updateLandingNodes(nodes, permissions, user);

return landings.map(landing => ({ ...landing, children: applyFavicon(landing.children, landing.favicon) }));
}

// Return
// a redirect URL if there is only one visible action on primary nodes
Expand Down
4 changes: 2 additions & 2 deletions src/webapp/pages/settings/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SharedUpdate } from "../../components/permissions-dialog/PermissionsDia
import { useAppContext } from "../../contexts/app-context";
import { User } from "../../../domain/entities/User";
import { Maybe } from "../../../types/utils";
import { LandingNode, updateLandingNodes } from "../../../domain/entities/LandingNode";
import { LandingNode, updateLandings } from "../../../domain/entities/LandingNode";

export function useConfig(): useConfigPloc {
const { compositionRoot, landings } = useAppContext();
Expand All @@ -17,7 +17,7 @@ export function useConfig(): useConfigPloc {

const userLandings = useMemo<LandingNode[] | undefined>(() => {
if (!(landings && landingPagePermissions && user)) return undefined;
return updateLandingNodes(landings, landingPagePermissions, user);
return updateLandings(landings, landingPagePermissions, user);
}, [landingPagePermissions, landings, user]);

useEffect(() => {
Expand Down

0 comments on commit addc479

Please sign in to comment.