From 62a465c3e954105b359a0046dc0bf893ccaa324a Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Fri, 5 Oct 2018 09:57:16 +0200 Subject: [PATCH] rewrite app utils in ts (#773) --- .../utils/{getHistory.js => getHistory.ts} | 16 ++- ...ion.js => handleRequiredAuthentication.ts} | 7 +- ...tion.js => handleRequiredAuthorization.ts} | 7 +- .../{startReactApp.js => startReactApp.tsx} | 131 ++++++++++++------ 4 files changed, 100 insertions(+), 61 deletions(-) rename server/sonar-web/src/main/js/app/utils/{getHistory.js => getHistory.ts} (74%) rename server/sonar-web/src/main/js/app/utils/{handleRequiredAuthentication.js => handleRequiredAuthentication.ts} (89%) rename server/sonar-web/src/main/js/app/utils/{handleRequiredAuthorization.js => handleRequiredAuthorization.ts} (90%) rename server/sonar-web/src/main/js/app/utils/{startReactApp.js => startReactApp.tsx} (69%) diff --git a/server/sonar-web/src/main/js/app/utils/getHistory.js b/server/sonar-web/src/main/js/app/utils/getHistory.ts similarity index 74% rename from server/sonar-web/src/main/js/app/utils/getHistory.js rename to server/sonar-web/src/main/js/app/utils/getHistory.ts index de272923840..e23f7c5f756 100644 --- a/server/sonar-web/src/main/js/app/utils/getHistory.js +++ b/server/sonar-web/src/main/js/app/utils/getHistory.ts @@ -17,17 +17,19 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow import { useRouterHistory } from 'react-router'; -import { createHistory } from 'history'; +import { createHistory, History } from 'history'; -let history; +let history: History; -const ensureHistory = () => { +function ensureHistory() { history = useRouterHistory(createHistory)({ - basename: window.baseUrl + '/' + // do not use `getBaseUrl` from `helpers/urls` to no import this file with all its dependecies + basename: (window as any).baseUrl + '/' }); return history; -}; +} -export default () => (history ? history : ensureHistory()); +export default function getHistory() { + return history ? history : ensureHistory(); +} diff --git a/server/sonar-web/src/main/js/app/utils/handleRequiredAuthentication.js b/server/sonar-web/src/main/js/app/utils/handleRequiredAuthentication.ts similarity index 89% rename from server/sonar-web/src/main/js/app/utils/handleRequiredAuthentication.js rename to server/sonar-web/src/main/js/app/utils/handleRequiredAuthentication.ts index bc3a3b95160..aaf5732e0ba 100644 --- a/server/sonar-web/src/main/js/app/utils/handleRequiredAuthentication.js +++ b/server/sonar-web/src/main/js/app/utils/handleRequiredAuthentication.ts @@ -17,14 +17,13 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow import getHistory from './getHistory'; -export default () => { +export default function handleRequiredAuthentication() { const history = getHistory(); const returnTo = window.location.pathname + window.location.search + window.location.hash; history.replace({ pathname: '/sessions/new', - query: { return_to: returnTo } + query: { return_to: returnTo } // eslint-disable-line camelcase }); -}; +} diff --git a/server/sonar-web/src/main/js/app/utils/handleRequiredAuthorization.js b/server/sonar-web/src/main/js/app/utils/handleRequiredAuthorization.ts similarity index 90% rename from server/sonar-web/src/main/js/app/utils/handleRequiredAuthorization.js rename to server/sonar-web/src/main/js/app/utils/handleRequiredAuthorization.ts index eaa2cf81b75..a3a32b38ba6 100644 --- a/server/sonar-web/src/main/js/app/utils/handleRequiredAuthorization.js +++ b/server/sonar-web/src/main/js/app/utils/handleRequiredAuthorization.ts @@ -17,12 +17,11 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow import getStore from './getStore'; import getHistory from './getHistory'; import { requireAuthorization } from '../../store/appState'; -export default () => { +export default function handleRequiredAuthorization() { const store = getStore(); const history = getHistory(); @@ -31,6 +30,6 @@ export default () => { store.dispatch(requireAuthorization()); history.replace({ pathname: '/sessions/new', - query: { return_to: returnTo } + query: { return_to: returnTo } // eslint-disable-line camelcase }); -}; +} diff --git a/server/sonar-web/src/main/js/app/utils/startReactApp.js b/server/sonar-web/src/main/js/app/utils/startReactApp.tsx similarity index 69% rename from server/sonar-web/src/main/js/app/utils/startReactApp.js rename to server/sonar-web/src/main/js/app/utils/startReactApp.tsx index 474e9f56083..78bd52c4c53 100644 --- a/server/sonar-web/src/main/js/app/utils/startReactApp.js +++ b/server/sonar-web/src/main/js/app/utils/startReactApp.tsx @@ -18,11 +18,12 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* eslint-disable react/jsx-sort-props */ -import React from 'react'; +import * as React from 'react'; import { render } from 'react-dom'; -import { Router, Route, IndexRoute, Redirect } from 'react-router'; +import { Router, Route, IndexRoute, Redirect, RouteProps, RouteConfig } from 'react-router'; import { Provider } from 'react-redux'; import { IntlProvider } from 'react-intl'; +import { Location } from 'history'; import getStore from './getStore'; import getHistory from './getHistory'; import MigrationContainer from '../components/MigrationContainer'; @@ -68,8 +69,9 @@ import { maintenanceRoutes, setupRoutes } from '../../apps/maintenance/routes'; import { globalPermissionsRoutes, projectPermissionsRoutes } from '../../apps/permissions/routes'; import { lazyLoad } from '../../components/lazyLoad'; import { isSonarCloud } from '../../helpers/system'; +import { CurrentUser, AppState } from '../types'; -function handleUpdate() { +function handleUpdate(this: { state: { location: Location } }) { const { action } = this.state.location; if (action === 'PUSH') { @@ -77,7 +79,16 @@ function handleUpdate() { } } -const startReactApp = (lang, currentUser, appState) => { +// this is not an official api +const RouteWithChildRoutes = Route as React.ComponentClass< + RouteProps & { childRoutes: RouteConfig } +>; + +export default function startReactApp( + lang: string, + currentUser?: CurrentUser, + appState?: AppState +) { const el = document.getElementById('content'); const history = getHistory(); @@ -96,7 +107,7 @@ const startReactApp = (lang, currentUser, appState) => { { + onEnter={(_, replace) => { replace('/coding_rules' + window.location.hash); }} /> @@ -110,7 +121,7 @@ const startReactApp = (lang, currentUser, appState) => { { + onEnter={(_, replace) => { replace('/issues' + window.location.hash); }} /> @@ -150,18 +161,18 @@ const startReactApp = (lang, currentUser, appState) => { import('../components/SimpleSessionsContainer'))}> - + import('../components/Landing'))} /> - + - - - - + + + + @@ -171,7 +182,7 @@ const startReactApp = (lang, currentUser, appState) => { component={lazyLoad(() => import('../components/extensions/GlobalPageExtension'))} /> - + {isSonarCloud() && ( { )} /> )} - - - + + + import('../components/extensions/PortfoliosPage'))} /> - - + + import('../components/ComponentContainer'))}> - - - - - + + + + + @@ -207,24 +224,39 @@ const startReactApp = (lang, currentUser, appState) => { path="project/security_reports/:type" component={lazyLoad(() => import('../../apps/securityReports/components/App'))} /> - - + import('../components/ProjectAdminContainer'))}> - + import('../components/extensions/ProjectAdminPageExtension') )} /> - - - - - + + + + + import('../../apps/projectDeletion/App'))} @@ -249,18 +281,27 @@ const startReactApp = (lang, currentUser, appState) => { import('../components/extensions/GlobalAdminPageExtension') )} /> - - - - - - - - - - - - + + + + + + + + + + + + { , el ); -}; - -export default startReactApp; +} -- 2.39.5