Untitled
unknown
plain_text
3 years ago
11 kB
12
Indexable
import React from 'react';
import PropTypes from 'prop-types';
import * as Yup from 'yup';
import clsx from 'clsx';
import { Modal } from 'antd';
import Tooltip from '../../elements/Tooltip';
import { ButtonFilled } from '@telkomdesign/tedis-reactjs-component/lib';
import TextInput from '../../elements/TextInput';
import { REGEX, ICONS } from '../../../configs';
import { Formik } from 'formik';
import { postData, endpoints } from '../../../configs/Api';
import get from 'lodash.get';
class ModalPassword extends React.Component {
constructor(props) {
super(props);
this.form = React.createRef();
this.state = {
focused: '', visible: false, initialValues: { newPassword: '', oldPassword: '' }
};
}
componentDidUpdate(prevProps) {
const { profilPT, toggleModal, newPassword, oldPassword, onError } = this.props;
const { onError: prevOnError } = prevProps;
if (toggleModal !== prevProps.toggleModal) {
this._handleToggleModal();
}
if (onError !== prevOnError && onError) {
this.setState({ initialValues: { newPassword, oldPassword } });
}
if(this.form.current !== null && profilPT?.error?.code === 409
&& profilPT?.error?.message?.text === 'Sandi Baru tidak boleh sama dengan Sandi yang sudah pernah Anda ubah sebelumnya.') {
this._handleNegativeCase();
}
}
_handleNegativeCase = () => {
this.form.current.setFieldError('newPassword', 'Sandi Baru tidak boleh sama dengan Sandi yang sudah pernah Anda ubah sebelumnya.');
}
_handleBlurPassword = () => {
this._handleSetFocus('');
}
_schemaChangePassword = () => {
return Yup.object().shape({
oldPassword: Yup.string()
.required('')
.matches(REGEX.idealPassword, 'errorAll')
.matches(REGEX.checkUpperLowerCase, 'errorAll')
.min(8, 'errorAll'),
newPassword: Yup.string()
.required('')
.matches(REGEX.idealPassword, 'errorAll')
.matches(REGEX.checkUpperLowerCase, 'errorAll')
.min(8, 'errorAll')
});
}
_renderTooltipPassword = (helpers) => {
const { classes } = this.props;
const { values, touched, errors } = helpers;
const { newPassword } = values;
return (
<div className={classes.tooltipPassword}>
<p className={clsx('', {
['passed']: newPassword && newPassword.length > 7,
['failed']: touched.newPassword && errors.newPassword === 'errorAll'
})}>
{newPassword && newPassword.length > 7 ?
<img src={ICONS.checklistCircle} /> : <b>•</b>}
Minimal terdiri dari 8 karakter
</p>
<p className={clsx('', {
['passed']: REGEX.checkUpperLowerCase.test(newPassword),
['failed']: touched.newPassword && errors.newPassword === 'errorAll' ||
touched.newPassword && errors.newPassword === 'error2'
})}>
{REGEX.checkUpperLowerCase.test(newPassword) ?
<img src={ICONS.checklistCircle} /> : <b>•</b>}
Kombinasi huruf kapital dan huruf kecil
</p>
<p className={clsx('', {
['passed']: REGEX.idealPassword.test(newPassword),
['failed']: touched.newPassword && errors.newPassword === 'errorAll' ||
touched.newPassword && errors.newPassword === 'error3'
})}>
{REGEX.idealPassword.test(newPassword) ? <img src={ICONS.checklistCircle} /> : <b>•</b>}
Minimal terdiri dari 1 angka atau simbol
</p>
</div>
);
}
_renderTooltipOldPassword = (helpers) => {
const { classes } = this.props;
const { values, touched, errors } = helpers;
const { oldPassword } = values;
return (
<div className={classes.tooltipPassword}>
<p className={clsx('', {
['passed']: oldPassword && oldPassword.length > 7,
['failed']: touched.oldPassword && errors.oldPassword === 'errorAll'
})}>
{oldPassword && oldPassword.length > 7 ?
<img src={ICONS.checklistCircle} /> : <b>•</b>}
Minimal terdiri dari 8 karakter
</p>
<p className={clsx('', {
['passed']: REGEX.checkUpperLowerCase.test(oldPassword),
['failed']: touched.oldPassword && errors.oldPassword === 'errorAll' ||
touched.oldPassword && errors.oldPassword === 'error2'
})}>
{REGEX.checkUpperLowerCase.test(oldPassword) ?
<img src={ICONS.checklistCircle} /> : <b>•</b>}
Kombinasi huruf kapital dan huruf kecil
</p>
<p className={clsx('', {
['passed']: REGEX.idealPassword.test(oldPassword),
['failed']: touched.oldPassword && errors.oldPassword === 'errorAll' ||
touched.oldPassword && errors.oldPassword === 'error3'
})}>
{REGEX.idealPassword.test(oldPassword) ? <img src={ICONS.checklistCircle} /> : <b>•</b>}
Minimal terdiri dari 1 angka atau simbol
</p>
</div>
);
}
_handleChange = ({ target: { name, value } }) => {
const { setFieldValue, touched,
setFieldTouched } = this.form.current;
setFieldValue(name, value);
if (!touched[name]) setFieldTouched(name, true);
}
_handleCheckError = () => {
const { visible } = this.state;
const { onError } = this.props;
if (visible && onError ) {
console.log('terpanggi');
if (!this.form.current.error?.newPassword) {
console.log('masuk if');
// this.form.current.setFieldError('newPassword',
// 'Sandi Baru tidak boleh sama dengan Sandi yang sudah pernah Anda ubah sebelumnya');
}
}
}
_renderForm = (helpers) => {
const { handleSubmit: _handleSubmit,
touched, errors, values } = helpers;
const { oldPassword, newPassword } = values;
const { classes } = this.props;
const tooltipVisible = Object.keys(errors).length > 0 && errors.newPassword === 'errorAll'
|| this.state.focused === 'newPassword' && !values.newPassword;
const tooltipVisible1 = Object.keys(errors).length > 0 && errors.oldPassword === 'errorAll'
|| this.state.focused === 'oldPassword' && !values.oldPassword;
return (
<form className={clsx('wrapperChangePass', { 'error-new': tooltipVisible, 'error-old': tooltipVisible1 })} onSubmit={_handleSubmit}>
<p className="modal-title">Ubah Sandi</p>
<p className="modal-description">Silakan isi kolom berikut untuk mengubah sandi. </p>
<Tooltip
title={this._renderTooltipOldPassword(helpers)}
placement="bottomLeft"
type="password"
className={classes.tooltipBox}
zIndex={1300}
visible={tooltipVisible1}>
<TextInput
onBlur={this._handleBlurPassword}
name={'oldPassword'}
label="Sandi Lama"
type={'password'}
value={oldPassword}
hinterror={!tooltipVisible1 && touched.oldPassword && errors.oldPassword}
onChange={this._handleChange}
onFocus={this._handleSetFocus.bind(this, 'oldPassword')}
/>
</Tooltip>
<Tooltip
title={this._renderTooltipPassword(helpers)}
placement="bottomLeft"
type="password"
className={classes.tooltipBox}
zIndex={1300}
visible={tooltipVisible}>
<TextInput
onBlur={this._handleBlurPassword}
name={'newPassword'}
label="Sandi Baru"
type={'password'}
value={newPassword}
hinterror={!tooltipVisible && touched.newPassword && errors.newPassword}
onChange={this._handleChange}
onFocus={this._handleSetFocus.bind(this, 'newPassword')}
/>
</Tooltip>
<ButtonFilled
className={classes.saveBtn}
size="48"
type="submit"
disabled={!values.oldPassword || !values.newPassword || errors?.newPassword ||
errors?.oldPassword}
>
Simpan Perubahan
</ButtonFilled>
</form>
);
}
_handleSetFocus = (focus) => {
this.setState({
focused: focus,
});
};
_handleToggleModal = () => this.setState({ visible: !this.state.visible });
_handleCheckValidPassword = async () => {
const { auth: { pt }, onSubmit,
actions: { requestProfilePt } } = this.props, { values, setFieldError } = this.form.current;
requestProfilePt({ token: get(pt, 'token') });
try {
await postData({
endpoint: endpoints.postLoginPerguruanTinggi,
payload: { email: pt.profile.email, password: values.oldPassword },
});
if(values.oldPassword !== values.newPassword) {
this.setState({ focused: '' }, () => {
onSubmit(values);
});
}else(setFieldError('newPassword', 'Sandi Baru sama dengan Sandi Lama'));
} catch (error) {
if (error?.response?.data?.message?.type === 'password') {
setFieldError('oldPassword', 'Sandi Lama tidak sesuai');
}
}
}
render() {
const { classes, onClose } = this.props;
const { visible, initialValues } = this.state;
return (
<>
<Modal
visible={visible}
closable
destroyOnClose
closeIcon={<img src={ICONS.closeIcon} />}
onCancel={onClose}
centered
footer={false}
zIndex={1300}
className={classes.wrapperModal}
>
<Formik
innerRef={this.form}
initialValues={initialValues}
validationSchema={this._schemaChangePassword}
component={this._renderForm}
onSubmit={this._handleCheckValidPassword}
enableReinitialize={true}
/>
</Modal>
</>
);
}
}
ModalPassword.defaultProps = {
classes: {},
onClose: () => null,
toggleModal: false,
onSubmit: () => null,
newPassword: '',
oldPassword: '',
onError: false
};
ModalPassword.propTypes = {
auth: PropTypes.object.isRequired,
profilPT: PropTypes.object.isRequired,
classes: PropTypes.object,
visible: PropTypes.bool.isRequired,
onClose: PropTypes.func,
onSubmit: PropTypes.func,
toggleModal: PropTypes.bool,
actions: PropTypes.object.isRequired,
newPassword: PropTypes.string,
oldPassword: PropTypes.string,
onError: PropTypes.bool
};
export default ModalPassword;Editor is loading...