-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChangePasswordForm.tsx
133 lines (120 loc) · 3.89 KB
/
ChangePasswordForm.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
import * as React from "react";
import { Store } from "@reduxjs/toolkit";
import { FetchErrorData } from "@thepalaceproject/web-opds-client/lib/interfaces";
import { RootState } from "../store";
import { connect } from "react-redux";
import ActionCreator from "../actions";
import LoadingIndicator from "@thepalaceproject/web-opds-client/lib/components/LoadingIndicator";
import ErrorMessage from "./ErrorMessage";
import EditableInput from "./EditableInput";
import { Form } from "library-simplified-reusable-components";
export interface ChangePasswordFormStateProps {
fetchError?: FetchErrorData;
isFetching?: boolean;
}
export interface ChangePasswordFormDispatchProps {
changePassword?: (data: FormData) => Promise<void>;
}
export interface ChangePasswordFormOwnProps {
store?: Store<RootState>;
csrfToken: string;
}
export interface ChangePasswordFormProps
extends ChangePasswordFormStateProps,
ChangePasswordFormDispatchProps,
ChangePasswordFormOwnProps {}
export interface ChangePasswordState {
success: boolean;
error: string | null;
}
export class ChangePasswordForm extends React.Component<
ChangePasswordFormProps,
ChangePasswordState
> {
private passwordRef = React.createRef<EditableInput>();
private confirmRef = React.createRef<EditableInput>();
constructor(props) {
super(props);
this.state = { success: false, error: null };
this.save = this.save.bind(this);
}
render(): JSX.Element {
const formContent = (
<fieldset key="change-password">
<legend className="visuallyHidden">Change admin's password</legend>
<EditableInput
elementType="input"
type="password"
disabled={this.props.isFetching}
name="password"
label="New Password"
ref={this.passwordRef}
required={true}
/>
<EditableInput
elementType="input"
type="password"
disabled={this.props.isFetching}
name="confirm_password"
label="Confirm New Password"
ref={this.confirmRef}
required={true}
/>
</fieldset>
);
return (
<main className="change-password-form">
<h2>Change Password</h2>
<Form
onSubmit={this.save}
content={formContent}
disableButton={this.props.isFetching}
buttonClass="left-align"
className="border change-password-form"
successText={this.state.success && "Password changed successfully"}
errorText={
(this.props.fetchError && (
<ErrorMessage error={this.props.fetchError} />
)) ||
this.state.error
}
loadingText={this.props.isFetching && <LoadingIndicator />}
></Form>
</main>
);
}
save(data: FormData) {
if (!(data.get("password") && data.get("confirm_password"))) {
this.setState({ success: false, error: "Fields cannot be blank." });
} else if (data.get("password") !== data.get("confirm_password")) {
this.setState({ success: false, error: "Passwords do not match." });
} else {
this.props.changePassword(data).then(() => {
this.setState({ success: true, error: null });
});
}
}
}
function mapStateToProps(state, ownProps) {
return {
fetchError:
state.editor.changePassword && state.editor.changePassword.fetchError,
isFetching:
state.editor.changePassword && state.editor.changePassword.isFetching,
};
}
function mapDispatchToProps(dispatch, ownProps) {
const actions = new ActionCreator(null, ownProps.csrfToken);
return {
changePassword: (data: FormData) => dispatch(actions.changePassword(data)),
};
}
const ConnectedChangePasswordForm = connect<
ChangePasswordFormStateProps,
ChangePasswordFormDispatchProps,
ChangePasswordFormOwnProps
>(
mapStateToProps,
mapDispatchToProps
)(ChangePasswordForm);
export default ConnectedChangePasswordForm;