mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
refactor formatContentTypeData + add story
This commit is contained in:
parent
2a381562bb
commit
14b30da820
@ -0,0 +1,32 @@
|
||||
<!--- formatContentTypeData.stories.mdx --->
|
||||
|
||||
import { Meta } from '@storybook/addon-docs';
|
||||
|
||||
<Meta title="utils/formatContentTypeData" />
|
||||
|
||||
# formatContentTypeData
|
||||
|
||||
This util is used to format the data received by the backend so it can be used by the admin.
|
||||
It:
|
||||
- hads the key `__temp_key__` to each component (easier for reordering repeatable components)
|
||||
- stringifies JSON fields (easier to harmonize the data format for the json editor from user input or backend data)
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { formatContentTypeData } from '@strapi/helper-plugin';
|
||||
|
||||
const Compo = ({ allLayoutData }) => {
|
||||
const allLayoutDataRef = useRef(allLayoutData);
|
||||
|
||||
const cleanReceivedData = useCallback(data => {
|
||||
return formatContentTypeData(
|
||||
data,
|
||||
allLayoutDataRef.current.contentType,
|
||||
allLayoutDataRef.current.components
|
||||
);
|
||||
}, []);
|
||||
|
||||
// ...
|
||||
};
|
||||
```
|
||||
@ -4,7 +4,7 @@ import testData from './testData';
|
||||
const { contentType, components, modifiedData } = testData;
|
||||
|
||||
describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => {
|
||||
it('should add the __temp_key__ property to each repeatable component object && stringify json fields', () => {
|
||||
it('should add the __temp_key__ property to each repeatable component object', () => {
|
||||
const expected = {
|
||||
createdAt: '2020-04-28T13:22:13.033Z',
|
||||
dz: [
|
||||
@ -13,8 +13,6 @@ describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => {
|
||||
id: 7,
|
||||
name: 'name',
|
||||
password: 'password',
|
||||
jsonString: '"hello"',
|
||||
jsonObject: '{\n "hello": true\n}',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
@ -55,8 +53,6 @@ describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => {
|
||||
],
|
||||
},
|
||||
password: 'password',
|
||||
jsonString: '"hello"',
|
||||
jsonObject: '{\n "hello": true\n}',
|
||||
repeatable: [
|
||||
{
|
||||
id: 2,
|
||||
@ -80,4 +76,68 @@ describe('STRAPI_HELPER_PLUGIN | utils | formatContentTypeData', () => {
|
||||
|
||||
expect(formatContentTypeData(modifiedData, contentType, components)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should stringify json fields', () => {
|
||||
const contentType = {
|
||||
uid: 'api::test.test',
|
||||
apiID: 'test',
|
||||
attributes: {
|
||||
id: { type: 'integer' },
|
||||
name: { type: 'string' },
|
||||
dz: { type: 'dynamiczone', components: ['compos.sub-compo'] },
|
||||
jsonString: { type: 'json' },
|
||||
jsonObject: { type: 'json' },
|
||||
},
|
||||
};
|
||||
|
||||
const components = {
|
||||
'compos.sub-compo': {
|
||||
uid: 'compos.sub-compo',
|
||||
category: 'compos',
|
||||
attributes: {
|
||||
id: { type: 'integer' },
|
||||
name: { type: 'string' },
|
||||
password: { type: 'password' },
|
||||
jsonString: { type: 'json' },
|
||||
jsonObject: { type: 'json' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const data = {
|
||||
id: 1,
|
||||
name: 'name',
|
||||
dz: [
|
||||
{
|
||||
__component: 'compos.sub-compo',
|
||||
id: 7,
|
||||
name: 'name',
|
||||
password: 'password',
|
||||
jsonString: 'hello',
|
||||
jsonObject: { hello: true },
|
||||
},
|
||||
],
|
||||
jsonString: 'hello',
|
||||
jsonObject: { hello: true },
|
||||
};
|
||||
|
||||
const expected = {
|
||||
id: 1,
|
||||
name: 'name',
|
||||
dz: [
|
||||
{
|
||||
__component: 'compos.sub-compo',
|
||||
id: 7,
|
||||
name: 'name',
|
||||
password: 'password',
|
||||
jsonString: '"hello"',
|
||||
jsonObject: '{\n "hello": true\n}',
|
||||
},
|
||||
],
|
||||
jsonString: '"hello"',
|
||||
jsonObject: '{\n "hello": true\n}',
|
||||
};
|
||||
|
||||
expect(formatContentTypeData(data, contentType, components)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@ -7,8 +7,6 @@ const testData = {
|
||||
dz: { type: 'dynamiczone', components: ['compos.test-compo', 'compos.sub-compo'] },
|
||||
id: { type: 'integer' },
|
||||
name: { type: 'string' },
|
||||
jsonString: { type: 'json' },
|
||||
jsonObject: { type: 'json' },
|
||||
notrepeatable: {
|
||||
type: 'component',
|
||||
repeatable: false,
|
||||
@ -27,8 +25,6 @@ const testData = {
|
||||
id: { type: 'integer' },
|
||||
name: { type: 'string' },
|
||||
password: { type: 'password' },
|
||||
jsonString: { type: 'json' },
|
||||
jsonObject: { type: 'json' },
|
||||
},
|
||||
},
|
||||
'compos.test-compo': {
|
||||
@ -59,8 +55,6 @@ const testData = {
|
||||
id: 7,
|
||||
name: 'name',
|
||||
password: 'password',
|
||||
jsonString: 'hello',
|
||||
jsonObject: { hello: true },
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
@ -101,8 +95,6 @@ const testData = {
|
||||
],
|
||||
},
|
||||
password: 'password',
|
||||
jsonString: 'hello',
|
||||
jsonObject: { hello: true },
|
||||
repeatable: [
|
||||
{
|
||||
id: 2,
|
||||
@ -181,8 +173,6 @@ const testData = {
|
||||
__component: 'compos.sub-compo',
|
||||
name: 'name',
|
||||
password: 'password',
|
||||
jsonString: 'hello',
|
||||
jsonObject: { hello: true },
|
||||
},
|
||||
{
|
||||
name: 'name',
|
||||
@ -206,8 +196,6 @@ const testData = {
|
||||
__component: 'compos.test-compo',
|
||||
},
|
||||
],
|
||||
jsonString: 'hello',
|
||||
jsonObject: { hello: true },
|
||||
name: 'name',
|
||||
notrepeatable: {
|
||||
name: 'name',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user