Skip to content

Commit f6f5bf6

Browse files
committed
Convert remaining identifier javascript code to typescript
Add types for relevant annotations with packages.
1 parent 73a4ec1 commit f6f5bf6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1308
-1219
lines changed

identifier/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"axios": "^0.22.0",
1111
"classnames": "^2.3.2",
1212
"glob": "^8.1.0",
13+
"history": "^5.3.0",
1314
"i18next": "^21.10.0",
14-
"i18next-browser-languagedetector": "^6.1.8",
15+
"i18next-browser-languagedetector": "^8.0.2",
1516
"i18next-http-backend": "^1.4.5",
1617
"i18next-resources-to-backend": "^1.0.0",
1718
"query-string": "^7.1.3",
@@ -47,6 +48,7 @@
4748
"@types/react": "^17.0.70",
4849
"@types/react-dom": "^17.0.23",
4950
"@types/react-redux": "^7.1.25",
51+
"@types/react-router-dom": "^5.3.3",
5052
"@types/redux-logger": "^3.0.12",
5153
"@types/validator": "^13",
5254
"@typescript-eslint/eslint-plugin": "^6.11.0",
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@ import React, { Suspense, lazy } from 'react';
33
import { MuiThemeProvider } from '@material-ui/core/styles';
44
import {
55
CssBaseline,
6-
} from '@material-ui/core';
6+
} from '@material-ui/core';
77

88
import './App.css';
99
import './fancy-background.css';
1010
import Spinner from './components/Spinner';
1111
import * as version from './version';
1212
import theme from './theme';
1313

14-
const LazyMain = lazy(() => import(/* webpackChunkName: "identifier-main" */ './Main'));
14+
const LazyMain = lazy(() => import(/* webpackChunkName: "identifier-main" */ './Main') as Promise<{ default: React.ComponentType<object> }>);
1515

1616
console.info(`Kopano Identifier build version: ${version.build}`); // eslint-disable-line no-console
1717

18+
1819
const App = () => {
1920
return (
2021
<MuiThemeProvider theme={theme}>
21-
<CssBaseline/>
22-
<Suspense fallback={<Spinner/>}>
22+
<CssBaseline />
23+
<Suspense fallback={<Spinner />}>
2324
<LazyMain />
2425
</Suspense>
2526
</MuiThemeProvider>
26-
);
27+
)
2728
}
2829

2930
export default App;

identifier/src/Main.jsx

Lines changed: 0 additions & 57 deletions
This file was deleted.

identifier/src/Main.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import { connect } from 'react-redux';
3+
4+
import { BrowserRouter } from 'react-router-dom';
5+
6+
import { createStyles, WithStyles, withStyles } from '@material-ui/core/styles';
7+
8+
import Routes from './Routes';
9+
import { RootState } from './store';
10+
import { ObjectType } from './types';
11+
12+
const styles = () => createStyles({
13+
root: {
14+
position: 'relative',
15+
display: 'flex',
16+
flex: 1
17+
}
18+
});
19+
20+
export interface MainProps extends WithStyles<typeof styles> {
21+
hello: ObjectType;
22+
pathPrefix: string;
23+
updateAvailable: boolean;
24+
}
25+
26+
27+
const Main: React.FC<MainProps> = ({ classes, hello, pathPrefix }) => {
28+
29+
return (
30+
<div className={classes.root}>
31+
<BrowserRouter basename={pathPrefix}>
32+
<Routes hello={hello} />
33+
</BrowserRouter>
34+
</div>
35+
);
36+
}
37+
38+
const mapStateToProps = (state: RootState) => {
39+
const { hello, updateAvailable, pathPrefix } = state.common;
40+
41+
return {
42+
hello: hello as ObjectType,
43+
updateAvailable,
44+
pathPrefix
45+
};
46+
};
47+
48+
export default connect(mapStateToProps)(withStyles(styles)(Main));
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import React, { lazy } from 'react';
2-
import PropTypes from 'prop-types';
32

43
import { Route, Switch } from 'react-router-dom';
54

65
import PrivateRoute from './components/PrivateRoute';
6+
import { ObjectType } from './types';
7+
8+
type RootType = {
9+
hello: ObjectType;
10+
}
11+
12+
13+
type ComponentType = Promise<{ default: React.ComponentType<object> }>
714

815
const AsyncLogin = lazy(() =>
9-
import(/* webpackChunkName: "containers-login" */ './containers/Login'));
16+
import(/* webpackChunkName: "containers-login" */ './containers/Login') as ComponentType);
1017
const AsyncWelcome = lazy(() =>
11-
import(/* webpackChunkName: "containers-welcome" */ './containers/Welcome'));
18+
import(/* webpackChunkName: "containers-welcome" */ './containers/Welcome') as ComponentType);
1219
const AsyncGoodbye = lazy(() =>
13-
import(/* webpackChunkName: "containers-goodbye" */ './containers/Goodbye'));
20+
import(/* webpackChunkName: "containers-goodbye" */ './containers/Goodbye') as ComponentType);
1421

15-
const Routes = ({ hello }) => (
22+
const Routes = ({ hello }: RootType) => (
1623
<Switch>
1724
<PrivateRoute
1825
path="/welcome"
@@ -32,8 +39,4 @@ const Routes = ({ hello }) => (
3239
</Switch>
3340
);
3441

35-
Routes.propTypes = {
36-
hello: PropTypes.object
37-
};
38-
3942
export default Routes;
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import axios from 'axios';
1+
import axios, { AxiosError, AxiosResponse } from 'axios';
22

3-
import { newHelloRequest } from '../models/hello';
3+
import { HelloQuery, newHelloRequest } from '../models/hello';
44
import { withClientRequestState } from '../utils';
55
import {
66
ExtendedError,
@@ -10,8 +10,11 @@ import {
1010

1111
import { handleAxiosError } from './utils';
1212
import * as types from './types';
13+
import { Dispatch } from 'redux';
14+
import { AppDispatch, PromiseDispatch, RootState } from '../store';
15+
import { ResponseType } from '../types';
1316

14-
export function receiveError(error) {
17+
export function receiveError(error?: ExtendedError | AxiosError<never> | null) {
1518
return {
1619
type: types.RECEIVE_ERROR,
1720
error
@@ -24,7 +27,7 @@ export function resetHello() {
2427
};
2528
}
2629

27-
export function receiveHello(hello) {
30+
export function receiveHello(hello: {success?: boolean, username?: string, displayName?: string}) {
2831
const { success, username, displayName } = hello;
2932

3033
return {
@@ -37,12 +40,12 @@ export function receiveHello(hello) {
3740
}
3841

3942
export function executeHello() {
40-
return function(dispatch, getState) {
43+
return function(dispatch:Dispatch , getState: () => RootState) {
4144
dispatch(resetHello());
4245

4346
const { flow, query } = getState().common;
4447

45-
const r = withClientRequestState(newHelloRequest(flow, query));
48+
const r = withClientRequestState(newHelloRequest(flow as string, query as HelloQuery));
4649
return axios.post('./identifier/_/hello', r, {
4750
headers: {
4851
'Kopano-Konnect-XSRF': '1'
@@ -60,11 +63,11 @@ export function executeHello() {
6063
};
6164
default:
6265
// error.
63-
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response);
66+
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response as AxiosResponse<never>);
6467
}
6568
}).then(response => {
6669
if (response.state !== r.state) {
67-
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response);
70+
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response as ResponseType);
6871
}
6972

7073
dispatch(receiveHello(response));
@@ -78,7 +81,7 @@ export function executeHello() {
7881
}
7982

8083
export function retryHello() {
81-
return function(dispatch) {
84+
return function(dispatch: PromiseDispatch) {
8285
dispatch(receiveError(null));
8386

8487
return dispatch(executeHello());
@@ -91,15 +94,15 @@ export function requestLogoff() {
9194
};
9295
}
9396

94-
export function receiveLogoff(state) {
97+
export function receiveLogoff(state: boolean) {
9598
return {
9699
type: types.RECEIVE_LOGOFF,
97100
state
98101
};
99102
}
100103

101104
export function executeLogoff() {
102-
return function(dispatch) {
105+
return function(dispatch: AppDispatch) {
103106
dispatch(resetHello());
104107
dispatch(requestLogoff());
105108

@@ -115,11 +118,11 @@ export function executeLogoff() {
115118
return response.data;
116119
default:
117120
// error.
118-
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response);
121+
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response as AxiosResponse<never>);
119122
}
120123
}).then(response => {
121124
if (response.state !== r.state) {
122-
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response);
125+
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response as ResponseType);
123126
}
124127

125128
dispatch(receiveLogoff(response.success === true));

0 commit comments

Comments
 (0)