Created
November 20, 2019 08:52
-
-
Save erwanriou/6485c6ec55f6aa7000aa1fdc70975830 to your computer and use it in GitHub Desktop.
Exemple of codesplitting optimization based on router component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Suspense, lazy } from "react" | |
import { connect } from "react-redux" | |
import { Switch, withRouter } from "react-router-dom" | |
// IMPORT ROUTES | |
import { Routes } from "../Routes" | |
// IMPORT COMPONENTS | |
import Loading from "../common/Loader" | |
// LOADED COMPONENTS | |
const PrivateRoleRoute = lazy(() => import("../common/PrivateRoleRoute")) | |
const PrivateRoute = lazy(() => import("../common/PrivateRoute")) | |
const Head = lazy(() => import("../common/Head")) | |
const DashboardBreadscrums = lazy(() => import("./DashboardBreadscrums")) | |
const DashboardSidebar = lazy(() => import("./DashboardSidebar")) | |
class Dashboard extends React.Component { | |
render() { | |
const { user, loading } = this.props.auth | |
return ( | |
<main | |
className="dashboard" | |
style={{ | |
width: | |
this.props.auth.isAuthenticated === true && | |
this.props.auth.user.isActivated === true | |
? "calc(100% - 3rem)" | |
: "100%" | |
}} | |
> | |
<Suspense fallback={<Loading />}> | |
<Head component="dashboard" /> | |
<DashboardSidebar user={user} loading={loading} /> | |
<DashboardBreadscrums /> | |
<Switch> | |
{Routes.dashboard.global.map((page, index) => ( | |
<PrivateRoute | |
exact={page.exact} | |
key={index} | |
path={page.path} | |
component={page.component} | |
/> | |
))} | |
{Routes.dashboard.architect.map((page, index) => ( | |
<PrivateRoleRoute | |
exact | |
key={index} | |
path={page.path} | |
roles={page.roles} | |
component={page.component} | |
/> | |
))} | |
{Routes.dashboard.user.map((page, index) => ( | |
<PrivateRoleRoute | |
exact | |
key={index} | |
path={page.path} | |
roles={page.roles} | |
component={page.component} | |
/> | |
))} | |
{Routes.dashboard.admin.map((page, index) => ( | |
<PrivateRoleRoute | |
exact | |
key={index} | |
path={page.path} | |
roles={page.roles} | |
component={page.component} | |
/> | |
))} | |
</Switch> | |
</Suspense> | |
</main> | |
) | |
} | |
} | |
const mapStateToProps = state => ({ | |
auth: state.auth | |
}) | |
export default withRouter(connect(mapStateToProps)(Dashboard)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react" | |
import { connect } from "react-redux" | |
import { getTranslate } from "react-localize-redux" | |
import { Helmet } from "react-helmet-async" | |
const Head = ({ translate, component }) => { | |
return ( | |
<Helmet> | |
<title>{translate(`meta.${component}.title`)}</title> | |
<meta | |
http-equiv="content-language" | |
content={translate(`meta.${component}.language`)} | |
/> | |
<meta name="keywords" content={translate(`meta.${component}.keywords`)} /> | |
<meta | |
name="description" | |
content={translate(`meta.${component}.meta-desc`)} | |
/> | |
</Helmet> | |
) | |
} | |
const mapStateToProps = state => ({ | |
translate: getTranslate(state.localize) | |
}) | |
export default connect(mapStateToProps)(Head) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react" | |
import { Route, Redirect } from "react-router-dom" | |
import PropTypes from "prop-types" | |
import { connect } from "react-redux" | |
const PrivateRoleRoute = ({ component: Component, auth, roles, ...rest }) => ( | |
<Route | |
{...rest} | |
render={props => | |
auth.isAuthenticated === true && auth.user.authorities.includes(roles) ? ( | |
<Component {...props} /> | |
) : ( | |
<Redirect to="/login" /> | |
) | |
} | |
/> | |
) | |
PrivateRoleRoute.propTypes = { | |
auth: PropTypes.object.isRequired | |
} | |
const mapStateToProps = state => ({ | |
auth: state.auth | |
}) | |
export default connect(mapStateToProps)(PrivateRoleRoute) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment