// Copyright 2020 The casbin Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React, {Component} from 'react'; import './App.css'; import * as Setting from "./Setting"; import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons'; import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd'; import {Switch, Route, withRouter, Redirect} from 'react-router-dom' import * as AccountBackend from "./backend/AccountBackend"; const { Header, Footer } = Layout; class App extends Component { constructor(props) { super(props); this.state = { classes: props, selectedMenuKey: 0, account: undefined, }; } componentWillMount() { this.updateMenuKey(); this.getAccount(); } updateMenuKey() { // eslint-disable-next-line no-restricted-globals const uri = location.pathname; if (uri === '/') { this.setState({ selectedMenuKey: 0 }); } else { this.setState({ selectedMenuKey: -1 }); } } onLogined() { this.getAccount(); } onUpdateAccount(account) { this.setState({ account: account }); } getAccount() { AccountBackend.getAccount() .then((res) => { const account = Setting.parseJson(res.data); if (window.location.pathname === '/' && account === null) { Setting.goToLink("/"); } this.setState({ account: account, }); if (account !== undefined && account !== null) { window.mouselogUserId = account.username; } }); } logout() { this.setState({ expired: false, submitted: false, }); AccountBackend.logout() .then((res) => { if (res.status === 'ok') { this.setState({ account: null }); Setting.showMessage("success", `Successfully logged out, redirected to homepage`); Setting.goToLink("/"); } else { Setting.showMessage("error", `Logout failed: ${res.msg}`); } }); } handleRightDropdownClick(e) { if (e.key === '0') { this.props.history.push(`/account`); } else if (e.key === '1') { this.logout(); } } renderRightDropdown() { const menu = ( My Account Logout ); return ( {Setting.getShortName(this.state.account.name)}     {Setting.isMobile() ? null : Setting.getShortName(this.state.account.name)}         ) } renderAccount() { let res = []; if (this.state.account !== null && this.state.account !== undefined) { res.push(this.renderRightDropdown()); } else { res.push( Register ); res.push( Login ); res.push( Home ); } return res; } renderMenu() { let res = []; if (this.state.account === null || this.state.account === undefined) { return []; } res.push( Home ); res.push( Programs ); return res; } renderHomeIfLogined(component) { if (this.state.account !== null && this.state.account !== undefined) { return } else { return component; } } renderLoginIfNotLogined(component) { if (this.state.account === null) { return } else if (this.state.account === undefined) { return null; } else { return component; } } isStartPages() { return window.location.pathname.startsWith('/login') || window.location.pathname.startsWith('/register') || window.location.pathname === '/'; } renderContent() { return (
{ Setting.isMobile() ? null : } { this.renderMenu() } { this.renderAccount() }
{/**/}
) } renderFooter() { // How to keep your footer where it belongs ? // https://www.freecodecamp.org/neyarnws/how-to-keep-your-footer-where-it-belongs-59c6aa05c59c/ return ( ) } render() { return (
{ this.renderContent() }
{ this.renderFooter() }
); } } export default withRouter(App);