2016-08-19 13:57:50 +02:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* LeftMenu
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-08 13:15:01 +02:00
|
|
|
import React, { useContext, useEffect, 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-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-08 16:51:42 +02:00
|
|
|
const [{ generalSectionLinks, isLoading }, dispatch] = useReducer(reducer, initialState);
|
2020-06-08 13:15:01 +02:00
|
|
|
const filteredLinks = generalSectionLinks.filter(link => link.isDisplayed);
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const generalSectionLinksArrayOfPromises = generalSectionLinks.map((_, index) =>
|
|
|
|
|
checkPermissions(index, generalSectionLinks[index].permissions)
|
|
|
|
|
);
|
2020-06-08 13:15:01 +02:00
|
|
|
|
2020-06-08 16:51:42 +02:00
|
|
|
const results = await Promise.all(generalSectionLinksArrayOfPromises);
|
|
|
|
|
|
|
|
|
|
results.forEach(result => {
|
2020-06-08 13:15:01 +02:00
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_LINK_PERMISSION',
|
2020-06-08 16:51:42 +02:00
|
|
|
...result,
|
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-08 13:15:01 +02:00
|
|
|
{filteredLinks.length && (
|
|
|
|
|
<LeftMenuLinksSection
|
|
|
|
|
section="general"
|
|
|
|
|
name="general"
|
|
|
|
|
links={filteredLinks}
|
|
|
|
|
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;
|