Enhancement: Extend useEnterprise hook by an enabled boolean flag

This commit is contained in:
Gustav Hansen 2023-06-21 13:36:41 +02:00
parent 3117d9f99e
commit b88b9e8af8
3 changed files with 17 additions and 4 deletions

View File

@ -54,3 +54,8 @@ if (!Component) {
return; return;
} }
``` ```
### `enabled`
Similar to react-query this boolean flag allows disabling the EE import, e.g. when more than one condition needs to be applied. If `enabled`
is set to false, the first argument (CE_DATA) will be returned.

View File

@ -54,6 +54,14 @@ describe('useEnterprise (EE)', () => {
await waitFor(() => expect(result.current).toStrictEqual(EE_DATA_FIXTURE)); await waitFor(() => expect(result.current).toStrictEqual(EE_DATA_FIXTURE));
}); });
test('Returns CE data, when enabled is set to false', async () => {
const { result } = setup(CE_DATA_FIXTURE, async () => EE_DATA_FIXTURE, {
enabled: false,
});
await waitFor(() => expect(result.current).toStrictEqual(CE_DATA_FIXTURE));
});
test('Returns a custom defaultValue on first render followed by the EE data', async () => { test('Returns a custom defaultValue on first render followed by the EE data', async () => {
const { result } = setup(CE_DATA_FIXTURE, async () => EE_DATA_FIXTURE, { const { result } = setup(CE_DATA_FIXTURE, async () => EE_DATA_FIXTURE, {
defaultValue: false, defaultValue: false,

View File

@ -9,7 +9,7 @@ function isEnterprise() {
export function useEnterprise( export function useEnterprise(
ceData, ceData,
eeCallback, eeCallback,
{ defaultValue = null, combine = (ceData, eeData) => eeData } = {} { defaultValue = null, combine = (ceData, eeData) => eeData, enabled = true } = {}
) { ) {
const eeCallbackRef = useCallbackRef(eeCallback); const eeCallbackRef = useCallbackRef(eeCallback);
const combineCallbackRef = useCallbackRef(combine); const combineCallbackRef = useCallbackRef(combine);
@ -17,7 +17,7 @@ export function useEnterprise(
// We have to use a nested object here, because functions (e.g. Components) // We have to use a nested object here, because functions (e.g. Components)
// can not be stored as value directly // can not be stored as value directly
const [{ data }, setData] = React.useState({ const [{ data }, setData] = React.useState({
data: isEnterprise() ? defaultValue : ceData, data: isEnterprise() && enabled ? defaultValue : ceData,
}); });
React.useEffect(() => { React.useEffect(() => {
@ -27,10 +27,10 @@ export function useEnterprise(
setData({ data: combineCallbackRef(ceData, eeData) }); setData({ data: combineCallbackRef(ceData, eeData) });
} }
if (isEnterprise()) { if (isEnterprise() && enabled) {
importEE(); importEE();
} }
}, [ceData, eeCallbackRef, combineCallbackRef]); }, [ceData, eeCallbackRef, combineCallbackRef, enabled]);
return data; return data;
} }