Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert remaining identifier javascript code to typescript #149

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion identifier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"axios": "^0.22.0",
"classnames": "^2.3.2",
"glob": "^8.1.0",
"history": "^5.3.0",
"i18next": "^21.10.0",
"i18next-browser-languagedetector": "^6.1.8",
"i18next-browser-languagedetector": "^8.0.2",
"i18next-http-backend": "^1.4.5",
"i18next-resources-to-backend": "^1.0.0",
"query-string": "^7.1.3",
Expand Down Expand Up @@ -47,6 +48,7 @@
"@types/react": "^17.0.70",
"@types/react-dom": "^17.0.23",
"@types/react-redux": "^7.1.25",
"@types/react-router-dom": "^5.3.3",
"@types/redux-logger": "^3.0.12",
"@types/validator": "^13",
"@typescript-eslint/eslint-plugin": "^6.11.0",
Expand Down
11 changes: 6 additions & 5 deletions identifier/src/App.jsx → identifier/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ import React, { Suspense, lazy } from 'react';
import { MuiThemeProvider } from '@material-ui/core/styles';
import {
CssBaseline,
} from '@material-ui/core';
} from '@material-ui/core';

import './App.css';
import './fancy-background.css';
import Spinner from './components/Spinner';
import * as version from './version';
import theme from './theme';

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

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


const App = () => {
return (
<MuiThemeProvider theme={theme}>
<CssBaseline/>
<Suspense fallback={<Spinner/>}>
<CssBaseline />
<Suspense fallback={<Spinner />}>
<LazyMain />
</Suspense>
</MuiThemeProvider>
);
)
}

export default App;
57 changes: 0 additions & 57 deletions identifier/src/Main.jsx

This file was deleted.

File renamed without changes.
48 changes: 48 additions & 0 deletions identifier/src/Main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { connect } from 'react-redux';

import { BrowserRouter } from 'react-router-dom';

import { createStyles, WithStyles, withStyles } from '@material-ui/core/styles';

import Routes from './Routes';
import { RootState } from './store';
import { ObjectType } from './types';

const styles = () => createStyles({
root: {
position: 'relative',
display: 'flex',
flex: 1
}
});

export interface MainProps extends WithStyles<typeof styles> {
hello: ObjectType;
pathPrefix: string;
updateAvailable: boolean;
}


const Main: React.FC<MainProps> = ({ classes, hello, pathPrefix }) => {

return (
<div className={classes.root}>
<BrowserRouter basename={pathPrefix}>
<Routes hello={hello} />
</BrowserRouter>
</div>
);
}

const mapStateToProps = (state: RootState) => {
const { hello, updateAvailable, pathPrefix } = state.common;

return {
hello: hello as ObjectType,
updateAvailable,
pathPrefix
};
};

export default connect(mapStateToProps)(withStyles(styles)(Main));
21 changes: 12 additions & 9 deletions identifier/src/Routes.jsx → identifier/src/Routes.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import React, { lazy } from 'react';
import PropTypes from 'prop-types';

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

import PrivateRoute from './components/PrivateRoute';
import { ObjectType } from './types';

type RootType = {
hello: ObjectType;
}


type ComponentType = Promise<{ default: React.ComponentType<object> }>

const AsyncLogin = lazy(() =>
import(/* webpackChunkName: "containers-login" */ './containers/Login'));
import(/* webpackChunkName: "containers-login" */ './containers/Login') as ComponentType);
const AsyncWelcome = lazy(() =>
import(/* webpackChunkName: "containers-welcome" */ './containers/Welcome'));
import(/* webpackChunkName: "containers-welcome" */ './containers/Welcome') as ComponentType);
const AsyncGoodbye = lazy(() =>
import(/* webpackChunkName: "containers-goodbye" */ './containers/Goodbye'));
import(/* webpackChunkName: "containers-goodbye" */ './containers/Goodbye') as ComponentType);

const Routes = ({ hello }) => (
const Routes = ({ hello }: RootType) => (
<Switch>
<PrivateRoute
path="/welcome"
Expand All @@ -32,8 +39,4 @@ const Routes = ({ hello }) => (
</Switch>
);

Routes.propTypes = {
hello: PropTypes.object
};

export default Routes;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import axios, { AxiosError, AxiosResponse } from 'axios';

import { newHelloRequest } from '../models/hello';
import { HelloQuery, newHelloRequest } from '../models/hello';
import { withClientRequestState } from '../utils';
import {
ExtendedError,
Expand All @@ -10,8 +10,11 @@ import {

import { handleAxiosError } from './utils';
import * as types from './types';
import { Dispatch } from 'redux';
import { AppDispatch, PromiseDispatch, RootState } from '../store';
import { ResponseType } from '../types';

export function receiveError(error) {
export function receiveError(error?: ExtendedError | AxiosError<never> | null) {
return {
type: types.RECEIVE_ERROR,
error
Expand All @@ -24,7 +27,7 @@ export function resetHello() {
};
}

export function receiveHello(hello) {
export function receiveHello(hello: {success?: boolean, username?: string, displayName?: string}) {
const { success, username, displayName } = hello;

return {
Expand All @@ -37,12 +40,12 @@ export function receiveHello(hello) {
}

export function executeHello() {
return function(dispatch, getState) {
return function(dispatch:Dispatch , getState: () => RootState) {
dispatch(resetHello());

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

const r = withClientRequestState(newHelloRequest(flow, query));
const r = withClientRequestState(newHelloRequest(flow as string, query as HelloQuery));
return axios.post('./identifier/_/hello', r, {
headers: {
'Kopano-Konnect-XSRF': '1'
Expand All @@ -60,11 +63,11 @@ export function executeHello() {
};
default:
// error.
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response);
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response as AxiosResponse<never>);
}
}).then(response => {
if (response.state !== r.state) {
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response);
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response as ResponseType);
}

dispatch(receiveHello(response));
Expand All @@ -78,7 +81,7 @@ export function executeHello() {
}

export function retryHello() {
return function(dispatch) {
return function(dispatch: PromiseDispatch) {
dispatch(receiveError(null));

return dispatch(executeHello());
Expand All @@ -91,15 +94,15 @@ export function requestLogoff() {
};
}

export function receiveLogoff(state) {
export function receiveLogoff(state: boolean) {
return {
type: types.RECEIVE_LOGOFF,
state
};
}

export function executeLogoff() {
return function(dispatch) {
return function(dispatch: AppDispatch) {
dispatch(resetHello());
dispatch(requestLogoff());

Expand All @@ -115,11 +118,11 @@ export function executeLogoff() {
return response.data;
default:
// error.
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response);
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, response as AxiosResponse<never>);
}
}).then(response => {
if (response.state !== r.state) {
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response);
throw new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATE, response as ResponseType);
}

dispatch(receiveLogoff(response.success === true));
Expand Down
Loading
Loading