diff --git a/frontend/common/stores/account-store.js b/frontend/common/stores/account-store.js index 23b5c603bfed..c9d86e965f79 100644 --- a/frontend/common/stores/account-store.js +++ b/frontend/common/stores/account-store.js @@ -302,14 +302,12 @@ const controller = { store.model = user if (user && user.organisations) { store.organisation = user.organisations[0] - const cookiedID = parseInt(API.getCookie('organisation')) - const pathID = parseInt( - matchPath(document.location.pathname, { - path: '/organisation/:organisationId', - strict: false, - })?.params?.organisationId, - ) - const orgId = pathID || cookiedID + const cookiedID = API.getCookie('organisation') + const pathID = matchPath(document.location.pathname, { + path: '/organisation/:organisationId', + strict: false, + })?.params?.organisationId + const orgId = parseInt(pathID || cookiedID) || undefined if (orgId) { const foundOrganisation = user.organisations.find( (v) => `${v.id}` === orgId, diff --git a/frontend/common/stores/project-store.js b/frontend/common/stores/project-store.js index 0fe419143906..10bea02bd1bd 100644 --- a/frontend/common/stores/project-store.js +++ b/frontend/common/stores/project-store.js @@ -96,7 +96,12 @@ const controller = { }) }, getProject: (id, cb, force) => { - if (force) { + if (!id) { + if (!getIsWidget()) { + !force && AsyncStorage.removeItem('lastEnv') + document.location.href = '/404' + } + } else if (force) { store.loading() return Promise.all([ diff --git a/frontend/web/components/ProjectsPage.tsx b/frontend/web/components/ProjectsPage.tsx index 7f36d12613f9..8bbaeb703a4e 100644 --- a/frontend/web/components/ProjectsPage.tsx +++ b/frontend/web/components/ProjectsPage.tsx @@ -16,9 +16,7 @@ const ProjectsPage: FC = ({ match }) => { {() => { return (
- +
) }} diff --git a/frontend/web/components/base/higher-order/ParameterizedRoute.tsx b/frontend/web/components/base/higher-order/ParameterizedRoute.tsx new file mode 100644 index 000000000000..c1ca292e5e8d --- /dev/null +++ b/frontend/web/components/base/higher-order/ParameterizedRoute.tsx @@ -0,0 +1,49 @@ +import NotFoundPage from 'components/pages/NotFoundPage' +import React from 'react' +import { RouteComponentProps, Route } from 'react-router-dom' + +type ParameterizedRouteType = { + component: React.ComponentType + [key: string]: any +} + +export const ParameterizedRoute = ({ + component: Component, + ...props +}: ParameterizedRouteType) => { + const { organisationId, projectId } = props.computedMatch.params + + const parsedOrganisationId = organisationId && parseInt(organisationId) + const parsedProjectId = projectId && parseInt(projectId) + + // Handle the case where the parameters are invalid + if ( + (projectId && isNaN(parseInt(projectId))) || + (organisationId && isNaN(parseInt(organisationId))) + ) { + return + } + + if (!projectId && !organisationId) { + return + } + + return ( + ( + + )} + /> + ) +} diff --git a/frontend/web/routes.js b/frontend/web/routes.js index 2c1fbf473380..1370fd8a529b 100644 --- a/frontend/web/routes.js +++ b/frontend/web/routes.js @@ -38,6 +38,7 @@ import OrganisationsPage from './components/pages/OrganisationsPage' import UsersAndPermissionsPage from './components/pages/UsersAndPermissionsPage' import ProjectRedirectPage from './components/pages/ProjectRedirectPage' import SDKKeysPage from './components/SDKKeysPage' +import { ParameterizedRoute } from './components/base/higher-order/ParameterizedRoute' export default ( @@ -56,27 +57,27 @@ export default ( exact component={PasswordResetPage} /> - - - - - - - - - - - - - - - + - - - - - - - - - + - - -