-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Changes from 6 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import React, { Component, ReactElement } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import isEqual from 'lodash/isEqual'; | ||
|
||
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 | ||
|
@@ -31,20 +39,13 @@ 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); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (nextProps.location !== this.props.location) { | ||
if (!isEqual(nextProps.location, this.props.location)) { | ||
this.checkAuthentication(nextProps); | ||
} | ||
} | ||
|
@@ -69,5 +70,5 @@ export class Authenticated extends Component { | |
|
||
export default connect( | ||
null, | ||
{ userCheck } | ||
{ userCheck: userCheckAction } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you rename the action creator in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because TypeScript otherwise complain about shadowed variable names when we get |
||
)(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; | ||
|
||
|
@@ -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 }; | ||
|
@@ -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, { | ||
|
@@ -139,7 +154,7 @@ export class WithPermissions extends Component { | |
} | ||
} | ||
const mapStateToProps = state => ({ | ||
isLoggedIn: isLoggedIn(state), | ||
isLoggedIn: getIsLoggedIn(state), | ||
}); | ||
|
||
export default compose( | ||
|
@@ -148,6 +163,6 @@ export default compose( | |
}), | ||
connect( | ||
mapStateToProps, | ||
{ userCheck } | ||
{ userCheck: userCheckAction } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same remark as above |
||
) | ||
)(WithPermissions); |
This file was deleted.
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React-router's
location
object is never mutated, better rely on strict equality.