]> Untitled Git - lemmy-ui.git/blob - src/shared/components/person/password-change.tsx
fix toaster upon user settings change (#1802)
[lemmy-ui.git] / src / shared / components / person / password-change.tsx
1 import { myAuth, setIsoData } from "@utils/app";
2 import { capitalizeFirstLetter } from "@utils/helpers";
3 import { Component, linkEvent } from "inferno";
4 import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
5 import { HttpService, I18NextService, UserService } from "../../services";
6 import { RequestState } from "../../services/HttpService";
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 `${I18NextService.i18n.t("password_change")} - ${
37       this.state.siteRes.site_view.site.name
38     }`;
39   }
40
41   render() {
42     return (
43       <div className="password-change 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             <h1 className="h4 mb-4">
51               {I18NextService.i18n.t("password_change")}
52             </h1>
53             {this.passwordChangeForm()}
54           </div>
55         </div>
56       </div>
57     );
58   }
59
60   passwordChangeForm() {
61     return (
62       <form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
63         <div className="mb-3 row">
64           <label className="col-sm-2 col-form-label" htmlFor="new-password">
65             {I18NextService.i18n.t("new_password")}
66           </label>
67           <div className="col-sm-10">
68             <input
69               id="new-password"
70               type="password"
71               value={this.state.form.password}
72               onInput={linkEvent(this, this.handlePasswordChange)}
73               className="form-control"
74               required
75               maxLength={60}
76             />
77           </div>
78         </div>
79         <div className="mb-3 row">
80           <label className="col-sm-2 col-form-label" htmlFor="verify-password">
81             {I18NextService.i18n.t("verify_password")}
82           </label>
83           <div className="col-sm-10">
84             <input
85               id="verify-password"
86               type="password"
87               value={this.state.form.password_verify}
88               onInput={linkEvent(this, this.handleVerifyPasswordChange)}
89               className="form-control"
90               required
91               maxLength={60}
92             />
93           </div>
94         </div>
95         <div className="mb-3 row">
96           <div className="col-sm-10">
97             <button type="submit" className="btn btn-secondary">
98               {this.state.passwordChangeRes.state == "loading" ? (
99                 <Spinner />
100               ) : (
101                 capitalizeFirstLetter(I18NextService.i18n.t("save"))
102               )}
103             </button>
104           </div>
105         </div>
106       </form>
107     );
108   }
109
110   handlePasswordChange(i: PasswordChange, event: any) {
111     i.state.form.password = event.target.value;
112     i.setState(i.state);
113   }
114
115   handleVerifyPasswordChange(i: PasswordChange, event: any) {
116     i.state.form.password_verify = event.target.value;
117     i.setState(i.state);
118   }
119
120   async handlePasswordChangeSubmit(i: PasswordChange, event: any) {
121     event.preventDefault();
122     i.setState({ passwordChangeRes: { state: "loading" } });
123
124     const password = i.state.form.password;
125     const password_verify = i.state.form.password_verify;
126
127     if (password && password_verify) {
128       i.setState({
129         passwordChangeRes: await HttpService.client.passwordChangeAfterReset({
130           token: i.state.form.token,
131           password,
132           password_verify,
133         }),
134       });
135
136       if (i.state.passwordChangeRes.state === "success") {
137         const data = i.state.passwordChangeRes.data;
138         UserService.Instance.login({
139           res: data,
140         });
141
142         const site = await HttpService.client.getSite({ auth: myAuth() });
143         if (site.state === "success") {
144           UserService.Instance.myUserInfo = site.data.my_user;
145         }
146
147         i.props.history.replace("/");
148       }
149     }
150   }
151 }