Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not cast menu items ids to number #858

Merged
merged 5 commits into from
Aug 28, 2024
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
6 changes: 6 additions & 0 deletions .changeset/late-rings-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@headstartwp/core": patch
"@headstartwp/headstartwp": patch
---

Fix: do not cast menu item ids to number as they are strings
185 changes: 183 additions & 2 deletions packages/core/src/data/fetchFn/__tests__/fetchAppSettings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectTypeOf } from 'expect-type';
import { AppEntity, EndpointParams } from '../..';
import { fetchAppSettings } from '../fetchAppSettings';
import { AppEntity, EndpointParams, MenuItemEntity } from '../..';
import { fetchAppSettings, flatToHierarchical } from '../fetchAppSettings';
import { setHeadstartWPConfig } from '../../../utils';

describe('fetchAppSettings', () => {
Expand Down Expand Up @@ -34,3 +34,184 @@ describe('fetchAppSettings', () => {
>();
});
});

describe('flatToHierarchical', () => {
const menuData: Array<MenuItemEntity> = [
{
// @ts-expect-error to be fixed in next major release
ID: '89-en',
title: 'Privacy Policy',
slug: 'privacy-policy',
post_parent: '0',
guid: 'http://localhost:8888/?p=89',
menu_item_parent: '0',
object_id: 'page',
url: 'http://localhost:8888/privacy-policy/',
target: '',
attr_title: '',
description: '',
classes: [''],
menu_order: 1,
order: 1,
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'page',
type: 'post_type',
type_label: 'Page',
children: [],
},
{
ID: 90,
title: 'Sample Page 9',
slug: '',
post_parent: '89-en',
guid: 'http://localhost:8888/?p=90',
menu_item_parent: '89-en',
object_id: 'page',
url: 'http://localhost:8888/',
target: '',
attr_title: '',
description: '',
classes: [''],
menu_order: 2,
order: 2,
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'page',
type: 'post_type',
type_label: 'Page',
children: [],
},
{
ID: 91,
title: 'Ut quae sed exercitationem 8',
slug: 'ut-quae-sed-exercitationem',
post_parent: '90',
guid: 'http://localhost:8888/?p=91',
menu_item_parent: '90',
object_id: 'post',
url: 'http://localhost:8888/ut-quae-sed-exercitationem/',
target: '',
attr_title: '',
description: '',
classes: [''],
menu_order: 3,
order: 3,
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'post',
type: 'post_type',
type_label: 'Post',
children: [],
},
{
ID: 92,
title: 'Dolor praesentium nihil quis 2',
slug: 'dolor-praesentium-nihil-quis',
post_parent: '0',
guid: 'http://localhost:8888/?p=92',
menu_item_parent: '0',
object_id: 'post',
url: 'http://localhost:8888/dolor-praesentium-nihil-quis/',
target: '',
attr_title: '',
description: '',
classes: [''],
menu_order: 4,
order: 4,
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'post',
type: 'post_type',
type_label: 'Post',
children: [],
},
];

it('converts flat menu data to hierarchical', () => {
expect(flatToHierarchical(menuData)).toMatchObject([
{
ID: '89-en',
title: 'Privacy Policy',
slug: 'privacy-policy',
post_parent: '0',
guid: 'http://localhost:8888/?p=89',
menu_item_parent: '0',
object_id: 'page',
url: 'http://localhost:8888/privacy-policy/',
target: '',
attr_title: '',
description: '',
classes: [''],
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'page',
type: 'post_type',
type_label: 'Page',
children: [
{
ID: 90,
title: 'Sample Page 9',
slug: '',
post_parent: '89-en',
guid: 'http://localhost:8888/?p=90',
menu_item_parent: '89-en',
object_id: 'page',
url: 'http://localhost:8888/',
target: '',
attr_title: '',
description: '',
classes: [''],
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'page',
type: 'post_type',
type_label: 'Page',
children: [
{
ID: 91,
title: 'Ut quae sed exercitationem 8',
slug: 'ut-quae-sed-exercitationem',
post_parent: '90',
guid: 'http://localhost:8888/?p=91',
menu_item_parent: '90',
object_id: 'post',
url: 'http://localhost:8888/ut-quae-sed-exercitationem/',
target: '',
attr_title: '',
description: '',
classes: [''],
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'post',
type: 'post_type',
type_label: 'Post',
children: [],
},
],
},
],
},
{
ID: 92,
title: 'Dolor praesentium nihil quis 2',
slug: 'dolor-praesentium-nihil-quis',
post_parent: '0',
guid: 'http://localhost:8888/?p=92',
menu_item_parent: '0',
object_id: 'post',
url: 'http://localhost:8888/dolor-praesentium-nihil-quis/',
target: '',
attr_title: '',
description: '',
classes: [''],
post_type: 'nav_menu_item',
post_mime_type: '',
object: 'post',
type: 'post_type',
type_label: 'Post',
children: [],
},
]);
});
});
10 changes: 5 additions & 5 deletions packages/core/src/data/fetchFn/fetchAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ export type AppQueryResult<T> = {
export function flatToHierarchical(flat: MenuItemEntity[]): MenuItemEntity[] {
const roots: MenuItemEntity[] = [];

const all: Record<number, MenuItemEntity> = {};
const all: Record<string, MenuItemEntity> = {};
flat.forEach((item, index) => {
all[item.ID] = { ...item, children: [], order: index };
});

Object.keys(all).forEach((key) => {
const id = Number(key);
const id = key;
const item = all[id];
const parentId = Number(item.menu_item_parent);
const parentId = item.menu_item_parent;

if (parentId === 0) {
if (parentId === '0') {
roots.push(item);
} else if (item.menu_item_parent in all) {
const p = all[item.menu_item_parent];
if (!('children' in p)) {
if (!p.children) {
p.children = [];
}
p.children.push(item);
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/data/types/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,18 +705,22 @@ export type Redirect = {
};

export interface MenuItemEntity {
// TODO: this should be a string but changing this to a string will require a new major
// @see https://github.com/10up/headstartwp/issues/772
ID: number;
menu_order: number;
post_type: string;
post_mime_type: string;
menu_item_parent: string;
post_parent: string;
object_id: string;
object: string;
type: string;
type_label: string;
url: string;
title: string;
target: '_blank' | '_self' | '_parent' | '_top';
guid: string;
target: '_blank' | '_self' | '_parent' | '_top' | '';
attr_title: string;
description: string;
classes: string[];
Expand Down
6 changes: 6 additions & 0 deletions wp/headless-wp/includes/classes/API/AppEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ public function prepare_menus( $menu ) {
$filtered_menu_item->attr_title = $menu_item->attr_title;
$filtered_menu_item->description = $menu_item->description;
$filtered_menu_item->classes = $menu_item->classes;
$filtered_menu_item->menu_order = $menu_item->menu_order;
$filtered_menu_item->post_type = $menu_item->post_type;
$filtered_menu_item->post_mime_type = $menu_item->post_mime_type;
$filtered_menu_item->object = $menu_item->object;
$filtered_menu_item->type = $menu_item->type;
$filtered_menu_item->type_label = $menu_item->type_label;

$filtered_menu[] = $filtered_menu_item;
}
Expand Down
Loading