diff --git a/dashboard/src/containers/Root.tsx b/dashboard/src/containers/Root.tsx index 549dc7b98a7..cd8abd2461f 100644 --- a/dashboard/src/containers/Root.tsx +++ b/dashboard/src/containers/Root.tsx @@ -6,7 +6,7 @@ import Layout from "../components/Layout"; import store, { history } from "../store"; import ConfigLoaderContainer from "./ConfigLoaderContainer"; import HeaderContainer from "./HeaderContainer"; -import Routes from "./Routes"; +import Routes from "./RoutesContainer"; class Root extends React.Component { public render() { diff --git a/dashboard/src/containers/Routes.tsx b/dashboard/src/containers/Routes.tsx deleted file mode 100644 index 513b45e2851..00000000000 --- a/dashboard/src/containers/Routes.tsx +++ /dev/null @@ -1,68 +0,0 @@ -import * as React from "react"; -import { Redirect, Route, RouteComponentProps, Switch } from "react-router"; -import NotFound from "../components/NotFound"; -import store from "../store"; -import AppList from "./AppListContainer"; -import AppNew from "./AppNewContainer"; -import AppUpgrade from "./AppUpgradeContainer"; -import AppView from "./AppViewContainer"; -import ChartList from "./ChartListContainer"; -import ChartView from "./ChartViewContainer"; -import ClassListContainer from "./ClassListContainer"; -import { ClassViewContainer } from "./ClassView"; -import FunctionListContainer from "./FunctionListContainer"; -import FunctionViewContainer from "./FunctionViewContainer"; -import InstanceListViewContainer from "./InstanceListViewContainer"; -import InstanceView from "./InstanceView"; -import LoginFormContainer from "./LoginFormContainer"; -import PrivateRouteContainer from "./PrivateRouteContainer"; -import RepoListContainer from "./RepoListContainer"; -import ServiceCatalogContainer from "./ServiceCatalogContainer"; - -class Routes extends React.Component { - public static exactRoutes: { - [route: string]: React.ComponentType> | React.ComponentType; - } = { - "/apps/ns/:namespace": AppList, - "/apps/ns/:namespace/:releaseName": AppView, - "/apps/ns/:namespace/new/:repo/:id/versions/:version": AppNew, - "/apps/ns/:namespace/upgrade/:releaseName": AppUpgrade, - "/charts": ChartList, - "/charts/:repo": ChartList, - "/charts/:repo/:id": ChartView, - "/charts/:repo/:id/versions/:version": ChartView, - "/config/brokers": ServiceCatalogContainer, - "/config/repos": RepoListContainer, - "/functions/ns/:namespace": FunctionListContainer, - "/functions/ns/:namespace/:name": FunctionViewContainer, - "/services/brokers/:brokerName/classes/:className": ClassViewContainer, - "/services/brokers/:brokerName/instances/ns/:namespace/:instanceName": InstanceView, - "/services/classes": ClassListContainer, - "/services/instances/ns/:namespace": InstanceListViewContainer, - }; - - public render() { - return ( - - - - {Object.keys(Routes.exactRoutes).map(route => ( - - ))} - {/* If the route doesn't match any expected path redirect to a 404 page */} - - - ); - } - public rootNamespacedRedirect = (props: any) => { - const { namespace } = store.getState(); - return ; - }; -} - -export default Routes; diff --git a/dashboard/src/containers/Routes.test.tsx b/dashboard/src/containers/RoutesContainer/Routes.test.tsx similarity index 52% rename from dashboard/src/containers/Routes.test.tsx rename to dashboard/src/containers/RoutesContainer/Routes.test.tsx index e47b6b76f59..39def63b88c 100644 --- a/dashboard/src/containers/Routes.test.tsx +++ b/dashboard/src/containers/RoutesContainer/Routes.test.tsx @@ -1,13 +1,32 @@ import { mount } from "enzyme"; +import { createMemoryHistory } from "history"; import * as React from "react"; -import { Redirect, StaticRouter } from "react-router"; -import NotFound from "../components/NotFound"; +import { StaticRouter } from "react-router"; +import { Redirect, RouteComponentProps } from "react-router-dom"; + +import NotFound from "../../components/NotFound"; import Routes from "./Routes"; +const emptyRouteComponentProps: RouteComponentProps<{}> = { + history: createMemoryHistory(), + location: { + hash: "", + pathname: "", + search: "", + state: "", + }, + match: { + isExact: false, + params: {}, + path: "", + url: "", + }, +}; + it("invalid path should show a 404 error", () => { const wrapper = mount( - + , ); expect(wrapper.find(NotFound)).toExist(); @@ -17,7 +36,7 @@ it("invalid path should show a 404 error", () => { it("should render a redirect to the default page", () => { const wrapper = mount( - + , ); expect(wrapper.find(NotFound)).not.toExist(); diff --git a/dashboard/src/containers/RoutesContainer/Routes.tsx b/dashboard/src/containers/RoutesContainer/Routes.tsx new file mode 100644 index 00000000000..ea5e9f6054c --- /dev/null +++ b/dashboard/src/containers/RoutesContainer/Routes.tsx @@ -0,0 +1,82 @@ +import * as React from "react"; +import { Redirect, Route, RouteComponentProps, RouteProps, Switch } from "react-router"; + +import NotFound from "../../components/NotFound"; +import AppList from "../../containers/AppListContainer"; +import AppNew from "../../containers/AppNewContainer"; +import AppUpgrade from "../../containers/AppUpgradeContainer"; +import AppView from "../../containers/AppViewContainer"; +import ChartList from "../../containers/ChartListContainer"; +import ChartView from "../../containers/ChartViewContainer"; +import ClassListContainer from "../../containers/ClassListContainer"; +import { ClassViewContainer } from "../../containers/ClassView"; +import FunctionListContainer from "../../containers/FunctionListContainer"; +import FunctionViewContainer from "../../containers/FunctionViewContainer"; +import InstanceListViewContainer from "../../containers/InstanceListViewContainer"; +import InstanceView from "../../containers/InstanceView"; +import LoginFormContainer from "../../containers/LoginFormContainer"; +import PrivateRouteContainer from "../../containers/PrivateRouteContainer"; +import RepoListContainer from "../../containers/RepoListContainer"; +import ServiceCatalogContainer from "../../containers/ServiceCatalogContainer"; + +type IRouteComponentPropsAndRouteProps = RouteProps & RouteComponentProps; + +const privateRoutes: { + [route: string]: React.ComponentType> | React.ComponentType; +} = { + "/apps/ns/:namespace": AppList, + "/apps/ns/:namespace/:releaseName": AppView, + "/apps/ns/:namespace/new/:repo/:id/versions/:version": AppNew, + "/apps/ns/:namespace/upgrade/:releaseName": AppUpgrade, + "/charts": ChartList, + "/charts/:repo": ChartList, + "/charts/:repo/:id": ChartView, + "/charts/:repo/:id/versions/:version": ChartView, + "/config/brokers": ServiceCatalogContainer, + "/config/repos": RepoListContainer, + "/functions/ns/:namespace": FunctionListContainer, + "/functions/ns/:namespace/:name": FunctionViewContainer, + "/services/brokers/:brokerName/classes/:className": ClassViewContainer, + "/services/brokers/:brokerName/instances/ns/:namespace/:instanceName": InstanceView, + "/services/classes": ClassListContainer, + "/services/instances/ns/:namespace": InstanceListViewContainer, +}; + +// Public routes that don't require authentication +const routes: { + [route: string]: React.ComponentType> | React.ComponentType; +} = { + "/login": LoginFormContainer, +}; + +interface IRoutesProps extends IRouteComponentPropsAndRouteProps { + namespace: string; +} + +class Routes extends React.Component { + public render() { + return ( + + + {Object.keys(routes).map(route => ( + + ))} + {Object.keys(privateRoutes).map(route => ( + + ))} + {/* If the route doesn't match any expected path redirect to a 404 page */} + + + ); + } + private rootNamespacedRedirect = (props: any) => { + return ; + }; +} + +export default Routes; diff --git a/dashboard/src/containers/RoutesContainer/RoutesContainer.tsx b/dashboard/src/containers/RoutesContainer/RoutesContainer.tsx new file mode 100644 index 00000000000..575053aa5c3 --- /dev/null +++ b/dashboard/src/containers/RoutesContainer/RoutesContainer.tsx @@ -0,0 +1,11 @@ +import { connect } from "react-redux"; +import { withRouter } from "react-router"; + +import { IStoreState } from "../../shared/types"; +import Routes from "./Routes"; + +function mapStateToProps({ namespace }: IStoreState) { + return { namespace: namespace.current }; +} + +export default withRouter(connect(mapStateToProps)(Routes)); diff --git a/dashboard/src/containers/RoutesContainer/index.ts b/dashboard/src/containers/RoutesContainer/index.ts new file mode 100644 index 00000000000..40f5b1660be --- /dev/null +++ b/dashboard/src/containers/RoutesContainer/index.ts @@ -0,0 +1,3 @@ +import RoutesContainer from "./RoutesContainer"; + +export default RoutesContainer;