Skip to content

Commit

Permalink
release@main
Browse files Browse the repository at this point in the history
  • Loading branch information
primeinteger authored Dec 17, 2024
2 parents 089960c + 6b0b02a commit ba835c3
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 39 deletions.
6 changes: 3 additions & 3 deletions examples/next-app-router/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"npmClient": "npm",
"version": "1.26.0",
"version": "1.27.0-beta.0",
"command": {
"version": {
"allowBranch": ["main", "next", "development"],
Expand Down
48 changes: 35 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentful/experiences-components-react",
"version": "1.26.0",
"version": "1.27.0-beta.0",
"description": "A basic set of components to use with Studio Experiences",
"homepage": "https://github.com/contentful/experience-builder/tree/next/packages/components#readme",
"repository": {
Expand Down Expand Up @@ -79,7 +79,7 @@
},
"dependencies": {
"@contentful/experiences-core": "file:../core",
"@contentful/rich-text-react-renderer": "^15.17.2",
"@contentful/rich-text-react-renderer": "^16.0.1",
"postcss-import": "^16.0.1",
"style-inject": "^0.3.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentful/experiences-core",
"version": "1.26.0",
"version": "1.27.0-beta.0",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/entity/EntityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EntityStoreBase } from './EntityStoreBase';
import { get } from '@/utils/get';
import { transformAssetFileToUrl } from './value-transformers';
import { isLink } from '@/utils/isLink';
import { gatherUsedComponentsWithDeepRefernces } from '@/fetchers/gatherUsedComponentsWithDeepReferences';
type EntityStoreArgs = {
experienceEntry: ExperienceEntry | Entry;
entities: Array<Entry | Asset>;
Expand All @@ -14,6 +15,7 @@ type EntityStoreArgs = {
export class EntityStore extends EntityStoreBase {
private _experienceEntry: ExperienceFields | undefined;
private _unboundValues: ExperienceUnboundValues | undefined;
private _usedComponentsWithDeepReferences: ExperienceEntry[];

constructor(json: string);
constructor({ experienceEntry, entities, locale }: EntityStoreArgs);
Expand All @@ -31,13 +33,19 @@ export class EntityStore extends EntityStoreBase {
});
this._experienceEntry = _experienceEntry;
this._unboundValues = _unboundValues;
this._usedComponentsWithDeepReferences = gatherUsedComponentsWithDeepRefernces(
this._experienceEntry,
);
} else {
const { experienceEntry, entities, locale } = options;
super({ entities, locale });

if (isExperienceEntry(experienceEntry)) {
this._experienceEntry = (experienceEntry as ExperienceEntry).fields;
this._unboundValues = (experienceEntry as ExperienceEntry).fields.unboundValues;
this._usedComponentsWithDeepReferences = gatherUsedComponentsWithDeepRefernces(
this._experienceEntry,
);
} else {
throw new Error('Provided entry is not experience entry');
}
Expand Down Expand Up @@ -69,7 +77,7 @@ export class EntityStore extends EntityStoreBase {
}

public get usedComponents() {
return this._experienceEntry?.usedComponents ?? [];
return this._usedComponentsWithDeepReferences ?? [];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/fetchers/fetchExperienceEntry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('fetchExperienceEntry', () => {
expect(mockClient.getEntries).toHaveBeenCalledWith({
content_type: 'books',
locale: 'en-US',
include: 3,
'fields.slug': 'slug',
});

Expand All @@ -93,6 +94,7 @@ describe('fetchExperienceEntry', () => {
expect(mockClient.getEntries).toHaveBeenCalledWith({
content_type: 'books',
locale: 'en-US',
include: 3,
'sys.id': 'entry-id',
});

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/fetchers/fetchExperienceEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const fetchExperienceEntry = async ({
const entries = await client.getEntries({
content_type: experienceTypeId,
locale,
include: 3, // fetching max 3 level deep references due to nested patterns
...filter,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { experienceEntryFieldsWithFilledUsedComponents } from '../test/__fixtures__/experience';
import { gatherUsedComponentsWithDeepRefernces } from './gatherUsedComponentsWithDeepReferences';

describe('fetchReferencedEntities gatherUsedComponentsWithDeepRefernces', () => {
it('should return an empty array if no used components are found', async () => {
const usedComponents = gatherUsedComponentsWithDeepRefernces(undefined);

expect(usedComponents).toEqual([]);
});

it('should return an array of used components with deep references', async () => {
const usedComponents = gatherUsedComponentsWithDeepRefernces(
experienceEntryFieldsWithFilledUsedComponents,
);

expect(usedComponents[0].sys.id).toEqual('root-pattern-1');
expect(usedComponents[1].sys.id).toEqual('nested-pattern-1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ExperienceEntry, ExperienceFields } from '@/types';
export const gatherUsedComponentsWithDeepRefernces = (
experienceEntryFields?: ExperienceFields,
): ExperienceEntry[] => {
const usedComponentDeepReferences: ExperienceEntry[] = [];
const usedComponents = experienceEntryFields?.usedComponents as ExperienceEntry[];
if (!usedComponents || usedComponents.length === 0) {
return [];
}
for (const component of usedComponents) {
if ('fields' in component) {
usedComponentDeepReferences.push(component);
usedComponentDeepReferences.push(...gatherUsedComponentsWithDeepRefernces(component.fields));
}
}
return usedComponentDeepReferences;
};
Loading

0 comments on commit ba835c3

Please sign in to comment.