create AdminLayout in vulcan:admin

This commit is contained in:
Apollinaire 2019-01-08 16:22:14 +01:00
parent be42d6740f
commit 1da5d5d4f2
3 changed files with 46 additions and 1 deletions

View file

@ -0,0 +1,44 @@
/**
* @Author: Apollinaire Lecocq <apollinaire>
* @Date: 08-01-19
* @Last modified by: apollinaire
* @Last modified time: 08-01-19
*/
import React from 'react';
import {
registerComponent,
Components,
withCurrentUser,
} from 'meteor/vulcan:core';
/**
* A simple component that renders the existing layout and checks wether the currentUser is an admin or not.
*/
class AdminLayout extends React.PureComponent {
renderWithRestrictedAccess(children, currentUser) {
// while the currentUser is loading, don't render anything.
if (currentUser && currentUser.loading) {
return null;
} //if the currentUser is an admin, then render the children
else if (currentUser && currentUser.isAdmin) {
return children;
} // for every other case (just a member or not logged), render the 404 message
return <Components.Error404 />;
}
render() {
const {currentUser, children} = this.props;
return (
<Components.Layout>
{this.renderWithRestrictedAccess(children, currentUser)}
</Components.Layout>
);
}
}
registerComponent({
name: 'AdminLayout',
component: AdminLayout,
hocs: [withCurrentUser],
});

View file

@ -1,3 +1,4 @@
import './fragments.js';
import './routes.js';
import './i18n.js';
import '../components/AdminLayout';

View file

@ -1,5 +1,5 @@
import { addRoute, getDynamicComponent } from 'meteor/vulcan:core';
import React from 'react';
addRoute({ name: 'admin', path: '/admin', component: () => getDynamicComponent(import('../components/AdminHome.jsx'))});
addRoute({ name: 'admin', path: '/admin', component: () => getDynamicComponent(import('../components/AdminHome.jsx')), layoutName: 'AdminLayout'});
addRoute({ name: 'admin2', path: '/admin/users', component: () => getDynamicComponent(import('../components/AdminHome.jsx'))});