]> Untitled Git - lemmy-ui.git/blob - src/shared/components/password_change.tsx
Merge remote-tracking branch 'origin/drone-ci' into drone-ci-dess
[lemmy-ui.git] / src / shared / components / password_change.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import {
4   UserOperation,
5   LoginResponse,
6   PasswordChange as PasswordChangeForm,
7   SiteView,
8 } from 'lemmy-js-client';
9 import { WebSocketService, UserService } from '../services';
10 import {
11   wsJsonToRes,
12   capitalizeFirstLetter,
13   toast,
14   setIsoData,
15   isBrowser,
16   wsSubscribe,
17   wsUserOp,
18   wsClient,
19 } from '../utils';
20 import { i18n } from '../i18next';
21 import { HtmlTags } from './html-tags';
22
23 interface State {
24   passwordChangeForm: PasswordChangeForm;
25   loading: boolean;
26   site_view: SiteView;
27 }
28
29 export class PasswordChange extends Component<any, State> {
30   private isoData = setIsoData(this.context);
31   private subscription: Subscription;
32
33   emptyState: State = {
34     passwordChangeForm: {
35       token: this.props.match.params.token,
36       password: undefined,
37       password_verify: undefined,
38     },
39     loading: false,
40     site_view: this.isoData.site_res.site_view,
41   };
42
43   constructor(props: any, context: any) {
44     super(props, context);
45
46     this.state = this.emptyState;
47
48     this.parseMessage = this.parseMessage.bind(this);
49     this.subscription = wsSubscribe(this.parseMessage);
50   }
51
52   componentWillUnmount() {
53     if (isBrowser()) {
54       this.subscription.unsubscribe();
55     }
56   }
57
58   get documentTitle(): string {
59     return `${i18n.t('password_change')} - ${this.state.site_view.site.name}`;
60   }
61
62   render() {
63     return (
64       <div class="container">
65         <HtmlTags
66           title={this.documentTitle}
67           path={this.context.router.route.match.url}
68         />
69         <div class="row">
70           <div class="col-12 col-lg-6 offset-lg-3 mb-4">
71             <h5>{i18n.t('password_change')}</h5>
72             {this.passwordChangeForm()}
73           </div>
74         </div>
75       </div>
76     );
77   }
78
79   passwordChangeForm() {
80     return (
81       <form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
82         <div class="form-group row">
83           <label class="col-sm-2 col-form-label">
84             {i18n.t('new_password')}
85           </label>
86           <div class="col-sm-10">
87             <input
88               type="password"
89               value={this.state.passwordChangeForm.password}
90               onInput={linkEvent(this, this.handlePasswordChange)}
91               class="form-control"
92               required
93             />
94           </div>
95         </div>
96         <div class="form-group row">
97           <label class="col-sm-2 col-form-label">
98             {i18n.t('verify_password')}
99           </label>
100           <div class="col-sm-10">
101             <input
102               type="password"
103               value={this.state.passwordChangeForm.password_verify}
104               onInput={linkEvent(this, this.handleVerifyPasswordChange)}
105               class="form-control"
106               required
107             />
108           </div>
109         </div>
110         <div class="form-group row">
111           <div class="col-sm-10">
112             <button type="submit" class="btn btn-secondary">
113               {this.state.loading ? (
114                 <svg class="icon icon-spinner spin">
115                   <use xlinkHref="#icon-spinner"></use>
116                 </svg>
117               ) : (
118                 capitalizeFirstLetter(i18n.t('save'))
119               )}
120             </button>
121           </div>
122         </div>
123       </form>
124     );
125   }
126
127   handlePasswordChange(i: PasswordChange, event: any) {
128     i.state.passwordChangeForm.password = event.target.value;
129     i.setState(i.state);
130   }
131
132   handleVerifyPasswordChange(i: PasswordChange, event: any) {
133     i.state.passwordChangeForm.password_verify = event.target.value;
134     i.setState(i.state);
135   }
136
137   handlePasswordChangeSubmit(i: PasswordChange, event: any) {
138     event.preventDefault();
139     i.state.loading = true;
140     i.setState(i.state);
141
142     WebSocketService.Instance.send(
143       wsClient.passwordChange(i.state.passwordChangeForm)
144     );
145   }
146
147   parseMessage(msg: any) {
148     let op = wsUserOp(msg);
149     if (msg.error) {
150       toast(i18n.t(msg.error), 'danger');
151       this.state.loading = false;
152       this.setState(this.state);
153       return;
154     } else if (op == UserOperation.PasswordChange) {
155       let data = wsJsonToRes<LoginResponse>(msg).data;
156       this.state = this.emptyState;
157       this.setState(this.state);
158       UserService.Instance.login(data);
159       this.props.history.push('/');
160     }
161   }
162 }