2019-03-21 01:22:31 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { LoginForm, RegisterForm, LoginResponse, UserOperation } from '../interfaces';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
|
|
|
import { msgOp } from '../utils';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
loginForm: LoginForm;
|
|
|
|
registerForm: RegisterForm;
|
2019-04-08 21:46:09 +00:00
|
|
|
loginLoading: boolean;
|
|
|
|
registerLoading: boolean;
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class Login extends Component<any, State> {
|
2019-03-23 01:42:57 +00:00
|
|
|
private subscription: Subscription;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-04-25 15:51:23 +00:00
|
|
|
emptyState: State = {
|
|
|
|
loginForm: {
|
|
|
|
username_or_email: undefined,
|
|
|
|
password: undefined
|
|
|
|
},
|
|
|
|
registerForm: {
|
|
|
|
username: undefined,
|
|
|
|
password: undefined,
|
|
|
|
password_verify: undefined,
|
|
|
|
admin: false,
|
2019-08-14 02:52:43 +00:00
|
|
|
show_nsfw: false,
|
2019-04-25 15:51:23 +00:00
|
|
|
},
|
|
|
|
loginLoading: false,
|
2019-04-26 05:50:42 +00:00
|
|
|
registerLoading: false,
|
2019-04-25 15:51:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
super(props, context);
|
|
|
|
|
2019-04-25 15:51:23 +00:00
|
|
|
this.state = this.emptyState;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2019-04-08 21:46:09 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
(msg) => this.parseMessage(msg),
|
2019-03-23 01:42:57 +00:00
|
|
|
(err) => console.error(err),
|
2019-03-25 03:51:27 +00:00
|
|
|
() => console.log("complete")
|
2019-04-08 21:46:09 +00:00
|
|
|
);
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-04-22 16:24:13 +00:00
|
|
|
componentDidMount() {
|
2019-08-10 00:14:43 +00:00
|
|
|
document.title = `${i18n.t('login')} - ${WebSocketService.Instance.site.name}`;
|
2019-04-22 16:24:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 mb-4">
|
|
|
|
{this.loginForm()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-lg-6">
|
|
|
|
{this.registerForm()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
loginForm() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>Login</h5>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="form-group row">
|
2019-08-10 00:14:43 +00:00
|
|
|
<label class="col-sm-2 col-form-label"><T i18nKey="email_or_username">#</T></label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-03-23 01:42:57 +00:00
|
|
|
<input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-08-10 00:14:43 +00:00
|
|
|
<label class="col-sm-2 col-form-label"><T i18nKey="password">#</T></label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
|
|
|
<input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
2019-04-08 21:46:09 +00:00
|
|
|
<button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
|
2019-08-10 00:14:43 +00:00
|
|
|
<svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : i18n.t('login')}</button>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
registerForm() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
|
2019-08-10 00:14:43 +00:00
|
|
|
<h5><T i18nKey="sign_up">#</T></h5>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="form-group row">
|
2019-08-10 00:14:43 +00:00
|
|
|
<label class="col-sm-2 col-form-label"><T i18nKey="username">#</T></label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-04-18 15:14:45 +00:00
|
|
|
<input type="text" class="form-control" value={this.state.registerForm.username} onInput={linkEvent(this, this.handleRegisterUsernameChange)} required minLength={3} maxLength={20} pattern="[a-zA-Z0-9_]+" />
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-08-10 00:14:43 +00:00
|
|
|
<label class="col-sm-2 col-form-label"><T i18nKey="email">#</T></label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-08-10 00:14:43 +00:00
|
|
|
<input type="email" class="form-control" placeholder={i18n.t('optional')} value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-08-10 00:14:43 +00:00
|
|
|
<label class="col-sm-2 col-form-label"><T i18nKey="password">#</T></label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
|
|
|
<input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-08-10 00:14:43 +00:00
|
|
|
<label class="col-sm-2 col-form-label"><T i18nKey="verify_password">#</T></label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
|
|
|
<input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-08-14 02:52:43 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<div class="form-check">
|
|
|
|
<input class="form-check-input" type="checkbox" checked={this.state.registerForm.show_nsfw} onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}/>
|
|
|
|
<label class="form-check-label"><T i18nKey="show_nsfw">#</T></label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
2019-04-08 21:46:09 +00:00
|
|
|
<button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
|
2019-08-10 00:14:43 +00:00
|
|
|
<svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : i18n.t('sign_up')}</button>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleLoginSubmit(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
event.preventDefault();
|
2019-04-08 21:46:09 +00:00
|
|
|
i.state.loginLoading = true;
|
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
WebSocketService.Instance.login(i.state.loginForm);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleLoginUsernameChange(i: Login, event: any) {
|
2019-03-23 01:42:57 +00:00
|
|
|
i.state.loginForm.username_or_email = event.target.value;
|
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleLoginPasswordChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.loginForm.password = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterSubmit(i: Login, event: any) {
|
2019-04-16 23:04:23 +00:00
|
|
|
event.preventDefault();
|
2019-04-08 21:46:09 +00:00
|
|
|
i.state.registerLoading = true;
|
|
|
|
i.setState(i.state);
|
2019-04-26 05:50:42 +00:00
|
|
|
|
2019-05-15 16:32:33 +00:00
|
|
|
WebSocketService.Instance.register(i.state.registerForm);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterUsernameChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.username = event.target.value;
|
2019-04-26 15:31:23 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterEmailChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.email = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterPasswordChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.password = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterPasswordVerifyChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.password_verify = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
handleRegisterShowNsfwChange(i: Login, event: any) {
|
|
|
|
i.state.registerForm.show_nsfw = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
2019-08-10 00:14:43 +00:00
|
|
|
alert(i18n.t(msg.error));
|
2019-04-25 15:51:23 +00:00
|
|
|
this.state = this.emptyState;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.setState(this.state);
|
2019-03-23 01:42:57 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2019-04-25 18:26:44 +00:00
|
|
|
if (op == UserOperation.Login) {
|
2019-05-02 05:26:31 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
this.setState(this.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
let res: LoginResponse = msg;
|
2019-04-08 05:19:02 +00:00
|
|
|
UserService.Instance.login(res);
|
2019-03-23 01:42:57 +00:00
|
|
|
this.props.history.push('/');
|
2019-04-25 18:26:44 +00:00
|
|
|
} else if (op == UserOperation.Register) {
|
2019-05-02 05:26:31 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
this.setState(this.state);
|
2019-04-25 18:26:44 +00:00
|
|
|
let res: LoginResponse = msg;
|
|
|
|
UserService.Instance.login(res);
|
|
|
|
this.props.history.push('/communities');
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-04-25 18:26:44 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|