mirror of
https://github.com/strapi/strapi.git
synced 2025-09-27 01:09:49 +00:00
useFolderStructure: Split out utility function + add tests
This commit is contained in:
parent
b8f52b8d7f
commit
65a5f2ed34
@ -2,21 +2,13 @@ import { useQuery } from 'react-query';
|
|||||||
|
|
||||||
import pluginId from '../pluginId';
|
import pluginId from '../pluginId';
|
||||||
import { axiosInstance, getRequestUrl } from '../utils';
|
import { axiosInstance, getRequestUrl } from '../utils';
|
||||||
|
import { recursiveRenameKeys } from './utils/rename-keys';
|
||||||
|
|
||||||
const FIELD_MAPPING = {
|
const FIELD_MAPPING = {
|
||||||
name: 'label',
|
name: 'label',
|
||||||
id: 'value',
|
id: 'value',
|
||||||
};
|
};
|
||||||
|
|
||||||
const renameKeys = (obj, fn) =>
|
|
||||||
Object.fromEntries(
|
|
||||||
Object.entries(obj).map(([key, value]) => {
|
|
||||||
const getValue = v => (typeof v === 'object' && v !== null ? renameKeys(v, fn) : v);
|
|
||||||
|
|
||||||
return [fn(key), Array.isArray(value) ? value.map(val => getValue(val)) : getValue(value)];
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export const useFolderStructure = ({ enabled = true } = {}) => {
|
export const useFolderStructure = ({ enabled = true } = {}) => {
|
||||||
const dataRequestURL = getRequestUrl('folder-structure');
|
const dataRequestURL = getRequestUrl('folder-structure');
|
||||||
|
|
||||||
@ -25,7 +17,7 @@ export const useFolderStructure = ({ enabled = true } = {}) => {
|
|||||||
data: { data },
|
data: { data },
|
||||||
} = await axiosInstance.get(dataRequestURL);
|
} = await axiosInstance.get(dataRequestURL);
|
||||||
|
|
||||||
return data.map(f => renameKeys(f, key => FIELD_MAPPING?.[key] ?? key));
|
return data.map(f => recursiveRenameKeys(f, key => FIELD_MAPPING?.[key] ?? key));
|
||||||
};
|
};
|
||||||
|
|
||||||
const { data, error, isLoading } = useQuery(
|
const { data, error, isLoading } = useQuery(
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
export const recursiveRenameKeys = (obj, fn) =>
|
||||||
|
Object.fromEntries(
|
||||||
|
Object.entries(obj).map(([key, value]) => {
|
||||||
|
const getValue = v => (typeof v === 'object' && v !== null ? recursiveRenameKeys(v, fn) : v);
|
||||||
|
|
||||||
|
return [fn(key), Array.isArray(value) ? value.map(val => getValue(val)) : getValue(value)];
|
||||||
|
})
|
||||||
|
);
|
@ -0,0 +1,45 @@
|
|||||||
|
import { recursiveRenameKeys } from '../rename-keys';
|
||||||
|
|
||||||
|
const FIXTURE = {
|
||||||
|
foo: 'bar',
|
||||||
|
bar: 'foo',
|
||||||
|
|
||||||
|
nested: {
|
||||||
|
foo: 'baz',
|
||||||
|
baz: 'bar',
|
||||||
|
|
||||||
|
deeper: {
|
||||||
|
foo: false,
|
||||||
|
bam: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('recursiveRenameKeys', () => {
|
||||||
|
test('does rename keys', () => {
|
||||||
|
expect(
|
||||||
|
recursiveRenameKeys(FIXTURE, key => {
|
||||||
|
switch (key) {
|
||||||
|
case 'foo':
|
||||||
|
return 'bam';
|
||||||
|
|
||||||
|
case 'baz':
|
||||||
|
return 'bar';
|
||||||
|
|
||||||
|
default:
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
).toStrictEqual({
|
||||||
|
bam: 'bar',
|
||||||
|
bar: 'foo',
|
||||||
|
nested: {
|
||||||
|
bam: 'baz',
|
||||||
|
bar: 'bar',
|
||||||
|
deeper: {
|
||||||
|
bam: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user