57 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
*
* Initializer
*
*/
2019-09-10 12:14:07 +02:00
import { useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { chain } from 'lodash';
import { request } from 'strapi-helper-plugin';
import pluginId from '../../pluginId';
const Initializer = ({ updatePlugin }) => {
const ref = useRef();
ref.current = updatePlugin;
useEffect(() => {
const getData = async () => {
2019-12-04 10:54:57 +01:00
// When updating this we also need to update the content-type-builder/admin/src/containers/DataManager/index.js => updateAppMenu
// since it uses the exact same method...
const requestURL = `/${pluginId}/content-types`;
try {
const { data } = await request(requestURL, { method: 'GET' });
// Two thinks to know here:
// First, we group content types by schema.kind to get an object with two separated content types (singleTypes, collectionTypes)
// Then, we sort by name to keep collection types at the first position everytime.
// As all content types are sort by name, if a single type name start with abc, the single types section will be at the first position.
// However, we want to keep collection types at the first position in the admin menu
ref.current(
pluginId,
'leftMenuSections',
chain(data)
.groupBy('schema.kind')
.map((value, key) => ({ name: key, links: value }))
.sortBy('name')
.value()
);
2019-09-10 12:14:07 +02:00
ref.current(pluginId, 'isReady', true);
} catch (err) {
strapi.notification.error('content-manager.error.model.fetch');
}
};
getData();
}, []);
return null;
};
Initializer.propTypes = {
updatePlugin: PropTypes.func.isRequired,
};
2019-09-10 12:14:07 +02:00
export default Initializer;