2016-08-19 13:57:50 +02:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* LeftMenu
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-09 12:05:59 +02:00
|
|
|
import React, { useContext, useEffect, useMemo, useReducer } from 'react';
|
2020-02-07 14:46:12 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-06-08 11:25:05 +02:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2020-06-08 16:51:42 +02:00
|
|
|
import { UserContext, hasPermissions } from 'strapi-helper-plugin';
|
2020-06-08 11:25:05 +02:00
|
|
|
import {
|
|
|
|
|
LeftMenuLinksSection,
|
|
|
|
|
LeftMenuFooter,
|
|
|
|
|
LeftMenuHeader,
|
|
|
|
|
LeftMenuLinkContainer,
|
|
|
|
|
LinksContainer,
|
|
|
|
|
} from '../../components/LeftMenu';
|
2020-06-09 12:05:59 +02:00
|
|
|
import init from './init';
|
2020-06-08 13:15:01 +02:00
|
|
|
import reducer, { initialState } from './reducer';
|
2020-06-08 16:51:42 +02:00
|
|
|
import Loader from './Loader';
|
2019-09-17 17:19:10 +02:00
|
|
|
import Wrapper from './Wrapper';
|
2016-08-19 13:57:50 +02:00
|
|
|
|
2020-06-08 11:25:05 +02:00
|
|
|
const LeftMenu = ({ version, plugins }) => {
|
|
|
|
|
const location = useLocation();
|
2020-06-08 13:15:01 +02:00
|
|
|
const permissions = useContext(UserContext);
|
2020-06-09 12:05:59 +02:00
|
|
|
const [{ generalSectionLinks, isLoading, pluginsSectionLinks }, dispatch] = useReducer(
|
|
|
|
|
reducer,
|
|
|
|
|
initialState,
|
|
|
|
|
() => init(initialState, plugins)
|
|
|
|
|
);
|
|
|
|
|
const generalSectionLinksFiltered = useMemo(
|
|
|
|
|
() => generalSectionLinks.filter(link => link.isDisplayed),
|
|
|
|
|
[generalSectionLinks]
|
|
|
|
|
);
|
|
|
|
|
const pluginsSectionLinksFiltered = useMemo(
|
|
|
|
|
() => pluginsSectionLinks.filter(link => link.isDisplayed),
|
|
|
|
|
[pluginsSectionLinks]
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-08 13:15:01 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
const getLinksPermissions = async () => {
|
2020-06-08 16:51:42 +02:00
|
|
|
const checkPermissions = async (index, permissionsToCheck) => {
|
|
|
|
|
const hasPermission = await hasPermissions(permissions, permissionsToCheck);
|
|
|
|
|
|
|
|
|
|
return { index, hasPermission };
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-09 12:05:59 +02:00
|
|
|
const generateArrayOfPromises = array =>
|
|
|
|
|
array.map((_, index) => checkPermissions(index, array[index].permissions));
|
|
|
|
|
|
|
|
|
|
const generalSectionLinksArrayOfPromises = generateArrayOfPromises(generalSectionLinks);
|
|
|
|
|
const pluginsSectionLinksArrayOfPromises = generateArrayOfPromises(pluginsSectionLinks);
|
2020-06-08 13:15:01 +02:00
|
|
|
|
2020-06-09 12:05:59 +02:00
|
|
|
const generalSectionResults = await Promise.all(generalSectionLinksArrayOfPromises);
|
|
|
|
|
const pluginsSectionResults = await Promise.all(pluginsSectionLinksArrayOfPromises);
|
2020-06-08 16:51:42 +02:00
|
|
|
|
2020-06-09 08:37:41 +02:00
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_LINK_PERMISSIONS',
|
2020-06-09 12:05:59 +02:00
|
|
|
data: {
|
|
|
|
|
generalSectionLinks: generalSectionResults,
|
|
|
|
|
pluginsSectionLinks: pluginsSectionResults,
|
|
|
|
|
},
|
2020-06-08 13:15:01 +02:00
|
|
|
});
|
2020-06-08 16:51:42 +02:00
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'TOGGLE_IS_LOADING',
|
|
|
|
|
});
|
2020-06-08 13:15:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getLinksPermissions();
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
2020-06-08 11:25:05 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Wrapper>
|
2020-06-08 16:51:42 +02:00
|
|
|
<Loader show={isLoading} />
|
2020-06-08 11:25:05 +02:00
|
|
|
<LeftMenuHeader />
|
|
|
|
|
<LinksContainer>
|
|
|
|
|
<LeftMenuLinkContainer plugins={plugins} />
|
2020-06-09 12:05:59 +02:00
|
|
|
<LeftMenuLinksSection
|
|
|
|
|
section="plugins"
|
|
|
|
|
name="plugins"
|
|
|
|
|
links={pluginsSectionLinksFiltered}
|
|
|
|
|
location={location}
|
|
|
|
|
searchable={false}
|
|
|
|
|
emptyLinksListMessage="app.components.LeftMenuLinkContainer.noPluginsInstalled"
|
|
|
|
|
/>
|
|
|
|
|
{generalSectionLinksFiltered.length && (
|
2020-06-08 13:15:01 +02:00
|
|
|
<LeftMenuLinksSection
|
|
|
|
|
section="general"
|
|
|
|
|
name="general"
|
2020-06-09 12:05:59 +02:00
|
|
|
links={generalSectionLinksFiltered}
|
2020-06-08 13:15:01 +02:00
|
|
|
location={location}
|
|
|
|
|
searchable={false}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-06-08 11:25:05 +02:00
|
|
|
</LinksContainer>
|
|
|
|
|
<LeftMenuFooter key="footer" version={version} />
|
|
|
|
|
</Wrapper>
|
|
|
|
|
);
|
|
|
|
|
};
|
2016-08-19 13:57:50 +02:00
|
|
|
|
2020-02-07 14:46:12 +01:00
|
|
|
LeftMenu.propTypes = {
|
|
|
|
|
version: PropTypes.string.isRequired,
|
|
|
|
|
plugins: PropTypes.object.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default LeftMenu;
|