-
-
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
Migrate ra-ui-materialui to TypeScript (Part 1: auth) #2984
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,4 @@ export { | |
} from './reducer/admin/references/oneToMany'; | ||
|
||
export * from './sideEffect'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import React, { Component } from 'react'; | ||
import React, { | ||
Component, | ||
ReactElement, | ||
ComponentType, | ||
HtmlHTMLAttributes, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
import Card from '@material-ui/core/Card'; | ||
|
@@ -8,49 +13,48 @@ import { | |
createMuiTheme, | ||
withStyles, | ||
createStyles, | ||
WithStyles, | ||
Theme, | ||
} from '@material-ui/core/styles'; | ||
import LockIcon from '@material-ui/icons/Lock'; | ||
import { StaticContext } from 'react-router'; | ||
|
||
import defaultTheme from '../defaultTheme'; | ||
import Notification from '../layout/Notification'; | ||
import DefaultLoginForm from './LoginForm'; | ||
|
||
const styles = theme => createStyles({ | ||
main: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
minHeight: '100vh', | ||
height: '1px', | ||
alignItems: 'center', | ||
justifyContent: 'flex-start', | ||
backgroundRepeat: 'no-repeat', | ||
backgroundSize: 'cover', | ||
}, | ||
card: { | ||
minWidth: 300, | ||
marginTop: '6em', | ||
}, | ||
avatar: { | ||
margin: '1em', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
}, | ||
icon: { | ||
backgroundColor: theme.palette.secondary[500], | ||
}, | ||
}); | ||
interface Props { | ||
backgroundImage?: string; | ||
loginForm: ReactElement<any>; | ||
theme: object; | ||
staticContext: StaticContext; | ||
} | ||
|
||
const sanitizeRestProps = ({ | ||
array, | ||
backgroundImage, | ||
classes, | ||
className, | ||
location, | ||
staticContext, | ||
theme, | ||
title, | ||
...rest | ||
}) => rest; | ||
const styles = (theme: Theme) => | ||
createStyles({ | ||
main: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
minHeight: '100vh', | ||
height: '1px', | ||
alignItems: 'center', | ||
justifyContent: 'flex-start', | ||
backgroundRepeat: 'no-repeat', | ||
backgroundSize: 'cover', | ||
}, | ||
card: { | ||
minWidth: 300, | ||
marginTop: '6em', | ||
}, | ||
avatar: { | ||
margin: '1em', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
}, | ||
icon: { | ||
backgroundColor: theme.palette.secondary[500], | ||
}, | ||
}); | ||
|
||
/** | ||
* A standalone login page, to serve as authentication gate to the admin | ||
|
@@ -70,31 +74,31 @@ const sanitizeRestProps = ({ | |
* </Admin> | ||
* ); | ||
*/ | ||
class Login extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.theme = createMuiTheme(props.theme); | ||
this.containerRef = React.createRef(); | ||
this.backgroundImageLoaded = false; | ||
} | ||
class Login extends Component< | ||
Props & WithStyles<typeof styles> & HtmlHTMLAttributes<HTMLDivElement> | ||
> { | ||
static propTypes = { | ||
backgroundImage: PropTypes.string, | ||
loginForm: PropTypes.element, | ||
theme: PropTypes.object, | ||
}; | ||
|
||
static defaultProps = { | ||
backgroundImage: 'https://source.unsplash.com/random/1600x900/daily', | ||
theme: defaultTheme, | ||
loginForm: <DefaultLoginForm />, | ||
}; | ||
|
||
// Even though the React doc ensure the ref creation is done before the | ||
// componentDidMount, it can happen that the ref is set to null until the | ||
// next render. | ||
// So, to handle this case the component will now try to load the image on | ||
// the componentDidMount, but if the ref doesn't exist, it will try again | ||
// on the following componentDidUpdate. The try will be done only once. | ||
// @see https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element | ||
updateBackgroundImage = (lastTry = false) => { | ||
theme = createMuiTheme(this.props.theme); | ||
containerRef = React.createRef<HTMLDivElement>(); | ||
backgroundImageLoaded = false; | ||
|
||
updateBackgroundImage = () => { | ||
if (!this.backgroundImageLoaded && this.containerRef.current) { | ||
const { backgroundImage } = this.props; | ||
this.containerRef.current.style.backgroundImage = `url(${backgroundImage})`; | ||
this.backgroundImageLoaded = true; | ||
} | ||
|
||
if (lastTry) { | ||
this.backgroundImageLoaded = true; | ||
} | ||
}; | ||
|
||
// Load background image asynchronously to speed up time to interactive | ||
|
@@ -114,18 +118,25 @@ class Login extends Component { | |
|
||
componentDidUpdate() { | ||
if (!this.backgroundImageLoaded) { | ||
this.lazyLoadBackgroundImage(true); | ||
this.lazyLoadBackgroundImage(); | ||
} | ||
} | ||
|
||
render() { | ||
const { classes, className, loginForm, ...rest } = this.props; | ||
const { | ||
backgroundImage, | ||
classes, | ||
className, | ||
loginForm, | ||
staticContext, | ||
...rest | ||
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. Some of the props that were in 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 they were unnecessary. I checked |
||
} = this.props; | ||
|
||
return ( | ||
<MuiThemeProvider theme={this.theme}> | ||
<div | ||
className={classnames(classes.main, className)} | ||
{...sanitizeRestProps(rest)} | ||
{...rest} | ||
fzaninotto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ref={this.containerRef} | ||
> | ||
<Card className={classes.card}> | ||
|
@@ -143,21 +154,6 @@ class Login extends Component { | |
} | ||
} | ||
|
||
Login.propTypes = { | ||
authProvider: PropTypes.func, | ||
backgroundImage: PropTypes.string, | ||
classes: PropTypes.object, | ||
className: PropTypes.string, | ||
input: PropTypes.object, | ||
loginForm: PropTypes.element, | ||
meta: PropTypes.object, | ||
previousRoute: PropTypes.string, | ||
}; | ||
|
||
Login.defaultProps = { | ||
backgroundImage: 'https://source.unsplash.com/random/1600x900/daily', | ||
theme: defaultTheme, | ||
loginForm: <DefaultLoginForm />, | ||
}; | ||
const EnhancedLogin = withStyles(styles)(Login) as ComponentType<Props>; | ||
|
||
export default withStyles(styles)(Login); | ||
export default EnhancedLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why did you remove the lastTry? See the comment above the function.
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.
First, because this code does not work at all, read the original carefully and look where we were passing
lastTry
. Second because TypeScript agrees with meThere 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.
then you should remove the comment above the method, too