Browse Source

fix endline

pull/1/head
wahyu 1 year ago
parent
commit
025f499341
  1. 654
      src/containers/DefaultLayout/DefaultLayout.js

654
src/containers/DefaultLayout/DefaultLayout.js

@ -1,327 +1,327 @@
import React, { Component, Suspense, Divider } from 'react';
import { Redirect, Route, Switch, withRouter } from 'react-router-dom';
import * as router from 'react-router-dom';
import { Button, Container, UncontrolledDropdown, DropdownItem, DropdownMenu, DropdownToggle, Nav, NavItem } from 'reactstrap';
import axios from 'axios';
import { ALERTUSER_SEARCH, ALERT_SEARCH, MENU_SEARCH, ROLEMENU_SEARCH, ALERTUSER_STATUSVIEW, ALERT_STATUSVIEW, MENU_MANAGEMENT, CONTROL_MONITORING_SEARCH } from '../../const/ApiConst';
import { handleChangeLng } from "../../utils/LangUtils";
import {
AppAside,
AppFooter,
AppHeader,
AppSidebar,
AppSidebarFooter,
AppSidebarForm,
AppSidebarHeader,
AppSidebarMinimizer,
AppBreadcrumb2 as AppBreadcrumb,
AppSidebarNav2 as AppSidebarNav,
} from '@coreui/react';
import LayoutHelper from '../../../node_modules/@coreui/react/lib/Shared/layout/layout';
import navigation from '../../_nav';
import routes from '../../routes';
import { emptyConstants } from '../../const/MapConst.js';
const countErr = localStorage.getItem('countErr');
const token = window.localStorage.getItem('token');
const DefaultAside = React.lazy(() => import('./DefaultAside'));
const DefaultFooter = React.lazy(() => import('./DefaultFooter'));
const DefaultHeader = React.lazy(() => import('./DefaultHeader'));
let errorCount = 0;
const config = {
headers:
{
Authorization: `Bearer ${token}`,
"Content-type": `application/json`
}
};
class DefaultLayout extends Component {
constructor(props) {
super(props);
this.state = {
role_id: localStorage.getItem('role_id'),
token: localStorage.getItem('token'),
menu: { items: [] },
routes2: routes,
finalRoutes: [],
breadrCrumbReady: false,
minimized: true
}
}
async componentDidMount() {
const menu = localStorage.getItem("menu_login")
if (menu) this.setMenu(JSON.parse(menu))
this.getAppBreadcrumb();
if (!localStorage.getItem("token")) {
this.signOut();
}
}
componentDidUpdate(prevProps, prevState) {
const menu = localStorage.getItem("menu_login")
const { pathname } = this.props.location;
if (this.state.token !== prevState.token) {
this.setMenu(JSON.parse(menu))
}
if (this.state.role_id !== prevState.role_id) {
this.setMenu(JSON.parse(menu))
}
if (this.state.menu !== prevState.menu) {
this.setFinalRoutes()
}
}
toggleMinimized = () => {
this.setState((prevState) => ({ minimized: !prevState.minimized }));
LayoutHelper.sidebarToggle(!this.state.minimized);
};
setFinalRoutes = () => {
const { routes2 } = this.state;
if (routes2) {
const newRouter2 = routes2;
this.state.routes2.map((val, index) => {
let indexRes = this.state.menu.items.findIndex(x => x.url === val.path);
if (indexRes >= 0) {
let obj = newRouter2[index]
obj['name'] = this.state.menu.items[indexRes].name
newRouter2[index] = obj
}
})
this.setState({ finalRoutes: newRouter2 }, () => {
this.setState({ breadrCrumbReady: true })
});
}
}
loading = () => <div className="animated fadeIn pt-1 text-center">Loading...</div>
async signOut(e) {
if (e) {
e.preventDefault()
}
await localStorage.removeItem("role_id");
await window.localStorage.clear();
this.props.history.replace('/login')
}
getDataMenu = async () => {
errorCount++;
let url = MENU_MANAGEMENT(this.state.role_id)
const result = await axios
.get(url, config)
.then(res => res)
.catch((error) => error.response);
if (result && result.data && result.data.code == 200) {
window.localStorage.setItem('countErr', 0);
let resData = result.data.data
this.setMenu(resData)
} else {
if (errorCount < 4) {
this.getDataMenu();
} else {
if (countErr) {
await window.localStorage.setItem('countErr', countErr + 1);
} else {
await window.localStorage.setItem('countErr', 1);
}
if (countErr) {
if (parseInt(countErr) > 2) {
// this.signOut();
} else {
window.location.reload();
}
} else {
window.location.reload();
}
}
}
}
parseMenuChild = (data) => {
const menu = data.map(res => {
const obj = {
name: res.name,
url: res.url,
icon: res.icon
}
if (res.children.length > 0) {
obj.children = this.parseMenuChild(res.children)
} else {
delete obj.children
}
return obj
})
return menu
}
setMenu = data => {
const menu = data.map(res => {
const obj = {
name: res.name,
url: res.url,
icon: res.icon
}
if (res.children.length > 0) {
obj.children = this.parseMenuChild(res.children)
} else {
delete obj.children
}
return obj
})
this.setState({ menu: { items: menu } }, () => {
})
}
checkLocation = () => {
const { pathname } = this.props.location;
let getIndex = this.getIndexMenu(pathname);
if (getIndex < 0) {
this.props.history.push("/403");
}
}
getIndexMenu = (url) => {
let index = this.state.menu.items.findIndex(obj => obj.url === url);
return index
}
getMenu = () => {
const { u_group } = this.state;
if (u_group == 'kominfo') {
}
else {
return <AppSidebarNav navConfig={this.state.menu} {...this.props} router={router} />
}
}
getAppBreadcrumb = () => {
const { u_group } = this.state;
if (u_group == 'kominfo') {
routes.map((route, idx) => {
if (route.path == '/dashboard-kominfo') {
return <AppBreadcrumb appRoutes={routes} router={router} className="remove-gap-bs-breadcrumb" />
}
else {
return <AppBreadcrumb appRoutes={routes} router={router} />
}
});
}
else {
if (!window.location.href.includes("dashboard")) {
return <AppBreadcrumb appRoutes={this.state.finalRoutes} router={router} />
}
}
}
isKominfo = () => {
let u_group = localStorage.getItem('u_group');
if (u_group == 'kominfo') {
return true;
}
else {
return false;
}
}
render() {
const { location } = this.props;
const { pathname } = location;
let renderSidebar = false
if (pathname.includes("/dashboard-project")) {
// Remove the base URL and hash
const path = pathname.replace("/dashboard-project/", "");
// Split the remaining path by "/"
const parts = path.split("/");
if (parts[2] == "1") {
renderSidebar = true
}
}
return (
<div className="app">
<div className="app-body">
{!window.location.href.includes("dashboard-project") || renderSidebar ? (
<AppSidebar minimized={this.state.minimized} fixed display="lg">
{/* <div class="sidebar-header">
<Suspense fallback={this.loading()}>
<DefaultHeader history={this.props.history} onLogout={e => this.signOut(e)} />
</Suspense>
</div> */}
<hr />
<AppSidebarHeader />
<AppSidebarForm />
<Suspense>
{this.getMenu()}
</Suspense>
<AppSidebarFooter />
{this.state.minimized ? null :
<UncontrolledDropdown direction="down">
<DropdownToggle nav>
<i className="nav-icon fa fa-user-circle"></i> {localStorage.getItem('user_name')}
</DropdownToggle>
<DropdownMenu right>
<DropdownItem color="primary" size="sm" onClick={() => handleChangeLng("id")}>ID</DropdownItem>
<DropdownItem color="success" size="sm" onClick={() => handleChangeLng("en")}>EN</DropdownItem>
<DropdownItem href="#/settings"><i className="fa fa-user"></i>Profile</DropdownItem>
<DropdownItem onClick={e => this.signOut(e)}><i className="fa fa-sign-out"></i> Logout</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>}
<button className='sidebar-minimizer mt-auto' type='button' onClick={this.toggleMinimized}>
</button>
</AppSidebar>
) : null}
<main className="main">
{this.state.breadrCrumbReady ? this.getAppBreadcrumb() : null}
<Container fluid>
<Suspense fallback={this.loading()}>
<Switch>
{routes.map((route, idx) => {
return route.component ? (
<Route
key={idx}
path={route.path}
exact={route.exact}
name={route.name}
render={props => (
<route.component params={{ name: route.name }} {...props} />
)} />
) : (null);
})}
<Redirect from="/" to="/dashboard" />
</Switch>
</Suspense>
</Container>
</main>
<AppAside fixed>
<Suspense fallback={this.loading()}>
<DefaultAside />
</Suspense>
</AppAside>
</div>
{!this.isKominfo ?
(
<AppFooter>
<Suspense fallback={this.loading()}>
<DefaultFooter />
</Suspense>
</AppFooter>
) : null
}
</div>
);
}
}
export default withRouter(DefaultLayout);
import React, { Component, Suspense, Divider } from 'react';
import { Redirect, Route, Switch, withRouter } from 'react-router-dom';
import * as router from 'react-router-dom';
import { Button, Container, UncontrolledDropdown, DropdownItem, DropdownMenu, DropdownToggle, Nav, NavItem } from 'reactstrap';
import axios from 'axios';
import { ALERTUSER_SEARCH, ALERT_SEARCH, MENU_SEARCH, ROLEMENU_SEARCH, ALERTUSER_STATUSVIEW, ALERT_STATUSVIEW, MENU_MANAGEMENT, CONTROL_MONITORING_SEARCH } from '../../const/ApiConst';
import { handleChangeLng } from "../../utils/LangUtils";
import {
AppAside,
AppFooter,
AppHeader,
AppSidebar,
AppSidebarFooter,
AppSidebarForm,
AppSidebarHeader,
AppSidebarMinimizer,
AppBreadcrumb2 as AppBreadcrumb,
AppSidebarNav2 as AppSidebarNav,
} from '@coreui/react';
import LayoutHelper from '../../../node_modules/@coreui/react/lib/Shared/layout/layout';
import navigation from '../../_nav';
import routes from '../../routes';
import { emptyConstants } from '../../const/MapConst.js';
const countErr = localStorage.getItem('countErr');
const token = window.localStorage.getItem('token');
const DefaultAside = React.lazy(() => import('./DefaultAside'));
const DefaultFooter = React.lazy(() => import('./DefaultFooter'));
const DefaultHeader = React.lazy(() => import('./DefaultHeader'));
let errorCount = 0;
const config = {
headers:
{
Authorization: `Bearer ${token}`,
"Content-type": `application/json`
}
};
class DefaultLayout extends Component {
constructor(props) {
super(props);
this.state = {
role_id: localStorage.getItem('role_id'),
token: localStorage.getItem('token'),
menu: { items: [] },
routes2: routes,
finalRoutes: [],
breadrCrumbReady: false,
minimized: true
}
}
async componentDidMount() {
const menu = localStorage.getItem("menu_login")
if (menu) this.setMenu(JSON.parse(menu))
this.getAppBreadcrumb();
if (!localStorage.getItem("token")) {
this.signOut();
}
}
componentDidUpdate(prevProps, prevState) {
const menu = localStorage.getItem("menu_login")
const { pathname } = this.props.location;
if (this.state.token !== prevState.token) {
this.setMenu(JSON.parse(menu))
}
if (this.state.role_id !== prevState.role_id) {
this.setMenu(JSON.parse(menu))
}
if (this.state.menu !== prevState.menu) {
this.setFinalRoutes()
}
}
toggleMinimized = () => {
this.setState((prevState) => ({ minimized: !prevState.minimized }));
LayoutHelper.sidebarToggle(!this.state.minimized);
};
setFinalRoutes = () => {
const { routes2 } = this.state;
if (routes2) {
const newRouter2 = routes2;
this.state.routes2.map((val, index) => {
let indexRes = this.state.menu.items.findIndex(x => x.url === val.path);
if (indexRes >= 0) {
let obj = newRouter2[index]
obj['name'] = this.state.menu.items[indexRes].name
newRouter2[index] = obj
}
})
this.setState({ finalRoutes: newRouter2 }, () => {
this.setState({ breadrCrumbReady: true })
});
}
}
loading = () => <div className="animated fadeIn pt-1 text-center">Loading...</div>
async signOut(e) {
if (e) {
e.preventDefault()
}
await localStorage.removeItem("role_id");
await window.localStorage.clear();
this.props.history.replace('/login')
}
getDataMenu = async () => {
errorCount++;
let url = MENU_MANAGEMENT(this.state.role_id)
const result = await axios
.get(url, config)
.then(res => res)
.catch((error) => error.response);
if (result && result.data && result.data.code == 200) {
window.localStorage.setItem('countErr', 0);
let resData = result.data.data
this.setMenu(resData)
} else {
if (errorCount < 4) {
this.getDataMenu();
} else {
if (countErr) {
await window.localStorage.setItem('countErr', countErr + 1);
} else {
await window.localStorage.setItem('countErr', 1);
}
if (countErr) {
if (parseInt(countErr) > 2) {
// this.signOut();
} else {
window.location.reload();
}
} else {
window.location.reload();
}
}
}
}
parseMenuChild = (data) => {
const menu = data.map(res => {
const obj = {
name: res.name,
url: res.url,
icon: res.icon
}
if (res.children.length > 0) {
obj.children = this.parseMenuChild(res.children)
} else {
delete obj.children
}
return obj
})
return menu
}
setMenu = data => {
const menu = data.map(res => {
const obj = {
name: res.name,
url: res.url,
icon: res.icon
}
if (res.children.length > 0) {
obj.children = this.parseMenuChild(res.children)
} else {
delete obj.children
}
return obj
})
this.setState({ menu: { items: menu } }, () => {
})
}
checkLocation = () => {
const { pathname } = this.props.location;
let getIndex = this.getIndexMenu(pathname);
if (getIndex < 0) {
this.props.history.push("/403");
}
}
getIndexMenu = (url) => {
let index = this.state.menu.items.findIndex(obj => obj.url === url);
return index
}
getMenu = () => {
const { u_group } = this.state;
if (u_group == 'kominfo') {
}
else {
return <AppSidebarNav navConfig={this.state.menu} {...this.props} router={router} />
}
}
getAppBreadcrumb = () => {
const { u_group } = this.state;
if (u_group == 'kominfo') {
routes.map((route, idx) => {
if (route.path == '/dashboard-kominfo') {
return <AppBreadcrumb appRoutes={routes} router={router} className="remove-gap-bs-breadcrumb" />
}
else {
return <AppBreadcrumb appRoutes={routes} router={router} />
}
});
}
else {
if (!window.location.href.includes("dashboard")) {
return <AppBreadcrumb appRoutes={this.state.finalRoutes} router={router} />
}
}
}
isKominfo = () => {
let u_group = localStorage.getItem('u_group');
if (u_group == 'kominfo') {
return true;
}
else {
return false;
}
}
render() {
const { location } = this.props;
const { pathname } = location;
let renderSidebar = false
if (pathname.includes("/dashboard-project")) {
// Remove the base URL and hash
const path = pathname.replace("/dashboard-project/", "");
// Split the remaining path by "/"
const parts = path.split("/");
if (parts[2] == "1") {
renderSidebar = true
}
}
return (
<div className="app">
<div className="app-body">
{!window.location.href.includes("dashboard-project") || renderSidebar ? (
<AppSidebar minimized={this.state.minimized} fixed display="lg">
{/* <div class="sidebar-header">
<Suspense fallback={this.loading()}>
<DefaultHeader history={this.props.history} onLogout={e => this.signOut(e)} />
</Suspense>
</div> */}
<hr />
<AppSidebarHeader />
<AppSidebarForm />
<Suspense>
{this.getMenu()}
</Suspense>
<AppSidebarFooter />
{this.state.minimized ? null :
<UncontrolledDropdown direction="down">
<DropdownToggle nav>
<i className="nav-icon fa fa-user-circle"></i> {localStorage.getItem('user_name')}
</DropdownToggle>
<DropdownMenu right>
<DropdownItem color="primary" size="sm" onClick={() => handleChangeLng("id")}>ID</DropdownItem>
<DropdownItem color="success" size="sm" onClick={() => handleChangeLng("en")}>EN</DropdownItem>
<DropdownItem href="#/settings"><i className="fa fa-user"></i>Profile</DropdownItem>
<DropdownItem onClick={e => this.signOut(e)}><i className="fa fa-sign-out"></i> Logout</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>}
<button className='sidebar-minimizer mt-auto' type='button' onClick={this.toggleMinimized}>
</button>
</AppSidebar>
) : null}
<main className="main">
{this.state.breadrCrumbReady ? this.getAppBreadcrumb() : null}
<Container fluid>
<Suspense fallback={this.loading()}>
<Switch>
{routes.map((route, idx) => {
return route.component ? (
<Route
key={idx}
path={route.path}
exact={route.exact}
name={route.name}
render={props => (
<route.component params={{ name: route.name }} {...props} />
)} />
) : (null);
})}
<Redirect from="/" to="/dashboard" />
</Switch>
</Suspense>
</Container>
</main>
<AppAside fixed>
<Suspense fallback={this.loading()}>
<DefaultAside />
</Suspense>
</AppAside>
</div>
{!this.isKominfo ?
(
<AppFooter>
<Suspense fallback={this.loading()}>
<DefaultFooter />
</Suspense>
</AppFooter>
) : null
}
</div>
);
}
}
export default withRouter(DefaultLayout);

Loading…
Cancel
Save