Skip to content

Commit

Permalink
Merge branch 'main' into chore/ds-140
Browse files Browse the repository at this point in the history
  • Loading branch information
simotae14 committed Nov 30, 2022
2 parents c31a334 + 3ebd2db commit a39b999
Show file tree
Hide file tree
Showing 21 changed files with 484 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/contributor-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ permissions: {}
jobs:
deploy:
permissions:
contents: write # to push pages branch (peaceiris/actions-gh-pages)
contents: write # to push pages branch (peaceiris/actions-gh-pages)

environment:
name: github-pages
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
workflow_dispatch:

permissions:
contents: read # to fetch code (actions/checkout)
contents: read # to fetch code (actions/checkout)

jobs:
publish:
Expand Down
43 changes: 21 additions & 22 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:
cancel-in-progress: true

permissions:
contents: read # to fetch code (actions/checkout)
contents: read # to fetch code (actions/checkout)

jobs:
lint:
Expand Down Expand Up @@ -142,18 +142,18 @@ jobs:
node: [14, 16, 18]
services:
mysql:
image: mysql
image: bitnami/mysql:latest
env:
MYSQL_ROOT_PASSWORD: strapi
MYSQL_USER: strapi
MYSQL_PASSWORD: strapi
MYSQL_DATABASE: strapi_test
MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
-e MYSQL_ROOT_PASSWORD=strapi
-e MYSQL_ROOT_HOST="%"
-e MYSQL_USER=strapi
-e MYSQL_PASSWORD=strapi
-e MYSQL_DATABASE=strapi_test
--entrypoint sh mysql -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
ports:
# Maps tcp port 5432 on service container to the host
- 3306:3306
Expand All @@ -180,18 +180,17 @@ jobs:
node: [14, 16, 18]
services:
mysql:
image: mysql:5
image: bitnami/mysql:5.7
env:
MYSQL_ROOT_PASSWORD: strapi
MYSQL_USER: strapi
MYSQL_PASSWORD: strapi
MYSQL_DATABASE: strapi_test
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
-e MYSQL_ROOT_PASSWORD=strapi
-e MYSQL_ROOT_HOST="%"
-e MYSQL_USER=strapi
-e MYSQL_PASSWORD=strapi
-e MYSQL_DATABASE=strapi_test
--entrypoint sh mysql -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
ports:
# Maps tcp port 5432 on service container to the host
- 3306:3306
Expand Down Expand Up @@ -288,18 +287,18 @@ jobs:
node: [14, 16, 18]
services:
mysql:
image: mysql
image: bitnami/mysql:latest
env:
MYSQL_ROOT_PASSWORD: strapi
MYSQL_USER: strapi
MYSQL_PASSWORD: strapi
MYSQL_DATABASE: strapi_test
MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
-e MYSQL_ROOT_PASSWORD=strapi
-e MYSQL_ROOT_HOST="%"
-e MYSQL_USER=strapi
-e MYSQL_PASSWORD=strapi
-e MYSQL_DATABASE=strapi_test
--entrypoint sh mysql -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
ports:
# Maps tcp port 5432 on service container to the host
- 3306:3306
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import get from 'lodash/get';
import omit from 'lodash/omit';
import take from 'lodash/take';
import isEqual from 'react-fast-compare';
import { GenericInput, NotAllowedInput, useLibrary, useCustomFields } from '@strapi/helper-plugin';
import { GenericInput, NotAllowedInput, useLibrary } from '@strapi/helper-plugin';
import { useContentTypeLayout } from '../../hooks';
import { getFieldName } from '../../utils';
import Wysiwyg from '../Wysiwyg';
Expand Down Expand Up @@ -37,11 +37,11 @@ function Inputs({
queryInfos,
value,
size,
customFieldInputs,
}) {
const { fields } = useLibrary();
const { formatMessage } = useIntl();
const { contentType: currentContentTypeLayout } = useContentTypeLayout();
const customFieldsRegistry = useCustomFields();

const disabled = useMemo(() => !get(metadatas, 'editable', true), [metadatas]);
const { type, customField: customFieldUid } = fieldSchema;
Expand Down Expand Up @@ -194,19 +194,6 @@ function Inputs({
return minutes % metadatas.step === 0 ? metadatas.step : step;
}, [inputType, inputValue, metadatas.step, step]);

// Memoize the component to avoid remounting it and losing state
const CustomFieldInput = useMemo(() => {
if (customFieldUid) {
const customField = customFieldsRegistry.get(customFieldUid);
const CustomFieldInput = React.lazy(customField.components.Input);

return CustomFieldInput;
}

// Not a custom field, component won't be used
return null;
}, [customFieldUid, customFieldsRegistry]);

if (visible === false) {
return null;
}
Expand Down Expand Up @@ -268,12 +255,9 @@ function Inputs({
media: fields.media,
wysiwyg: Wysiwyg,
...fields,
...customFieldInputs,
};

if (customFieldUid) {
customInputs[customFieldUid] = CustomFieldInput;
}

return (
<GenericInput
attribute={fieldSchema}
Expand Down Expand Up @@ -309,6 +293,7 @@ Inputs.defaultProps = {
size: undefined,
value: null,
queryInfos: {},
customFieldInputs: {},
};

Inputs.propTypes = {
Expand All @@ -330,6 +315,7 @@ Inputs.propTypes = {
defaultParams: PropTypes.object,
endPoint: PropTypes.string,
}),
customFieldInputs: PropTypes.object,
};

const Memoized = memo(Inputs, isEqual);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useEffect, useState } from 'react';
import { useCustomFields } from '@strapi/helper-plugin';

/**
* @description
* A hook to lazy load custom field components
* @param {Array.<string>} componentUids - The uids to look up components
* @returns object
*/
const useLazyComponents = (componentUids) => {
const [lazyComponentStore, setLazyComponentStore] = useState({});
const [loading, setLoading] = useState(true);
const customFieldsRegistry = useCustomFields();

useEffect(() => {
const lazyLoadComponents = async (uids, components) => {
const modules = await Promise.all(components);

uids.forEach((uid, index) => {
if (!Object.keys(lazyComponentStore).includes(uid)) {
setLazyComponentStore({ ...lazyComponentStore, [uid]: modules[index].default });
}
});
};

if (componentUids.length) {
const componentPromises = componentUids.map((uid) => {
const customField = customFieldsRegistry.get(uid);

return customField.components.Input();
});

lazyLoadComponents(componentUids, componentPromises);
}

if (componentUids.length === Object.keys(lazyComponentStore).length) {
setLoading(false);
}
}, [componentUids, customFieldsRegistry, loading, lazyComponentStore]);

return { isLazyLoading: loading, lazyComponentStore };
};

export default useLazyComponents;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { renderHook } from '@testing-library/react-hooks';
import useLazyComponents from '../index';

const mockCustomField = {
name: 'color',
pluginId: 'mycustomfields',
type: 'text',
icon: jest.fn(),
intlLabel: {
id: 'mycustomfields.color.label',
defaultMessage: 'Color',
},
intlDescription: {
id: 'mycustomfields.color.description',
defaultMessage: 'Select any color',
},
components: {
Input: jest.fn().mockResolvedValue({ default: jest.fn() }),
},
};

jest.mock('@strapi/helper-plugin', () => ({
useCustomFields: () => ({
get: jest.fn().mockReturnValue(mockCustomField),
}),
}));

describe('useLazyComponents', () => {
it('lazy loads the components', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useLazyComponents(['plugin::test.test'])
);

expect(result.current).toEqual({ isLazyLoading: true, lazyComponentStore: {} });

await waitForNextUpdate();

expect(JSON.stringify(result.current)).toEqual(
JSON.stringify({
isLazyLoading: false,
lazyComponentStore: { 'plugin::test.test': jest.fn() },
})
);
});
it('handles no components to load', async () => {
const { result } = renderHook(() => useLazyComponents([]));

expect(result.current).toEqual({ isLazyLoading: false, lazyComponentStore: {} });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { AnErrorOccurred } from '@strapi/helper-plugin';
import { Box } from '@strapi/design-system/Box';

const ErrorFallback = () => {
return (
<Box padding={8}>
<AnErrorOccurred />
</Box>
);
};

export default ErrorFallback;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Switch, Route } from 'react-router-dom';
import { ErrorBoundary } from 'react-error-boundary';
import { get } from 'lodash';
import PropTypes from 'prop-types';
import { ErrorFallback, LoadingIndicatorPage, CheckPagePermissions } from '@strapi/helper-plugin';
import { LoadingIndicatorPage, CheckPagePermissions } from '@strapi/helper-plugin';
import permissions from '../../../permissions';
import { ContentTypeLayoutContext } from '../../contexts';
import { useFetchContentTypeLayout } from '../../hooks';
Expand All @@ -12,6 +12,7 @@ import EditViewLayoutManager from '../EditViewLayoutManager';
import EditSettingsView from '../EditSettingsView';
import ListViewLayout from '../ListViewLayoutManager';
import ListSettingsView from '../ListSettingsView';
import ErrorFallback from './components/ErrorFallback';

const cmPermissions = permissions.contentManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Grid, GridItem } from '@strapi/design-system/Grid';
import Inputs from '../../../components/Inputs';
import FieldComponent from '../../../components/FieldComponent';

const GridRow = ({ columns }) => {
const GridRow = ({ columns, customFieldInputs }) => {
return (
<Grid gap={4}>
{columns.map(({ fieldSchema, labelAction, metadatas, name, size, queryInfos }) => {
Expand Down Expand Up @@ -41,6 +41,7 @@ const GridRow = ({ columns }) => {
labelAction={labelAction}
metadatas={metadatas}
queryInfos={queryInfos}
customFieldInputs={customFieldInputs}
/>
</GridItem>
);
Expand All @@ -49,8 +50,13 @@ const GridRow = ({ columns }) => {
);
};

GridRow.defaultProps = {
customFieldInputs: {},
};

GridRow.propTypes = {
columns: PropTypes.array.isRequired,
customFieldInputs: PropTypes.object,
};

export default GridRow;

0 comments on commit a39b999

Please sign in to comment.