Meriadec Pillet
7 years ago
committed by
GitHub
11 changed files with 242 additions and 20 deletions
@ -0,0 +1,121 @@ |
|||
// @flow
|
|||
|
|||
import React, { PureComponent } from 'react' |
|||
import bcrypt from 'bcryptjs' |
|||
|
|||
import Box from 'components/base/Box' |
|||
import Button from 'components/base/Button' |
|||
import InputPassword from 'components/base/InputPassword' |
|||
import { ErrorMessageInput } from 'components/base/Input' |
|||
import { Modal, ModalContent, ModalBody, ModalTitle, ModalFooter } from 'components/base/Modal' |
|||
|
|||
import type { T } from 'types/common' |
|||
|
|||
type Props = { |
|||
t: T, |
|||
onClose: Function, |
|||
isPasswordEnabled: boolean, |
|||
currentPasswordHash: string, |
|||
onChangePassword: Function, |
|||
} |
|||
|
|||
type State = { |
|||
currentPassword: string, |
|||
incorrectPassword: boolean, |
|||
} |
|||
|
|||
const INITIAL_STATE = { |
|||
currentPassword: '', |
|||
incorrectPassword: false, |
|||
} |
|||
|
|||
class DisablePasswordModal extends PureComponent<Props, State> { |
|||
state = INITIAL_STATE |
|||
|
|||
disablePassword = (e: SyntheticEvent<HTMLFormElement>) => { |
|||
if (e) { |
|||
e.preventDefault() |
|||
} |
|||
|
|||
const { currentPassword } = this.state |
|||
const { isPasswordEnabled, currentPasswordHash, onChangePassword } = this.props |
|||
if (isPasswordEnabled) { |
|||
if (!bcrypt.compareSync(currentPassword, currentPasswordHash)) { |
|||
this.setState({ incorrectPassword: true }) |
|||
return |
|||
} |
|||
onChangePassword('') |
|||
} else { |
|||
onChangePassword('') |
|||
} |
|||
} |
|||
|
|||
handleInputChange = (key: string) => (value: string) => { |
|||
if (this.state.incorrectPassword) { |
|||
this.setState({ incorrectPassword: false }) |
|||
} |
|||
this.setState({ [key]: value }) |
|||
} |
|||
|
|||
handleReset = () => this.setState(INITIAL_STATE) |
|||
|
|||
render() { |
|||
const { t, isPasswordEnabled, onClose, ...props } = this.props |
|||
const { currentPassword, incorrectPassword } = this.state |
|||
return ( |
|||
<Modal |
|||
{...props} |
|||
onHide={this.handleReset} |
|||
onClose={onClose} |
|||
render={({ onClose }) => ( |
|||
<form onSubmit={this.disablePassword}> |
|||
<ModalBody onClose={onClose}> |
|||
<ModalTitle>{t('settings:profile.disablePasswordModalTitle')}</ModalTitle> |
|||
<ModalContent> |
|||
<Box ff="Museo Sans|Regular" color="dark" textAlign="center" mb={2} mt={3}> |
|||
{t('settings:profile.disablePasswordModalSubtitle')} |
|||
</Box> |
|||
<Box ff="Open Sans" color="smoke" fontSize={4} textAlign="center" px={4}> |
|||
{t('settings:profile.disablePasswordModalDesc')} |
|||
<Box px={7} mt={4} flow={3}> |
|||
{isPasswordEnabled && ( |
|||
<Box flow={1}> |
|||
<InputPassword |
|||
autoFocus |
|||
type="password" |
|||
placeholder={t('settings:profile.disablePasswordModalInput')} |
|||
id="password" |
|||
onChange={this.handleInputChange('currentPassword')} |
|||
value={currentPassword} |
|||
/> |
|||
{incorrectPassword && ( |
|||
<ErrorMessageInput> |
|||
{t('password:errorMessageIncorrectPassword')} |
|||
</ErrorMessageInput> |
|||
)} |
|||
</Box> |
|||
)} |
|||
</Box> |
|||
</Box> |
|||
</ModalContent> |
|||
<ModalFooter horizontal align="center" justify="flex-end" flow={2}> |
|||
<Button type="button" onClick={onClose}> |
|||
{t('common:cancel')} |
|||
</Button> |
|||
<Button |
|||
primary |
|||
onClick={this.disablePassword} |
|||
disabled={!currentPassword && !incorrectPassword} |
|||
> |
|||
{t('settings:profile.disablePasswordModalSave')} |
|||
</Button> |
|||
</ModalFooter> |
|||
</ModalBody> |
|||
</form> |
|||
)} |
|||
/> |
|||
) |
|||
} |
|||
} |
|||
|
|||
export default DisablePasswordModal |
@ -0,0 +1,12 @@ |
|||
// @flow
|
|||
|
|||
import React from 'react' |
|||
|
|||
export default ({ size, ...p }: { size: number }) => ( |
|||
<svg viewBox="0 0 16 16" height={size} width={size} {...p}> |
|||
<path |
|||
fill="currentColor" |
|||
d="M12.19 2.75H10a.75.75 0 0 1 0-1.5h4a.75.75 0 0 1 .75.75v4a.75.75 0 1 1-1.5 0V3.81L7.197 9.865a.75.75 0 0 1-1.06-1.061l6.052-6.053zm-.94 5.917a.75.75 0 1 1 1.5 0v4c0 1.15-.933 2.083-2.083 2.083H3.333a2.083 2.083 0 0 1-2.083-2.083V5.333c0-1.15.933-2.083 2.083-2.083h4a.75.75 0 1 1 0 1.5h-4a.583.583 0 0 0-.583.583v7.334c0 .322.261.583.583.583h7.334a.583.583 0 0 0 .583-.583v-4z" |
|||
/> |
|||
</svg> |
|||
) |
Loading…
Reference in new issue