This repository has been archived on 2020-04-21. You can view files and clone it, but cannot push or open issues or pull requests.
lemmy/ui/src/components/setup.tsx

210 lines
5.7 KiB
TypeScript
Raw Normal View History

2019-04-16 23:04:23 +00:00
import { Component, linkEvent } from 'inferno';
import { Subscription } from 'rxjs';
2019-04-16 23:04:23 +00:00
import { retryWhen, delay, take } from 'rxjs/operators';
import { RegisterForm, LoginResponse, UserOperation } from '../interfaces';
import { WebSocketService, UserService } from '../services';
import { msgOp } from '../utils';
import { SiteForm } from './site-form';
import { i18n } from '../i18next';
import { T } from 'inferno-i18next';
2019-04-16 23:04:23 +00:00
interface State {
userForm: RegisterForm;
doneRegisteringUser: boolean;
userLoading: boolean;
}
export class Setup extends Component<any, State> {
private subscription: Subscription;
private emptyState: State = {
userForm: {
username: undefined,
password: undefined,
password_verify: undefined,
admin: true,
show_nsfw: true,
2019-04-16 23:04:23 +00:00
},
doneRegisteringUser: false,
userLoading: false,
};
2019-04-16 23:04:23 +00:00
constructor(props: any, context: any) {
super(props, context);
this.state = this.emptyState;
this.subscription = WebSocketService.Instance.subject
.pipe(
retryWhen(errors =>
errors.pipe(
delay(3000),
take(10)
)
)
)
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
() => console.log('complete')
);
2019-04-16 23:04:23 +00:00
}
componentWillUnmount() {
this.subscription.unsubscribe();
}
2019-04-22 16:24:13 +00:00
componentDidMount() {
document.title = `${i18n.t('setup')} - Lemmy`;
2019-04-22 16:24:13 +00:00
}
2019-04-16 23:04:23 +00:00
render() {
return (
<div class="container">
<div class="row">
<div class="col-12 offset-lg-3 col-lg-6">
<h3>
<T i18nKey="lemmy_instance_setup">#</T>
</h3>
{!this.state.doneRegisteringUser ? (
this.registerUser()
) : (
<SiteForm />
)}
2019-04-16 23:04:23 +00:00
</div>
</div>
</div>
);
2019-04-16 23:04:23 +00:00
}
registerUser() {
return (
<form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
<h5>
<T i18nKey="setup_admin">#</T>
</h5>
2019-04-16 23:04:23 +00:00
<div class="form-group row">
<label class="col-sm-2 col-form-label">
<T i18nKey="username">#</T>
</label>
2019-04-16 23:04:23 +00:00
<div class="col-sm-10">
<input
type="text"
class="form-control"
value={this.state.userForm.username}
onInput={linkEvent(this, this.handleRegisterUsernameChange)}
required
minLength={3}
maxLength={20}
pattern="[a-zA-Z0-9_]+"
/>
2019-04-16 23:04:23 +00:00
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">
<T i18nKey="email">#</T>
</label>
2019-04-16 23:04:23 +00:00
<div class="col-sm-10">
<input
type="email"
class="form-control"
placeholder={i18n.t('optional')}
value={this.state.userForm.email}
onInput={linkEvent(this, this.handleRegisterEmailChange)}
minLength={3}
/>
2019-04-16 23:04:23 +00:00
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">
<T i18nKey="password">#</T>
</label>
2019-04-16 23:04:23 +00:00
<div class="col-sm-10">
<input
type="password"
value={this.state.userForm.password}
onInput={linkEvent(this, this.handleRegisterPasswordChange)}
class="form-control"
required
/>
2019-04-16 23:04:23 +00:00
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">
<T i18nKey="verify_password">#</T>
</label>
2019-04-16 23:04:23 +00:00
<div class="col-sm-10">
<input
type="password"
value={this.state.userForm.password_verify}
onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
class="form-control"
required
/>
2019-04-16 23:04:23 +00:00
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-secondary">
{this.state.userLoading ? (
<svg class="icon icon-spinner spin">
<use xlinkHref="#icon-spinner"></use>
</svg>
) : (
i18n.t('sign_up')
)}
</button>
2019-04-16 23:04:23 +00:00
</div>
</div>
</form>
);
}
handleRegisterSubmit(i: Setup, event: any) {
event.preventDefault();
i.state.userLoading = true;
i.setState(i.state);
event.preventDefault();
WebSocketService.Instance.register(i.state.userForm);
}
handleRegisterUsernameChange(i: Setup, event: any) {
i.state.userForm.username = event.target.value;
i.setState(i.state);
}
handleRegisterEmailChange(i: Setup, event: any) {
i.state.userForm.email = event.target.value;
i.setState(i.state);
}
handleRegisterPasswordChange(i: Setup, event: any) {
i.state.userForm.password = event.target.value;
i.setState(i.state);
}
handleRegisterPasswordVerifyChange(i: Setup, event: any) {
i.state.userForm.password_verify = event.target.value;
i.setState(i.state);
}
parseMessage(msg: any) {
let op: UserOperation = msgOp(msg);
if (msg.error) {
alert(i18n.t(msg.error));
2019-04-16 23:04:23 +00:00
this.state.userLoading = false;
this.setState(this.state);
return;
} else if (op == UserOperation.Register) {
this.state.userLoading = false;
this.state.doneRegisteringUser = true;
let res: LoginResponse = msg;
UserService.Instance.login(res);
console.log(res);
this.setState(this.state);
} else if (op == UserOperation.CreateSite) {
this.props.history.push('/');
}
}
}