2019-11-02 06:41:57 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2020-07-27 15:54:42 +00:00
|
|
|
import { Helmet } from 'inferno-helmet';
|
2019-11-02 06:41:57 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
LoginResponse,
|
|
|
|
PasswordChangeForm,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2020-07-08 05:02:14 +00:00
|
|
|
GetSiteResponse,
|
2020-07-27 15:54:42 +00:00
|
|
|
Site,
|
2020-08-20 14:50:26 +00:00
|
|
|
} from 'lemmy-js-client';
|
2019-11-02 06:41:57 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2020-01-24 00:17:42 +00:00
|
|
|
import { wsJsonToRes, capitalizeFirstLetter, toast } from '../utils';
|
2019-11-02 06:41:57 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
passwordChangeForm: PasswordChangeForm;
|
|
|
|
loading: boolean;
|
2020-07-27 15:54:42 +00:00
|
|
|
site: Site;
|
2019-11-02 06:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PasswordChange extends Component<any, State> {
|
|
|
|
private subscription: Subscription;
|
|
|
|
|
|
|
|
emptyState: State = {
|
|
|
|
passwordChangeForm: {
|
|
|
|
token: this.props.match.params.token,
|
|
|
|
password: undefined,
|
|
|
|
password_verify: undefined,
|
|
|
|
},
|
|
|
|
loading: false,
|
2020-07-27 15:54:42 +00:00
|
|
|
site: undefined,
|
2019-11-02 06:41:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-23 03:29:11 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-11-02 06:41:57 +00:00
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
2020-07-08 05:02:14 +00:00
|
|
|
WebSocketService.Instance.getSite();
|
2019-11-02 06:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:54:42 +00:00
|
|
|
get documentTitle(): string {
|
|
|
|
if (this.state.site) {
|
|
|
|
return `${i18n.t('password_change')} - ${this.state.site.name}`;
|
|
|
|
} else {
|
|
|
|
return 'Lemmy';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 06:41:57 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-07-27 15:54:42 +00:00
|
|
|
<Helmet title={this.documentTitle} />
|
2019-11-02 06:41:57 +00:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
|
2020-02-02 18:50:44 +00:00
|
|
|
<h5>{i18n.t('password_change')}</h5>
|
2019-11-02 06:41:57 +00:00
|
|
|
{this.passwordChangeForm()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordChangeForm() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-sm-2 col-form-label">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('new_password')}
|
2019-11-02 06:41:57 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
value={this.state.passwordChangeForm.password}
|
|
|
|
onInput={linkEvent(this, this.handlePasswordChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-sm-2 col-form-label">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('verify_password')}
|
2019-11-02 06:41:57 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
value={this.state.passwordChangeForm.password_verify}
|
|
|
|
onInput={linkEvent(this, this.handleVerifyPasswordChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<button type="submit" class="btn btn-secondary">
|
|
|
|
{this.state.loading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : (
|
|
|
|
capitalizeFirstLetter(i18n.t('save'))
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePasswordChange(i: PasswordChange, event: any) {
|
|
|
|
i.state.passwordChangeForm.password = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleVerifyPasswordChange(i: PasswordChange, event: any) {
|
|
|
|
i.state.passwordChangeForm.password_verify = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePasswordChangeSubmit(i: PasswordChange, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.loading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
|
|
|
|
WebSocketService.Instance.passwordChange(i.state.passwordChangeForm);
|
|
|
|
}
|
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2019-11-02 06:41:57 +00:00
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-11-02 06:41:57 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
return;
|
2020-07-08 05:02:14 +00:00
|
|
|
} else if (res.op == UserOperation.PasswordChange) {
|
|
|
|
let data = res.data as LoginResponse;
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.setState(this.state);
|
|
|
|
UserService.Instance.login(data);
|
|
|
|
this.props.history.push('/');
|
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
2020-07-27 15:54:42 +00:00
|
|
|
this.state.site = data.site;
|
|
|
|
this.setState(this.state);
|
2019-11-02 06:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|