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

[Editing Integration] Add editMode to /editing/config endpoint response with configurable integration option #1803

Merged
merged 8 commits into from
May 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ Our versioning strategy is as follows:

### 🛠 Breaking Change

* Editing Integration Support: ([#1776](https://github.com/Sitecore/jss/pull/1776))([#1792](https://github.com/Sitecore/jss/pull/1792))([#1773](https://github.com/Sitecore/jss/pull/1773))([#1797](https://github.com/Sitecore/jss/pull/1797))([#1800](https://github.com/Sitecore/jss/pull/1800))
* Editing Integration Support: ([#1776](https://github.com/Sitecore/jss/pull/1776))([#1792](https://github.com/Sitecore/jss/pull/1792))([#1773](https://github.com/Sitecore/jss/pull/1773))([#1797](https://github.com/Sitecore/jss/pull/1797))([#1800](https://github.com/Sitecore/jss/pull/1800)) ([#1803](https://github.com/Sitecore/jss/pull/1803))
* `[sitecore-jss-react]` Introduces `PlaceholderMetadata` component which supports the hydration of chromes on Pages by rendering the components and placeholders with required metadata.
* `[sitecore-jss]` Chromes are hydrated based on the basis of new `editMode` property derived from LayoutData, which is defined as an enum consisting of `metadata` and `chromes`.
* `ComponentConsumerProps` is removed. You might need to reuse _WithSitecoreContextProps_ type.
* `[sitecore-jss-react]` `[sitecore-jss-nextjs]` Introduce FieldMetadata component and functionality to render it when metadata field property is provided in the field's layout data. In such case the field component is wrapped with metadata markup to enable chromes hydration when editing in pages. Ability to render metadata has been added to the field rendering components for react and nextjs.
* `[sitecore-jss-react]` Introduced `EditingScripts` component to render clientScripts / clientData in editing.
* `[sitecore-jss-nextjs]` `[template/nextjs-xmlcoud]` Add editMode to /editing/config endpoint response with configurable integration option.

## 22.0.0

Expand Down
13 changes: 12 additions & 1 deletion docs/upgrades/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,21 @@
```
import { EditingScripts } from '@sitecore-jss/sitecore-jss-nextjs';
...
const Scripts = (): JSX.Element | null => {
const Scripts = (): JSX.Element | null => (
<>
<EditingScripts />
...
</>
);
```

* We have introduced a new configuration option, `pagesEditMode`, in the `\src\pages\api\editing\config.ts` file to support the new editing metadata architecture for Pages (XMCloud). This option allows you to specify the editing mode used by Pages. It is set to `metadata` by default. However, if you are not ready to use a new integration and continue using the existing architecture, you can explicitly set the `pagesEditMode` to `chromes`.

```
import { EditMode } from '@sitecore-jss/sitecore-jss/layout';
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved

const handler = new EditingConfigMiddleware({
...
pagesEditMode: EditMode.Chromes,
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved
}).getHandler();
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { spy } from 'sinon';
import { expect } from 'chai';
import { EditingConfigMiddleware } from './editing-config-middleware';
import { QUERY_PARAM_EDITING_SECRET } from './constants';
import { EditMode } from '@sitecore-jss/sitecore-jss/layout';

type Query = {
[key: string]: string;
Expand Down Expand Up @@ -45,10 +46,18 @@ componentsMap.set('TestComponentOne', {});
componentsMap.set('TestComponentTwo', {});
const metadata = { packages: { testPackageOne: '0.1.1' } };

const expectedResult = {
const expectedResultWithChromes = {
components: ['TestComponentOne', 'TestComponentTwo'],
packages: { testPackageOne: '0.1.1' },
editMode: 'chromes',
};

const expectedResultWithMetadata = {
components: ['TestComponentOne', 'TestComponentTwo'],
packages: { testPackageOne: '0.1.1' },
editMode: 'metadata',
};

const expectedResultForbidden = { message: 'Missing or invalid editing secret' };

describe('EditingConfigMiddleware', () => {
Expand Down Expand Up @@ -111,14 +120,17 @@ describe('EditingConfigMiddleware', () => {
expect(res.json).to.have.been.calledWith(expectedResultForbidden);
});

it('should respond with 200 and return config data with components array as argument', async () => {
const testEditingConfig = async (
components: string[] | Map<string, unknown>,
expectedResult,
pagesEditMode?: EditMode
) => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = secret;
const req = mockRequest('GET', query);
const res = mockResponse();

const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const middleware = new EditingConfigMiddleware({ components, metadata, pagesEditMode });
const handler = middleware.getHandler();

await handler(req, res);
Expand All @@ -127,23 +139,21 @@ describe('EditingConfigMiddleware', () => {
expect(res.status).to.have.been.calledWith(200);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResult);
});
};

it('should respond with 200 and return config data with components map as argument', async () => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = secret;
const req = mockRequest('GET', query);
const res = mockResponse();
it('should respond with 200 and return config data with components array as argument and editMode as chromes', async () => {
await testEditingConfig(componentsArray, expectedResultWithChromes, EditMode.Chromes);
});

const middleware = new EditingConfigMiddleware({ components: componentsMap, metadata });
const handler = middleware.getHandler();
it('should respond with 200 and return config data with components map as argument and editMode as chromes', async () => {
await testEditingConfig(componentsMap, expectedResultWithChromes, EditMode.Chromes);
});

await handler(req, res);
it('should respond with 200 and return config data with components array as argument and editMode as metadata', async () => {
await testEditingConfig(componentsArray, expectedResultWithMetadata);
});

expect(res.status).to.have.been.calledOnce;
expect(res.status).to.have.been.calledWith(200);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResult);
it('should respond with 200 and return config data with components map as argument and editMode as metadata', async () => {
await testEditingConfig(componentsMap, expectedResultWithMetadata);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { EDITING_ALLOWED_ORIGINS, QUERY_PARAM_EDITING_SECRET } from './constants';
import { getJssEditingSecret } from '../utils/utils';
import { debug } from '@sitecore-jss/sitecore-jss';
import { EditMode } from '@sitecore-jss/sitecore-jss/layout';
import { Metadata } from '@sitecore-jss/sitecore-jss-dev-tools';
import { enforceCors } from '@sitecore-jss/sitecore-jss/utils';

Expand All @@ -14,6 +15,12 @@ export type EditingConfigMiddlewareConfig = {
* Application metadata
*/
metadata: Metadata;
/**
* Determines which editing mode should be used by Pages.
* Can be either 'chromes' or 'metadata'.
* By default its 'metadata'
*/
pagesEditMode?: EditMode;
};

/**
Expand Down Expand Up @@ -56,9 +63,12 @@ export class EditingConfigMiddleware {
? this.config.components
: Array.from(this.config.components.keys());

const editMode = this.config.pagesEditMode || EditMode.Metadata;

return res.status(200).json({
components,
packages: this.config.metadata.packages,
editMode,
});
};
}