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

[RFR] Migrate ra-core to TypeScript #2508

Merged
merged 7 commits into from
Nov 8, 2018
Merged
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
1 change: 0 additions & 1 deletion examples/demo/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'babel-polyfill';
import React, { Component } from 'react';
import { Admin, Resource } from 'react-admin';

Expand Down
1 change: 0 additions & 1 deletion examples/graphcool-demo/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'babel-polyfill';
import React, { Component } from 'react';
import { Admin, Resource } from 'react-admin';

Expand Down
80 changes: 80 additions & 0 deletions packages/ra-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions packages/ra-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
"watch": "rimraf ./lib && tsc --watch"
},
"devDependencies": {
"@types/history": "^4.7.2",
"@types/node-polyglot": "^0.4.31",
"@types/react-router": "^4.4.1",
"@types/recompose": "^0.27.0",
"cross-env": "^5.2.0",
"enzyme": "~3.3.0",
"enzyme-adapter-react-16": "~1.1.1",
"enzyme": "~3.7.0",
"enzyme-adapter-react-16": "~1.6.0",
"ignore-styles": "~5.0.1",
"react": "~16.3.1",
"react-dom": "~16.3.1",
"react-test-renderer": "~16.3.1",
"react-testing-library": "^5.2.3",
"rimraf": "^2.6.2"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { createElement } from 'react';
import React, {
createElement,
Component,
ReactNode,
ComponentType,
} from 'react';
import PropTypes from 'prop-types';
import { Provider } from 'react-redux';
import createHistory from 'history/createHashHistory';
Expand All @@ -9,35 +14,33 @@ import withContext from 'recompose/withContext';
import createAdminStore from './createAdminStore';
import TranslationProvider from './i18n/TranslationProvider';
import CoreAdminRouter from './CoreAdminRouter';
import { AuthProvider, I18nProvider, DataProvider } from './types';

const componentPropType = PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]);
export type ChildrenFunction = () => ComponentType[];

class CoreAdmin extends React.Component {
static propTypes = {
appLayout: componentPropType,
authProvider: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
catchAll: componentPropType,
customSagas: PropTypes.array,
customReducers: PropTypes.object,
customRoutes: PropTypes.array,
dashboard: componentPropType,
dataProvider: PropTypes.func,
history: PropTypes.object,
i18nProvider: PropTypes.func,
initialState: PropTypes.object,
loading: componentPropType,
locale: PropTypes.string,
loginPage: componentPropType,
logoutButton: componentPropType,
menu: componentPropType,
theme: PropTypes.object,
title: PropTypes.node,
};
interface Props {
appLayout: ComponentType;
authProvider: AuthProvider;
children: ReactNode | ChildrenFunction;
catchAll: ComponentType;
customSagas: any[];
customReducers: object;
customRoutes: any[];
dashboard: ComponentType;
dataProvider: DataProvider;
history: object;
i18nProvider: I18nProvider;
initialState: object;
loading: ComponentType;
locale: string;
loginPage: ComponentType;
logoutButton: ComponentType;
menu: ComponentType;
theme: object;
title: ReactNode;
}

class CoreAdmin extends Component<Props> {
static contextTypes = {
store: PropTypes.object,
};
Expand Down Expand Up @@ -123,12 +126,28 @@ React-admin requires a valid dataProvider function to work.`);
}

render() {
const {
authProvider,
customReducers,
customSagas,
dataProvider,
i18nProvider,
initialState,
locale,
} = this.props;

return this.reduxIsAlreadyInitialized ? (
this.renderCore()
) : (
<Provider
store={createAdminStore({
...this.props,
authProvider,
customReducers,
customSagas,
dataProvider,
i18nProvider,
initialState,
locale,
history: this.history,
})}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import React, { Component, ReactElement } from 'react';
import { connect } from 'react-redux';

import { userCheck } from '../actions/authActions';
import { userCheck as userCheckAction } from '../actions/authActions';
import { UserCheck } from './types';

interface Props {
authParams: object;
children: ReactElement<any>;
location: object;
userCheck: UserCheck;
}

/**
* Restrict access to children to authenticated users
Expand Down Expand Up @@ -31,14 +38,7 @@ import { userCheck } from '../actions/authActions';
* </Admin>
* );
*/
export class Authenticated extends Component {
static propTypes = {
authParams: PropTypes.object,
children: PropTypes.element.isRequired,
location: PropTypes.object,
userCheck: PropTypes.func,
};

export class Authenticated extends Component<Props> {
componentWillMount() {
this.checkAuthentication(this.props);
}
Expand Down Expand Up @@ -69,5 +69,5 @@ export class Authenticated extends Component {

export default connect(
null,
{ userCheck }
{ userCheck: userCheckAction }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you rename the action creator in the import? It's cumbersome.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because TypeScript otherwise complain about shadowed variable names when we get userCheck from the props through destructuring

)(Authenticated);
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
import { Children, Component } from 'react';
import { Children, Component, ReactNode } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import getContext from 'recompose/getContext';

import { userCheck } from '../actions/authActions';
import { AUTH_GET_PERMISSIONS } from '../auth/types';
import { isLoggedIn } from '../reducer';
import { userCheck as userCheckAction } from '../actions/authActions';
import { AUTH_GET_PERMISSIONS } from './types';
import { isLoggedIn as getIsLoggedIn } from '../reducer';
import warning from '../util/warning';
import { AuthProvider } from '../types';
import { UserCheck } from './types';
import { Location } from 'history';
import { match as Match } from 'react-router';

interface WithPermissionsChildrenParams {
authParams: object;
location?: Location;
match: Match;
permissions: any;
}

type WithPermissionsChildren = (
params: WithPermissionsChildrenParams
) => ReactNode;

interface Props {
authProvider: AuthProvider;
authParams: object;
children: WithPermissionsChildren;
location: Location;
match: Match;
render: WithPermissionsChildren;
isLoggedIn: boolean;
staticContext: object;
userCheck: UserCheck;
}

const isEmptyChildren = children => Children.count(children) === 0;

Expand Down Expand Up @@ -45,19 +72,7 @@ const isEmptyChildren = children => Children.count(children) === 0;
* </Admin>
* );
*/
export class WithPermissions extends Component {
static propTypes = {
authProvider: PropTypes.func,
authParams: PropTypes.object,
children: PropTypes.func,
location: PropTypes.object,
match: PropTypes.object,
render: PropTypes.func,
isLoggedIn: PropTypes.bool,
staticContext: PropTypes.object,
userCheck: PropTypes.func,
};

export class WithPermissions extends Component<Props> {
cancelled = false;

state = { permissions: null };
Expand Down Expand Up @@ -91,12 +106,12 @@ export class WithPermissions extends Component {
}
}

checkAuthentication(params) {
checkAuthentication(params: Props) {
const { userCheck, authParams, location } = params;
userCheck(authParams, location && location.pathname);
}

async checkPermissions(params) {
async checkPermissions(params: Props) {
const { authProvider, authParams, location, match } = params;
try {
const permissions = await authProvider(AUTH_GET_PERMISSIONS, {
Expand Down Expand Up @@ -139,7 +154,7 @@ export class WithPermissions extends Component {
}
}
const mapStateToProps = state => ({
isLoggedIn: isLoggedIn(state),
isLoggedIn: getIsLoggedIn(state),
});

export default compose(
Expand All @@ -148,6 +163,6 @@ export default compose(
}),
connect(
mapStateToProps,
{ userCheck }
{ userCheck: userCheckAction }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark as above

)
)(WithPermissions);
File renamed without changes.
5 changes: 0 additions & 5 deletions packages/ra-core/src/auth/types.js

This file was deleted.

13 changes: 13 additions & 0 deletions packages/ra-core/src/auth/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AuthActionType } from '../types';

export type UserCheck = (
payload: object,
pathName: string,
routeParams?: object
) => void;

export const AUTH_LOGIN: AuthActionType = 'AUTH_LOGIN';
export const AUTH_CHECK: AuthActionType = 'AUTH_CHECK';
export const AUTH_ERROR: AuthActionType = 'AUTH_ERROR';
export const AUTH_LOGOUT: AuthActionType = 'AUTH_LOGOUT';
export const AUTH_GET_PERMISSIONS: AuthActionType = 'AUTH_GET_PERMISSIONS';
Loading