// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import * as Utils from '../utils/utils.jsx';
import * as Client from '../utils/client.jsx';
import UserStore from '../stores/user_store.jsx';
import BrowserStore from '../stores/browser_store.jsx';
import Constants from '../utils/constants.jsx';
import LoadingScreen from '../components/loading_screen.jsx';
import {FormattedMessage, FormattedHTMLMessage} from 'mm-intl';
import {browserHistory} from 'react-router';
class SignupUserComplete extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.inviteInfoRecieved = this.inviteInfoRecieved.bind(this);
this.state = {
data: '',
hash: '',
usedBefore: false,
email: '',
teamDisplayName: '',
teamName: '',
teamId: ''
};
}
componentWillMount() {
let data = this.props.location.query.d;
let hash = this.props.location.query.h;
const inviteId = this.props.location.query.id;
let usedBefore = false;
let email = '';
let teamDisplayName = '';
let teamName = '';
let teamId = '';
// If we have a hash in the url then we are attempting to access a private team
if (hash) {
const parsedData = JSON.parse(data);
usedBefore = BrowserStore.getGlobalItem(hash);
email = parsedData.email;
teamDisplayName = parsedData.display_name;
teamName = parsedData.name;
teamId = parsedData.id;
} else {
Client.getInviteInfo(this.inviteInfoRecieved, null, inviteId);
data = '';
hash = '';
}
this.setState({
data,
hash,
usedBefore,
email,
teamDisplayName,
teamName,
teamId
});
}
inviteInfoRecieved(data) {
if (!data) {
return;
}
this.setState({
teamDisplayName: data.display_name,
teamName: data.name,
teamId: data.id
});
}
handleSubmit(e) {
e.preventDefault();
const providedEmail = ReactDOM.findDOMNode(this.refs.email).value.trim();
if (!providedEmail) {
this.setState({
nameError: '',
emailError: (),
passwordError: '',
serverError: ''
});
return;
}
if (!Utils.isEmail(providedEmail)) {
this.setState({
nameError: '',
emailError: (),
passwordError: '',
serverError: ''
});
return;
}
const providedUsername = ReactDOM.findDOMNode(this.refs.name).value.trim().toLowerCase();
if (!providedUsername) {
this.setState({
nameError: (),
emailError: '',
passwordError: '',
serverError: ''
});
return;
}
const usernameError = Utils.isValidUsername(providedUsername);
if (usernameError === 'Cannot use a reserved word as a username.') {
this.setState({
nameError: (),
emailError: '',
passwordError: '',
serverError: ''
});
return;
} else if (usernameError) {
this.setState({
nameError: (
),
emailError: '',
passwordError: '',
serverError: ''
});
return;
}
const providedPassword = ReactDOM.findDOMNode(this.refs.password).value.trim();
if (!providedPassword || providedPassword.length < Constants.MIN_PASSWORD_LENGTH) {
this.setState({
nameError: '',
emailError: '',
passwordError: (
),
serverError: ''
});
return;
}
this.setState({
nameError: '',
emailError: '',
passwordError: '',
serverError: ''
});
const user = {
team_id: this.state.teamId,
email: providedEmail,
username: providedUsername,
password: providedPassword,
allow_marketing: true
};
Client.createUser(user, this.state.data, this.state.hash,
() => {
Client.track('signup', 'signup_user_02_complete');
Client.loginByEmail(this.state.teamName, user.email, user.password,
() => {
UserStore.setLastEmail(user.email);
if (this.state.hash > 0) {
BrowserStore.setGlobalItem(this.state.hash, JSON.stringify({usedBefore: true}));
}
browserHistory.push('/' + this.state.teamName + '/channels/town-square');
},
(err) => {
if (err.id === 'api.user.login.not_verified.app_error') {
browserHistory.push('/should_verify_email?email=' + encodeURIComponent(user.email) + '&teamname=' + encodeURIComponent(this.state.teamName));
} else {
this.setState({serverError: err.message});
}
}
);
},
(err) => {
this.setState({serverError: err.message});
}
);
}
render() {
Client.track('signup', 'signup_user_01_welcome');
// If we have been used then just display a message
if (this.state.usedBefore) {
return (
);
}
// If we haven't got a team id yet we are waiting for
// the client so just show the standard loading screen
if (this.state.teamId === '') {
return ();
}
// set up error labels
var emailError = null;
var emailHelpText = (
);
var emailDivStyle = 'form-group';
if (this.state.emailError) {
emailError = ();
emailHelpText = '';
emailDivStyle += ' has-error';
}
var nameError = null;
var nameHelpText = (
);
var nameDivStyle = 'form-group';
if (this.state.nameError) {
nameError = ;
nameHelpText = '';
nameDivStyle += ' has-error';
}
var passwordError = null;
var passwordDivStyle = 'form-group';
if (this.state.passwordError) {
passwordError = ;
passwordDivStyle += ' has-error';
}
var serverError = null;
if (this.state.serverError) {
serverError = (
);
}
// set up the email entry and hide it if an email was provided
var yourEmailIs = '';
if (this.state.email) {
yourEmailIs = (
);
}
var emailContainerStyle = 'margin--extra';
if (this.state.email) {
emailContainerStyle = 'hidden';
}
var email = (
{emailError}
{emailHelpText}
);
var signupMessage = [];
if (global.window.mm_config.EnableSignUpWithGitLab === 'true') {
signupMessage.push(
);
}
if (global.window.mm_config.EnableSignUpWithGoogle === 'true') {
signupMessage.push(
);
}
var emailSignup;
if (global.window.mm_config.EnableSignUpWithEmail === 'true') {
emailSignup = (