2020-04-13 21:16:07 +02:00
|
|
|
// @flow
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { FormField, Form } from 'component/common/form';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import ErrorText from 'component/common/error-text';
|
|
|
|
import Card from 'component/common/card';
|
2020-07-07 09:36:13 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2020-04-13 21:16:07 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
user: ?User,
|
|
|
|
doToast: ({ message: string }) => void,
|
|
|
|
doUserPasswordSet: (string, ?string) => void,
|
|
|
|
doClearPasswordEntry: () => void,
|
|
|
|
passwordSetSuccess: boolean,
|
|
|
|
passwordSetError: ?string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function SettingAccountPassword(props: Props) {
|
|
|
|
const { user, doToast, doUserPasswordSet, passwordSetSuccess, passwordSetError, doClearPasswordEntry } = props;
|
|
|
|
const [oldPassword, setOldPassword] = useState('');
|
|
|
|
const [newPassword, setNewPassword] = useState('');
|
|
|
|
const [isAddingPassword, setIsAddingPassword] = useState(false);
|
|
|
|
const hasPassword = user && user.password_set;
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
doUserPasswordSet(newPassword, oldPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (passwordSetSuccess) {
|
|
|
|
setIsAddingPassword(false);
|
|
|
|
doToast({
|
|
|
|
message: __('Password updated successfully.'),
|
|
|
|
});
|
|
|
|
doClearPasswordEntry();
|
|
|
|
setOldPassword('');
|
|
|
|
setNewPassword('');
|
|
|
|
}
|
|
|
|
}, [passwordSetSuccess, setOldPassword, setNewPassword, doClearPasswordEntry, doToast]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
2020-08-26 22:28:33 +02:00
|
|
|
title={__('Account password')}
|
2020-06-24 13:22:52 +02:00
|
|
|
subtitle={hasPassword ? '' : __('You do not currently have a password set.')}
|
2020-04-13 21:16:07 +02:00
|
|
|
actions={
|
|
|
|
isAddingPassword ? (
|
|
|
|
<div>
|
2020-07-07 09:16:26 +02:00
|
|
|
<Form onSubmit={handleSubmit} className="section">
|
|
|
|
{hasPassword && (
|
|
|
|
<FormField
|
|
|
|
type="password"
|
|
|
|
name="setting_set_old_password"
|
|
|
|
label={__('Old Password')}
|
|
|
|
value={oldPassword}
|
|
|
|
onChange={e => setOldPassword(e.target.value)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<FormField
|
|
|
|
type="password"
|
|
|
|
name="setting_set_new_password"
|
|
|
|
label={__('New Password')}
|
|
|
|
value={newPassword}
|
|
|
|
onChange={e => setNewPassword(e.target.value)}
|
|
|
|
/>
|
|
|
|
|
2020-04-13 21:16:07 +02:00
|
|
|
<div className="section__actions">
|
2020-07-07 09:16:26 +02:00
|
|
|
<Button button="primary" type="submit" label={__('Set Password')} disabled={!newPassword} />
|
2020-07-07 09:36:13 +02:00
|
|
|
{hasPassword ? (
|
|
|
|
<Button button="link" label={__('Forgot Password?')} navigate={`/$/${PAGES.AUTH_PASSWORD_RESET}`} />
|
|
|
|
) : (
|
2020-07-07 09:16:26 +02:00
|
|
|
<Button button="link" label={__('Cancel')} onClick={() => setIsAddingPassword(false)} />
|
2020-07-03 05:59:24 +02:00
|
|
|
)}
|
2020-04-13 21:16:07 +02:00
|
|
|
</div>
|
2020-07-07 09:16:26 +02:00
|
|
|
</Form>
|
|
|
|
{passwordSetError && (
|
|
|
|
<div className="section">
|
|
|
|
<ErrorText>{passwordSetError}</ErrorText>
|
|
|
|
</div>
|
2020-04-13 21:16:07 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
button="primary"
|
|
|
|
label={hasPassword ? __('Update Your Password') : __('Add A Password')}
|
|
|
|
onClick={() => setIsAddingPassword(true)}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|