// Copyright 2021 99cloud // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React, { Component } from 'react'; import { Input, Button } from 'antd'; import { Link } from 'react-router-dom'; import { InfoCircleFilled } from '@ant-design/icons'; import { getPasswordOtherRule } from 'utils/validate'; import { inject, observer } from 'mobx-react'; import { isEmpty } from 'lodash'; import { toJS } from 'mobx'; import globalUserStore from 'stores/keystone/user'; import Notify from 'components/Notify'; import SimpleForm from 'components/SimpleForm'; import styles from './index.less'; @inject('rootStore') @observer export default class Password extends Component { formRef = null; constructor(props) { super(props); this.init(); this.state = { error: false, message: '', loading: false, }; } componentDidMount() { const { rootStore: { routing } = {} } = this.props; if (!this.passwordData || isEmpty(this.passwordData)) { routing.push('/auth/login'); } } componentWillUnmount() { const { rootStore } = this.props; rootStore.setPasswordInfo(null); } get passwordData() { const { rootStore: { oldPassword } = {} } = this.props; return toJS(oldPassword) || {}; } get defaultValue() { const { oldPassword } = this.passwordData; return { oldPassword, }; } onFinish = (values) => { const { rootStore } = this.props; const { userId, oldPassword } = this.passwordData; const { password } = values; const body = { id: userId, password, original_password: oldPassword, }; this.setState({ loading: true, }); globalUserStore.changePasswordUser(body).then( () => { this.setState({ loading: false, }); Notify.success( t('Password changed successfully, please log in again.') ); rootStore.setPasswordInfo(null); rootStore.routing.push('/auth/login'); }, (err) => { const { reason: { message }, } = err; this.setState({ error: true, message, loading: false, }); } ); }; getCount = (word) => { try { return word.split('be unique is ')[1].split('.')[0]; } catch (e) { return 0; } }; getErrorWord = (word) => { if (!word) { return t('Reset failed, please retry'); } if ( word.indexOf( 'The new password cannot be identical to a previous password.' ) >= 0 ) { const num = this.getCount(word); if (!num) { return t('The password must not be the same as the previous'); } if (num === '2') { return t('The password must not be the same as the previous two'); } return t('The password must not be the same as the previous {num}', { num, }); } return t('Reset failed, please retry'); }; get formItems() { const { error, message, loading } = this.state; // const { password: oldPassword } = this.passwordData; const errorHint = this.getErrorWord(message); return [ { name: 'hint', render: () => (