Sign-in form improvements

Summary:
Sign in form improvements:
1) Added context menu with "Paste" and "Reset" items
2) Added "Reset" button
3) When form is empty, simple click to it automatically pastes token from clipboard
4) Automatically sign-in after token is pasted into empty form

Reviewed By: mweststrate

Differential Revision: D21994724

fbshipit-source-id: 8605c7f9b96777036b1b20b69370fba499185911
This commit is contained in:
Anton Nikolaev
2020-06-11 07:28:44 -07:00
committed by Facebook GitHub Bot
parent d6c97b48d3
commit f88d707dbb

View File

@@ -23,6 +23,8 @@ import {writeKeychain, getUser} from '../fb-stubs/user';
import {login} from '../reducers/user'; import {login} from '../reducers/user';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {State as Store} from '../reducers'; import {State as Store} from '../reducers';
import ContextMenu from '../ui/components/ContextMenu';
import {clipboard} from 'electron';
const Container = styled(FlexColumn)({ const Container = styled(FlexColumn)({
padding: 20, padding: 20,
@@ -67,24 +69,60 @@ class SignInSheet extends Component<Props, State> {
error: null, error: null,
}; };
saveToken = async () => { saveToken = async (token: string) => {
this.setState({loading: true}); this.setState({token, loading: true});
const {token} = this.state;
if (token) {
await writeKeychain(token);
try { try {
await writeKeychain(token);
const user = await getUser(); const user = await getUser();
if (user) { if (user) {
this.props.login(user); this.props.login(user);
} else {
throw new Error('Failed to login using the provided token');
} }
this.setState({loading: false});
this.props.onHide(); this.props.onHide();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
this.setState({token: '', loading: false, error: `${error}`}); this.setState({
loading: false,
error: `${error}`,
});
}
};
onPaste = () => {
if (!this.state.token || this.state.token === '') {
const token = clipboard.readText();
// If pasted content looks like a token, we could try to login using it straight away!
if (token && token.length >= 100 && token.indexOf(' ') < 0) {
this.saveToken(token);
} }
} }
}; };
signIn = () => {
this.saveToken(this.state.token);
};
getContextMenu = () => {
const menu: Array<Electron.MenuItemConstructorOptions> = [
{
label: 'Paste',
role: 'paste',
click: this.onPaste,
},
{
label: 'Reset',
click: this.reset,
},
];
return menu;
};
reset = () => {
this.setState({token: '', error: ''});
};
render() { render() {
return ( return (
<Container> <Container>
@@ -94,15 +132,19 @@ class SignInSheet extends Component<Props, State> {
<Link href="https://our.internmc.facebook.com/intern/oauth/nuclide/"> <Link href="https://our.internmc.facebook.com/intern/oauth/nuclide/">
open this page open this page
</Link> </Link>
, copy the Nuclide access token you find on that page, and paste it , copy the Nuclide access token you find on that page to clipboard,
into the text input below. and click the text input below to paste it.
</InfoText> </InfoText>
<ContextMenu items={this.getContextMenu()}>
<TokenInput <TokenInput
disabled={this.state.loading} disabled={this.state.loading}
placeholder="Nuclide Access Token" placeholder="Click to paste Nuclide Access Token from clipboard"
onClick={this.onPaste}
value={this.state.token} value={this.state.token}
onPaste={this.onPaste}
onChange={(e) => this.setState({token: e.target.value})} onChange={(e) => this.setState({token: e.target.value})}
/> />
</ContextMenu>
<br /> <br />
{this.state.error && ( {this.state.error && (
<InfoText color={colors.red}> <InfoText color={colors.red}>
@@ -115,7 +157,10 @@ class SignInSheet extends Component<Props, State> {
<Button compact padded onClick={this.props.onHide}> <Button compact padded onClick={this.props.onHide}>
Cancel Cancel
</Button> </Button>
<Button type="primary" compact padded onClick={this.saveToken}> <Button compact padded onClick={this.reset}>
Reset
</Button>
<Button type="primary" compact padded onClick={this.signIn}>
Sign In Sign In
</Button> </Button>
</FlexRow> </FlexRow>