-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
ForgotPasswordModal.tsx
135 lines (114 loc) · 3.58 KB
/
ForgotPasswordModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import app from '../../forum/app';
import FormModal, { IFormModalAttrs } from '../../common/components/FormModal';
import Button from '../../common/components/Button';
import extractText from '../../common/utils/extractText';
import Stream from '../../common/utils/Stream';
import Mithril from 'mithril';
import RequestError from '../../common/utils/RequestError';
import ItemList from '../../common/utils/ItemList';
import Form from '../../common/components/Form';
export interface IForgotPasswordModalAttrs extends IFormModalAttrs {
email?: string;
}
/**
* The `ForgotPasswordModal` component displays a modal which allows the user to
* enter their email address and request a link to reset their password.
*/
export default class ForgotPasswordModal<CustomAttrs extends IForgotPasswordModalAttrs = IForgotPasswordModalAttrs> extends FormModal<CustomAttrs> {
/**
* The value of the email input.
*/
email!: Stream<string>;
success: boolean = false;
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
super.oninit(vnode);
this.email = Stream(this.attrs.email || '');
}
className() {
return 'ForgotPasswordModal Modal--small';
}
title() {
return app.translator.trans('core.forum.forgot_password.title');
}
content() {
if (this.success) {
return (
<div className="Modal-body">
<Form className="Form--centered">
<p className="helpText">{app.translator.trans('core.forum.forgot_password.email_sent_message')}</p>
<div className="Form-group Form-controls">
<Button className="Button Button--primary Button--block" onclick={this.hide.bind(this)}>
{app.translator.trans('core.forum.forgot_password.dismiss_button')}
</Button>
</div>
</Form>
</div>
);
}
return (
<div className="Modal-body">
<Form className="Form--centered" description={app.translator.trans('core.forum.forgot_password.text')}>
{this.fields().toArray()}
</Form>
</div>
);
}
fields() {
const items = new ItemList();
const emailLabel = extractText(app.translator.trans('core.forum.forgot_password.email_placeholder'));
items.add(
'email',
<div className="Form-group">
<input
className="FormControl"
name="email"
type="email"
placeholder={emailLabel}
aria-label={emailLabel}
bidi={this.email}
disabled={this.loading}
/>
</div>,
50
);
items.add(
'submit',
<div className="Form-group Form-controls">
<Button className="Button Button--primary Button--block" type="submit" loading={this.loading}>
{app.translator.trans('core.forum.forgot_password.submit_button')}
</Button>
</div>,
-10
);
return items;
}
onsubmit(e: SubmitEvent) {
e.preventDefault();
this.loading = true;
app
.request({
method: 'POST',
url: app.forum.attribute('apiUrl') + '/forgot',
body: this.requestParams(),
errorHandler: this.onerror.bind(this),
})
.then(() => {
this.success = true;
this.alertAttrs = null;
})
.catch(() => {})
.then(this.loaded.bind(this));
}
requestParams(): Record<string, unknown> {
const data = {
email: this.email(),
};
return data;
}
onerror(error: RequestError) {
if (error.status === 404 && error.alert) {
error.alert.content = app.translator.trans('core.forum.forgot_password.not_found_message');
}
super.onerror(error);
}
}