Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/github-test-mysql5
- Loading branch information
Showing
12 changed files
with
304 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/core/admin/admin/src/content-manager/hooks/useLazyComponents/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
50 changes: 50 additions & 0 deletions
50
...e/admin/admin/src/content-manager/hooks/useLazyComponents/tests/useLazyComponents.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} }); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
...n/admin/src/content-manager/pages/CollectionTypeRecursivePath/components/ErrorFallback.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.