diff --git a/src/app.js b/src/app.js index b501391..acc0efc 100644 --- a/src/app.js +++ b/src/app.js @@ -37,7 +37,7 @@ const initAppComponents = () => { routerPagesContainer.addRoutes([ {url: ROUTES.MAIN, pageComponent: MainPage}, {url: ROUTES.STORE, pageComponent: ApiPage}, - {url: ROUTES.LOGS, pageComponent: LogsPage}, + {url: ROUTES.LOGS, pageComponent: LogsPage, onlyAdmin: true}, {url: ROUTES.USERS, pageComponent: UsersPage}, {url: ROUTES.LOGIN, pageComponent: LoginPage}, {url: ROUTES.PROFILE, pageComponent: ProfilePage}, diff --git a/src/core/components/main-menu/MainMenu.js b/src/core/components/main-menu/MainMenu.js index 122de39..47428f6 100644 --- a/src/core/components/main-menu/MainMenu.js +++ b/src/core/components/main-menu/MainMenu.js @@ -4,6 +4,7 @@ import routeService from '../../../services/RouteService'; import './MainMenu.css'; import {EVENTS, ROUTES, TAG_NAME} from '../../consts'; import tokenApi from '../../../api/TokenAPI'; +import userInfoService from '../../../services/UserInfoService'; const NAV_MENU = [ { @@ -17,6 +18,7 @@ const NAV_MENU = [ { title: 'Журнал', url: ROUTES.LOGS, + onlyAdmin: true, }, { title: 'Пользователи', @@ -76,7 +78,7 @@ class MainMenu extends Component { } render = () => { - this.menuItems = NAV_MENU.map(({url, title, className = ''}) => { + this.menuItems = NAV_MENU.map(({url, title, className = '', onlyAdmin}) => { const li = this.createElement({ tagName: TAG_NAME.LI, parentNode: this.buttonsContainer, @@ -99,7 +101,7 @@ class MainMenu extends Component { } }); - return {url, link}; + return {url, li, link, onlyAdmin}; }); } @@ -111,7 +113,12 @@ class MainMenu extends Component { this.mainNode.remove(); } - showMenu = () => { + showMenu = async () => { + const {is_admin} = await userInfoService.getUserInfo(); + this.menuItems.forEach(({li, onlyAdmin}) => { + const isShow = !onlyAdmin || is_admin; + li.style.display = isShow ? 'list-item' : 'none'; + }); document.body.prepend(this.mainNode); } } diff --git a/src/core/components/router-pages-container/RouterPagesContainer.js b/src/core/components/router-pages-container/RouterPagesContainer.js index 98f9b20..49c53f9 100644 --- a/src/core/components/router-pages-container/RouterPagesContainer.js +++ b/src/core/components/router-pages-container/RouterPagesContainer.js @@ -3,6 +3,7 @@ import routeService from '../../../services/RouteService'; import NotFoundPage from '../../../pages/not-found/components/page/Page'; import {EVENTS, ROUTES} from '../../consts'; +import userInfoService from '../../../services/UserInfoService'; /** * @interface Route @@ -59,6 +60,14 @@ class RouterPagesContainer extends Component { this.currentPage = new PageComponent('#page', this.mainNode); } }); + + this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => { + const {url} = routeService.getUrlData(); + const currentRoute = this.routes.find((route) => route.url === url); + if (currentRoute.onlyAdmin && !is_admin) { + routeService.goTo(ROUTES.MAIN); + } + }); } /** diff --git a/src/pages/users/components/page/Page.js b/src/pages/users/components/page/Page.js index 17b5afb..cbc5696 100644 --- a/src/pages/users/components/page/Page.js +++ b/src/pages/users/components/page/Page.js @@ -27,7 +27,7 @@ class UsersPage extends Component { }); this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => { - this.createUserButton.disabled = !is_admin; + this.setCreateUserButton(is_admin); }); this.addEventListener(this.createUserButton, EVENTS.CLICK, () => { @@ -61,9 +61,13 @@ class UsersPage extends Component { this.initPage(); } + setCreateUserButton = (isAdmin) => { + this.createUserButton.style.display = isAdmin ? 'inline-block' : 'none'; + } + initPage = async () => { const user = await userInfoService.getUserInfo(); - this.createUserButton.disabled = !user.is_admin; + this.setCreateUserButton(user.is_admin); this.userList = await usersServiceApi.request(); this.renderTable(); }