1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
8 } from '../interfaces';
9 import { WebSocketService, UserService } from '../services';
10 import { wsJsonToRes, capitalizeFirstLetter } from '../utils';
11 import { i18n } from '../i18next';
12 import { T } from 'inferno-i18next';
15 passwordChangeForm: PasswordChangeForm;
19 export class PasswordChange extends Component<any, State> {
20 private subscription: Subscription;
24 token: this.props.match.params.token,
26 password_verify: undefined,
31 constructor(props: any, context: any) {
32 super(props, context);
34 this.state = this.emptyState;
36 this.subscription = WebSocketService.Instance.subject
37 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
39 msg => this.parseMessage(msg),
40 err => console.error(err),
41 () => console.log('complete')
45 componentWillUnmount() {
46 this.subscription.unsubscribe();
50 document.title = `${i18n.t('password_change')} - ${
51 WebSocketService.Instance.site.name
57 <div class="container">
59 <div class="col-12 col-lg-6 offset-lg-3 mb-4">
61 <T i18nKey="password_change">#</T>
63 {this.passwordChangeForm()}
70 passwordChangeForm() {
72 <form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
73 <div class="form-group row">
74 <label class="col-sm-2 col-form-label">
75 <T i18nKey="new_password">#</T>
77 <div class="col-sm-10">
80 value={this.state.passwordChangeForm.password}
81 onInput={linkEvent(this, this.handlePasswordChange)}
87 <div class="form-group row">
88 <label class="col-sm-2 col-form-label">
89 <T i18nKey="verify_password">#</T>
91 <div class="col-sm-10">
94 value={this.state.passwordChangeForm.password_verify}
95 onInput={linkEvent(this, this.handleVerifyPasswordChange)}
101 <div class="form-group row">
102 <div class="col-sm-10">
103 <button type="submit" class="btn btn-secondary">
104 {this.state.loading ? (
105 <svg class="icon icon-spinner spin">
106 <use xlinkHref="#icon-spinner"></use>
109 capitalizeFirstLetter(i18n.t('save'))
118 handlePasswordChange(i: PasswordChange, event: any) {
119 i.state.passwordChangeForm.password = event.target.value;
123 handleVerifyPasswordChange(i: PasswordChange, event: any) {
124 i.state.passwordChangeForm.password_verify = event.target.value;
128 handlePasswordChangeSubmit(i: PasswordChange, event: any) {
129 event.preventDefault();
130 i.state.loading = true;
133 WebSocketService.Instance.passwordChange(i.state.passwordChangeForm);
136 parseMessage(msg: any) {
137 let res = wsJsonToRes(msg);
139 alert(i18n.t(msg.error));
140 this.state.loading = false;
141 this.setState(this.state);
144 if (res.op == UserOperation.PasswordChange) {
145 let data = res.data as LoginResponse;
146 this.state = this.emptyState;
147 this.setState(this.state);
148 UserService.Instance.login(data);
149 this.props.history.push('/');