File size: 3,552 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
// @flow
import * as React from 'react';
import slugg from 'slugg';
import { connect } from 'react-redux';
import { withApollo } from 'react-apollo';
import compose from 'recompose/compose';
import { Error, Success } from 'src/components/formElements';
import UsernameSearch from 'src/components/usernameSearch';
import { addToastWithTimeout } from 'src/actions/toasts';
import { Form, Row } from './style';
import editUserMutation from 'shared/graphql/mutations/user/editUser';
import { ContinueButton } from '../../style';
import type { Dispatch } from 'redux';
type Props = {
client: Object,
editUser: Function,
save: Function,
dispatch: Dispatch<Object>,
user: ?Object,
};
type State = {
username: string,
error: string,
success: string,
isLoading: boolean,
};
class SetUsername extends React.Component<Props, State> {
_isMounted = false;
constructor(props) {
super(props);
const { user } = props;
// try to intelligently suggest a starting username based on the
// person's name, or firstname/lastname
let username = user
? user.name
? slugg(user.name)
: user.firstName && user.lastName
? `${user.firstName}-${user.lastName}`
: ''
: '';
this.state = {
username: username,
error: '',
success: '',
isLoading: false,
};
}
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
}
handleUsernameValidation = ({ error, success, username }) => {
this.setState({
error,
success,
username,
});
};
saveUsername = e => {
e.preventDefault();
const { username } = this.state;
this.setState({
isLoading: true,
});
const input = {
username,
};
this.props
.editUser(input)
.then(() => {
if (!this._isMounted) return;
this.setState({
isLoading: false,
success: '',
});
// trigger a method in the newUserOnboarding component class
// to determine what to do next with this user - either push them
// to community discovery or close the onboarding completely
return this.props.save();
})
.catch(err => {
if (!this._isMounted) return;
this.setState({
isLoading: false,
success: '',
});
this.props.dispatch(addToastWithTimeout('error', err.message));
});
};
render() {
const { username, isLoading, error, success } = this.state;
return (
<Form onSubmit={this.saveUsername}>
<Row>
<UsernameSearch
placeholder={'Your username...'}
autoFocus={true}
username={username}
onValidationResult={this.handleUsernameValidation}
dataCy={'username-search'}
/>
</Row>
<Row style={{ minHeight: '43px' }}>
{error && <Error data-cy="username-search-error">{error}</Error>}
{success && (
<Success data-cy="username-search-success">{success}</Success>
)}
</Row>
<Row>
<ContinueButton
onClick={this.saveUsername}
disabled={!username || error}
loading={isLoading}
data-cy="save-username-button"
>
{isLoading ? 'Saving...' : 'Save and Continue'}
</ContinueButton>
</Row>
</Form>
);
}
}
export default compose(
editUserMutation,
withApollo,
connect()
)(SetUsername);
|