]> Untitled Git - lemmy-ui.git/blob - src/shared/components/person/password-change.tsx
b36c15a297f12526682bcd2c06760435ad5800cc
[lemmy-ui.git] / src / shared / components / person / password-change.tsx
1 import { Component, linkEvent } from "inferno";
2 import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
3 import { i18n } from "../../i18next";
4 import { HttpService, UserService } from "../../services";
5 import { RequestState } from "../../services/HttpService";
6 import { capitalizeFirstLetter, myAuth, setIsoData } from "../../utils";
7 import { HtmlTags } from "../common/html-tags";
8 import { Spinner } from "../common/icon";
9
10 interface State {
11   passwordChangeRes: RequestState<LoginResponse>;
12   form: {
13     token: string;
14     password?: string;
15     password_verify?: string;
16   };
17   siteRes: GetSiteResponse;
18 }
19
20 export class PasswordChange extends Component<any, State> {
21   private isoData = setIsoData(this.context);
22
23   state: State = {
24     passwordChangeRes: { state: "empty" },
25     siteRes: this.isoData.site_res,
26     form: {
27       token: this.props.match.params.token,
28     },
29   };
30
31   constructor(props: any, context: any) {
32     super(props, context);
33   }
34
35   get documentTitle(): string {
36     return `${i18n.t("password_change")} - ${
37       this.state.siteRes.site_view.site.name
38     }`;
39   }
40
41   render() {
42     return (
43       <div className="container-lg">
44         <HtmlTags
45           title={this.documentTitle}
46           path={this.context.router.route.match.url}
47         />
48         <div className="row">
49           <div className="col-12 col-lg-6 offset-lg-3 mb-4">
50             <h5>{i18n.t("password_change")}</h5>
51             {this.passwordChangeForm()}
52           </div>
53         </div>
54       </div>
55     );
56   }
57
58   passwordChangeForm() {
59     return (
60       <form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
61         <div className="mb-3 row">
62           <label className="col-sm-2 col-form-label" htmlFor="new-password">
63             {i18n.t("new_password")}
64           </label>
65           <div className="col-sm-10">
66             <input
67               id="new-password"
68               type="password"
69               value={this.state.form.password}
70               onInput={linkEvent(this, this.handlePasswordChange)}
71               className="form-control"
72               required
73               maxLength={60}
74             />
75           </div>
76         </div>
77         <div className="mb-3 row">
78           <label className="col-sm-2 col-form-label" htmlFor="verify-password">
79             {i18n.t("verify_password")}
80           </label>
81           <div className="col-sm-10">
82             <input
83               id="verify-password"
84               type="password"
85               value={this.state.form.password_verify}
86               onInput={linkEvent(this, this.handleVerifyPasswordChange)}
87               className="form-control"
88               required
89               maxLength={60}
90             />
91           </div>
92         </div>
93         <div className="mb-3 row">
94           <div className="col-sm-10">
95             <button type="submit" className="btn btn-secondary">
96               {this.state.passwordChangeRes.state == "loading" ? (
97                 <Spinner />
98               ) : (
99                 capitalizeFirstLetter(i18n.t("save"))
100               )}
101             </button>
102           </div>
103         </div>
104       </form>
105     );
106   }
107
108   handlePasswordChange(i: PasswordChange, event: any) {
109     i.state.form.password = event.target.value;
110     i.setState(i.state);
111   }
112
113   handleVerifyPasswordChange(i: PasswordChange, event: any) {
114     i.state.form.password_verify = event.target.value;
115     i.setState(i.state);
116   }
117
118   async handlePasswordChangeSubmit(i: PasswordChange, event: any) {
119     event.preventDefault();
120     i.setState({ passwordChangeRes: { state: "loading" } });
121
122     const password = i.state.form.password;
123     const password_verify = i.state.form.password_verify;
124
125     if (password && password_verify) {
126       i.setState({
127         passwordChangeRes: await HttpService.client.passwordChangeAfterReset({
128           token: i.state.form.token,
129           password,
130           password_verify,
131         }),
132       });
133
134       if (i.state.passwordChangeRes.state === "success") {
135         const data = i.state.passwordChangeRes.data;
136         UserService.Instance.login(data);
137
138         const site = await HttpService.client.getSite({ auth: myAuth() });
139         if (site.state === "success") {
140           UserService.Instance.myUserInfo = site.data.my_user;
141         }
142
143         i.props.history.replace("/");
144       }
145     }
146   }
147 }