Advertisement
minafaw3

AppRoute.js

Aug 18th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import {
  5. withRouter, Switch, Route, Redirect,
  6. } from 'react-router-dom';
  7.  
  8. import PrivateRoute from 'lib/PrivateRoute';
  9. import CountrySelectPage from 'scenes/CountrySelectPage';
  10. import ImportPage from 'scenes/Import/ImportPage';
  11. import VerifyEmailPage from 'scenes/VerifyEmailPage';
  12. import LoginPage from 'scenes/LoginPageV2';
  13. import TypePage from 'scenes/TypePage';
  14. import RecoverPasswordPage from 'scenes/recoverPassword';
  15. import RegisterPage from 'scenes/RegisterPage';
  16. import JoinPage from 'scenes/JoinPage';
  17. import RequestPage from 'scenes/RequestPage';
  18. import InviteRequiredPage from 'scenes/InviteRequiredPage';
  19. import CompleteAccount from 'scenes/CompleteAccount';
  20. import CreateAccount from 'scenes/CreateAccountPage';
  21. import Inventory from 'scenes/Inventory';
  22. import Team from 'scenes/Team';
  23. import History from 'scenes/History';
  24. import CategoryPage from 'scenes/CategoryPage';
  25. import LocationPage from 'scenes/Locations';
  26. import MapPage from 'scenes/MapPage';
  27. import PageNotFound from 'scenes/PageNotFound';
  28. import Workers from 'scenes/Workers';
  29. import SettingsPage from 'scenes/SettingsPage';
  30.  
  31. import { permissionsSelectors } from 'ducks/Team';
  32. import { selectors as authSelectors } from 'ducks/Auth';
  33. import * as rootSelectors from 'ducks/selectors';
  34.  
  35. import DownloadAppPage from 'scenes/DownloadAppPage';
  36. import CreateCompanyPage from 'scenes/CreateCompanyPage';
  37. import AppLayout from './AppLayout';
  38.  
  39. const AppRoutes = ({
  40. location,
  41. isLoggedIn,
  42. hasAcceptedTerms,
  43. country,
  44. canViewTeam,
  45. canAccessWorkers,
  46. shouldShowWorkers,
  47. canAccessCategories,
  48. canViewLocations,
  49. canImportThings,
  50. }) => {
  51. const accessGranted = isLoggedIn && hasAcceptedTerms;
  52. const hasCountry = Boolean(country);
  53.  
  54. let authRedirectPath = '/';
  55.  
  56. if (!hasCountry && !accessGranted) {
  57. authRedirectPath = '/country';
  58. } else if (location.state && location.state.from && location.state.from.pathname && location.state.from.pathname !== '/country') {
  59. authRedirectPath = location.state.from.pathname;
  60. }
  61.  
  62. return (
  63. <Switch>
  64. <PrivateRoute path="/country" component={CountrySelectPage} redirectPath="/login" condition={!hasCountry && !accessGranted} />
  65. <PrivateRoute path="/login" component={LoginPage} redirectPath={authRedirectPath} condition={!accessGranted} />
  66. <Route path="/invites" component={LoginPage} />
  67. <Route path="/download-app" component={DownloadAppPage} />
  68. <PrivateRoute path="/register" component={RegisterPage} redirectPath={authRedirectPath} condition={hasCountry && !accessGranted} />
  69. <PrivateRoute path="/forgotPassword" component={RecoverPasswordPage} redirectPath={authRedirectPath} condition={hasCountry && !accessGranted} />
  70. <PrivateRoute path="/request-account" component={RequestPage} redirectPath={authRedirectPath} condition={!accessGranted} />
  71. <PrivateRoute path="/create-account" component={CreateAccount} redirectPath={authRedirectPath} condition={!accessGranted} />
  72. <PrivateRoute path="/invite-required" component={InviteRequiredPage} redirectPath={authRedirectPath} condition={!accessGranted} />
  73. <PrivateRoute path="/password-reset" component={RecoverPasswordPage} redirectPath={authRedirectPath} condition={hasCountry && !accessGranted} />
  74. <PrivateRoute path="/complete/account" component={CompleteAccount} redirectPath={authRedirectPath} condition={hasCountry && !accessGranted} />
  75. <PrivateRoute path="/join" component={JoinPage} redirectPath={authRedirectPath} condition={!accessGranted} />
  76. <PrivateRoute path="/verify-email" component={VerifyEmailPage} redirectPath={authRedirectPath} condition={!accessGranted} />
  77. <PrivateRoute path="/create-company" component={CreateCompanyPage} redirectPath={authRedirectPath} condition={hasCountry && !accessGranted} />
  78. <PrivateRoute path="/account-type" component={TypePage} redirectPath={authRedirectPath} condition={hasCountry && !accessGranted} />
  79. <PrivateRoute
  80. condition={accessGranted}
  81. redirectPath="/login"
  82. render={routeProps => (
  83. <AppLayout {...routeProps} showFooter={false}>
  84. <Switch>
  85. <Redirect path="/" exact to="/inventory" />
  86. <Redirect path="/tools" to="/inventory" />
  87. <Redirect path="/managers" to="/people" />
  88. <Redirect path="/locations" to="/assignments" />
  89. <Route path="/inventory" component={Inventory} />
  90. <Route path="/map" component={MapPage} />
  91. <Route path="/history" component={History} />
  92. <Route path="/settings" component={SettingsPage} />
  93. <PrivateRoute path="/people" component={Team} redirectPath="/" condition={canViewTeam} />
  94. <PrivateRoute path="/workers" component={Workers} redirectPath="/" condition={canAccessWorkers && shouldShowWorkers} />
  95. <PrivateRoute path="/categories" component={CategoryPage} redirectPath="/" condition={canAccessCategories} />
  96. <PrivateRoute path="/assignments" component={LocationPage} redirectPath="/" condition={canViewLocations} />
  97. <PrivateRoute path="/import" component={ImportPage} redirectPath="/" condition={canImportThings} />
  98. <Route component={PageNotFound} />
  99. </Switch>
  100. </AppLayout>
  101. )}
  102. />
  103. </Switch>
  104. );
  105. };
  106.  
  107. AppRoutes.propTypes = {
  108. isLoggedIn: PropTypes.bool.isRequired,
  109. hasAcceptedTerms: PropTypes.bool.isRequired,
  110. country: PropTypes.string,
  111. canViewTeam: PropTypes.bool.isRequired,
  112. canAccessWorkers: PropTypes.bool.isRequired,
  113. shouldShowWorkers: PropTypes.bool.isRequired,
  114. canAccessCategories: PropTypes.bool.isRequired,
  115. canViewLocations: PropTypes.bool.isRequired,
  116. canImportThings: PropTypes.bool.isRequired,
  117. location: PropTypes.shape({
  118. state: PropTypes.shape(),
  119. }).isRequired,
  120. };
  121.  
  122. AppRoutes.defaultProps = {
  123. country: '',
  124. };
  125.  
  126. const mapStateToProps = state => ({
  127. isLoggedIn: authSelectors.getIsLoggedIn(state),
  128. hasAcceptedTerms: authSelectors.getHasAcceptedTerms(state),
  129. country: authSelectors.getUserCountry(state),
  130. canViewTeam: permissionsSelectors.canViewTeam(state),
  131. canAccessWorkers: permissionsSelectors.canAccessWorkers(state),
  132. canAccessCategories: permissionsSelectors.canAccessCategories(state),
  133. canViewLocations: permissionsSelectors.canViewLocations(state),
  134. canImportThings: permissionsSelectors.canImportThings(state),
  135. shouldShowWorkers: rootSelectors.shouldShowWorkers(state),
  136. });
  137.  
  138. export default withRouter(connect(mapStateToProps)(AppRoutes));
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement