Skip to content

Commit e1cb13d

Browse files
committed
refactor(auth): migrate change password to antd v4
see #250
1 parent b22ccd0 commit e1cb13d

File tree

1 file changed

+97
-146
lines changed
  • src/Auth/components/ChangePassword

1 file changed

+97
-146
lines changed
+97-146
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,45 @@
1-
import React, { Component } from 'react';
2-
import { Form } from '@ant-design/compatible';
3-
import '@ant-design/compatible/assets/index.css';
4-
import { Input, Button } from 'antd';
1+
import React, { useState } from 'react';
2+
import { Input, Button, Form } from 'antd';
53
import PropTypes from 'prop-types';
64
import { Connect, putFocalPerson } from '@codetanzania/ewea-api-states';
75
import { notifyError, notifySuccess } from '../../../util';
86

9-
/**
10-
* @class
11-
* @name ChangePasswordForm
12-
* @description ChangePassword component which shows change password form
13-
*
14-
* @version 0.1.0
15-
* @since 0.1.0
16-
*/
17-
class ChangePasswordForm extends Component {
18-
// eslint-disable-next-line react/state-in-constructor
19-
state = {
20-
confirmDirty: false,
21-
};
22-
23-
/**
24-
* @function
25-
* @name handleSubmit
26-
* @description Handle submit action for change password
27-
*
28-
* @param {object} event submit event
29-
* @returns {undefined}
30-
*
31-
* @version 0.1.0
32-
* @since 0.1.0
33-
*/
34-
handleSubmit = (event) => {
35-
event.preventDefault();
36-
37-
const {
38-
form: { validateFieldsAndScroll },
39-
} = this.props;
40-
41-
validateFieldsAndScroll((err, values) => {
42-
const { user, onCancel } = this.props;
43-
if (!err) {
44-
const updatedUser = { ...user, password: values.password };
7+
const formItemLayout = {
8+
labelCol: {
9+
xs: { span: 24 },
10+
sm: { span: 24 },
11+
md: { span: 24 },
12+
lg: { span: 24 },
13+
xl: { span: 24 },
14+
xxl: { span: 24 },
15+
},
16+
wrapperCol: {
17+
xs: { span: 24 },
18+
sm: { span: 24 },
19+
md: { span: 24 },
20+
lg: { span: 24 },
21+
xl: { span: 24 },
22+
xxl: { span: 24 },
23+
},
24+
};
4525

46-
putFocalPerson(
47-
updatedUser,
48-
() => {
49-
notifySuccess('Password was changed successfully');
50-
onCancel();
51-
},
52-
() => {
53-
notifyError(
54-
'Something occurred while changing your password, please try again!'
55-
);
56-
}
26+
const ChangePasswordForm = ({ user, posting, onCancel }) => {
27+
const [isConfirmDirty, setConfirmDirty] = useState(false);
28+
const [form] = Form.useForm();
29+
const onFinish = (values) => {
30+
const updatedUser = { ...user, password: values.password };
31+
putFocalPerson(
32+
updatedUser,
33+
() => {
34+
notifySuccess('Password was changed successfully');
35+
onCancel();
36+
},
37+
() => {
38+
notifyError(
39+
'Something occurred while changing your password, please try again!'
5740
);
5841
}
59-
});
42+
);
6043
};
6144

6245
/**
@@ -70,11 +53,9 @@ class ChangePasswordForm extends Component {
7053
* @version 0.1.0
7154
* @since 0.1.0
7255
*/
73-
handleConfirmBlur = (event) => {
56+
const handleConfirmBlur = (event) => {
7457
const { value } = event.target;
75-
this.setState((prevState) => ({
76-
confirmDirty: prevState || !!value,
77-
}));
58+
setConfirmDirty(isConfirmDirty || !!value);
7859
};
7960

8061
/**
@@ -90,10 +71,9 @@ class ChangePasswordForm extends Component {
9071
* @version 0.1.0
9172
* @since 0.1.0
9273
*/
93-
compareToFirstPassword = (rule, value, callback) => {
94-
const { form } = this.props;
74+
const compareToFirstPassword = (rule, value, callback) => {
9575
if (value && value !== form.getFieldValue('password')) {
96-
callback('Two passwords that you enter is inconsistent!');
76+
callback('Two passwords that you enter are not the same!');
9777
} else {
9878
callback();
9979
}
@@ -112,92 +92,66 @@ class ChangePasswordForm extends Component {
11292
* @version 0.1.0
11393
* @since 0.1.0
11494
*/
115-
validateToNextPassword = (rule, value, callback) => {
116-
const { confirmDirty } = this.state;
117-
const { form } = this.props;
118-
if (value && confirmDirty) {
119-
form.validateFields(['confirm'], { force: true });
95+
const validateToNextPassword = (rule, value, callback) => {
96+
if (value && isConfirmDirty) {
97+
form.validateFields(['confirmPassword'], { force: true });
12098
}
12199
callback();
122100
};
123101

124-
render() {
125-
const {
126-
form: { getFieldDecorator },
127-
onCancel,
128-
posting,
129-
} = this.props;
130-
131-
const formItemLayout = {
132-
labelCol: {
133-
xs: { span: 24 },
134-
sm: { span: 24 },
135-
md: { span: 24 },
136-
lg: { span: 24 },
137-
xl: { span: 24 },
138-
xxl: { span: 24 },
139-
},
140-
wrapperCol: {
141-
xs: { span: 24 },
142-
sm: { span: 24 },
143-
md: { span: 24 },
144-
lg: { span: 24 },
145-
xl: { span: 24 },
146-
xxl: { span: 24 },
147-
},
148-
};
149-
return (
150-
// eslint-disable-next-line react/jsx-props-no-spreading
151-
<Form {...formItemLayout} onSubmit={this.handleSubmit}>
152-
{/* New Password input */}
153-
<Form.Item label="New Password" hasFeedback>
154-
{getFieldDecorator('password', {
155-
rules: [
156-
{
157-
required: true,
158-
message: 'Please input your new password!',
159-
},
160-
{
161-
validator: this.validateToNextPassword,
162-
},
163-
],
164-
})(<Input.Password />)}
165-
</Form.Item>
166-
{/* end new password input */}
102+
return (
103+
// eslint-disable-next-line react/jsx-props-no-spreading
104+
<Form form={form} {...formItemLayout} onFinish={onFinish}>
105+
{/* New Password input */}
106+
<Form.Item
107+
label="New Password"
108+
name="password"
109+
rules={[
110+
{
111+
required: true,
112+
message: 'Please input your new password!',
113+
},
114+
{ validator: validateToNextPassword },
115+
]}
116+
hasFeedback
117+
>
118+
<Input.Password />
119+
</Form.Item>
120+
{/* end new password input */}
167121

168-
{/* confirm password input */}
169-
<Form.Item label="Confirm Password" hasFeedback>
170-
{getFieldDecorator('confirm', {
171-
rules: [
172-
{
173-
required: true,
174-
message: 'Please confirm your password!',
175-
},
176-
{
177-
validator: this.compareToFirstPassword,
178-
},
179-
],
180-
})(<Input.Password onBlur={this.handleConfirmBlur} />)}
181-
</Form.Item>
182-
{/* end confirm password input */}
122+
{/* confirm password input */}
123+
<Form.Item
124+
label="Confirm Password"
125+
name="confirmPassword"
126+
rules={[
127+
{
128+
required: true,
129+
message: 'Please confirm your password!',
130+
},
131+
{ validator: compareToFirstPassword },
132+
]}
133+
hasFeedback
134+
>
135+
<Input.Password onBlur={handleConfirmBlur} />
136+
</Form.Item>
137+
{/* end confirm password input */}
183138

184-
{/* form actions */}
185-
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
186-
<Button onClick={onCancel}>Cancel</Button>
187-
<Button
188-
style={{ marginLeft: 8 }}
189-
type="primary"
190-
htmlType="submit"
191-
loading={posting}
192-
>
193-
Change Password
194-
</Button>
195-
</Form.Item>
196-
{/* end form actions */}
197-
</Form>
198-
);
199-
}
200-
}
139+
{/* form actions */}
140+
<Form.Item wrapperCol={{ span: 24 }} style={{ textAlign: 'right' }}>
141+
<Button onClick={onCancel}>Cancel</Button>
142+
<Button
143+
style={{ marginLeft: 8 }}
144+
type="primary"
145+
htmlType="submit"
146+
loading={posting}
147+
>
148+
Change Password
149+
</Button>
150+
</Form.Item>
151+
{/* end form actions */}
152+
</Form>
153+
);
154+
};
201155

202156
ChangePasswordForm.propTypes = {
203157
form: PropTypes.shape({
@@ -214,10 +168,7 @@ ChangePasswordForm.propTypes = {
214168
posting: PropTypes.bool.isRequired,
215169
};
216170

217-
export default Connect(
218-
Form.create({ name: 'changepassword' })(ChangePasswordForm),
219-
{
220-
user: 'app.party',
221-
posting: 'focalPeople.posting',
222-
}
223-
);
171+
export default Connect(ChangePasswordForm, {
172+
user: 'app.party',
173+
posting: 'focalPeople.posting',
174+
});

0 commit comments

Comments
 (0)