Skip to content

Commit

Permalink
feat(feo): add service tiles interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Dec 16, 2024
1 parent c228b19 commit cd2c515
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
6 changes: 6 additions & 0 deletions navnotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ Required to match nav item in bundle to current app
## frontendRef

search entries need a `frontendRef` attribute. Without the attribute, we can modify/add frontend entries, but we can't remove them

# Service tiles interceptor

## frontendRef

Service tile entries need a `frontendRef` attribute. Without the attribute, we can modify/add frontend entries, but we can't remove them
17 changes: 17 additions & 0 deletions packages/config-utils/src/feo/feo-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ export type BundleSegment = {
navItems: DirectNavItem[];
};

export type ServiceTile = {
section: string;
group: string;
id: string;
frontendRef: string;
};

export type ServiceGroup = {
id: string;
tiles: ServiceTile[];
};

export type ServiceCategory = {
id: string;
groups: ServiceGroup[];
};
export type CRDObject = {
metadata: {
name: string;
Expand All @@ -106,6 +122,7 @@ export type CRDObject = {
navigationSegments?: DirectNavItem[];
module: ChromeModule;
searchEntries?: ChromeStaticSearchEntry[];
serviceTiles?: ServiceTile[];
};
};

Expand Down
127 changes: 127 additions & 0 deletions packages/config-utils/src/feo/service-tiles-interceptor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { FrontendCRD, ServiceCategory } from './feo-types';
import serviceTilesInterceptor from './service-tiles-interceptor';

describe('Service tiles interceptor', () => {
it('should replace service tiles with the ones from the frontendCRD', () => {
const frontendName = 'frontendName';
const frontendCrd: FrontendCRD = {
objects: [
{
metadata: {
name: frontendName,
},
spec: {
module: {
manifestLocation: 'location',
},
serviceTiles: [
{
section: 'section-1',
group: 'group-1',
id: 'id-1',
frontendRef: frontendName,
},
{
section: 'section-1',
group: 'group-1',
id: 'id-2',
frontendRef: frontendName,
},
{
section: 'section-2',
group: 'group-1',
id: 'id-3',
frontendRef: frontendName,
},
],
},
},
],
};
const remoteServiceTiles: ServiceCategory[] = [
{
id: 'section-1',
groups: [
{
id: 'group-1',
tiles: [
{
section: 'section-1',
group: 'group-1',
id: 'otherFrontend',
frontendRef: 'otherFrontend',
},
{
section: 'section-1',
group: 'group-1',
id: 'id-2',
frontendRef: frontendName,
},
],
},
],
},
{
id: 'section-2',
groups: [
{
id: 'group-1',
tiles: [
{
section: 'section-2',
group: 'group-1',
id: 'otherFrontend',
frontendRef: 'otherFrontend',
},
],
},
],
},
];
const expectedServiceTiles: ServiceCategory[] = [
{
id: 'section-1',
groups: [
{
id: 'group-1',
tiles: [
remoteServiceTiles[0].groups[0].tiles[0],
{
section: 'section-1',
group: 'group-1',
id: 'id-1',
frontendRef: frontendName,
},
{
section: 'section-1',
group: 'group-1',
id: 'id-2',
frontendRef: frontendName,
},
],
},
],
},
{
id: 'section-2',
groups: [
{
id: 'group-1',
tiles: [
remoteServiceTiles[1].groups[0].tiles[0],
{
section: 'section-2',
group: 'group-1',
id: 'id-3',
frontendRef: frontendName,
},
],
},
],
},
];

const result = serviceTilesInterceptor(remoteServiceTiles, frontendCrd);
expect(result).toEqual(expectedServiceTiles);
});
});
42 changes: 42 additions & 0 deletions packages/config-utils/src/feo/service-tiles-interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FrontendCRD, ServiceCategory, ServiceTile } from './feo-types';

function serviceTilesInterceptor(serviceCategories: ServiceCategory[], frontendCrd: FrontendCRD): ServiceCategory[] {
const frontendRef = frontendCrd.objects[0].metadata.name;
let result = [...serviceCategories];

const frontendCategories =
frontendCrd.objects[0].spec.serviceTiles?.reduce<{
[section: string]: { [group: string]: ServiceTile[] };
}>((acc, tile) => {
const section = tile.section;
const group = tile.group;
if (!acc[section]) {
acc[section] = {};
}

if (!acc[section][group]) {
acc[section][group] = [];
}

acc[section][group].push({ ...tile });
return acc;
}, {}) ?? {};

result = result.map((category) => {
const newGroups = category.groups.map((group) => {
const newTiles = group.tiles.filter((tile) => tile.frontendRef !== frontendRef);
return {
...group,
tiles: [...newTiles, ...(frontendCategories[category.id]?.[group.id] ?? [])],
};
});
return {
...category,
groups: newGroups,
};
});

return result;
}

export default serviceTilesInterceptor;

0 comments on commit cd2c515

Please sign in to comment.