diff --git a/webapp/src/pages/changePasswordPage.scss b/webapp/src/pages/changePasswordPage.scss index c0b0b6cf1..5f9b38dcf 100644 --- a/webapp/src/pages/changePasswordPage.scss +++ b/webapp/src/pages/changePasswordPage.scss @@ -11,6 +11,13 @@ flex-direction: column; box-shadow: rgba(var(--main-fg), 0.1) 0px 0px 0px 1px, rgba(var(--main-fg), 0.3) 0px 4px 8px; + form { + display: flex; + flex-direction: column; + align-items: flex-start; + justify-content: center; + } + @media screen and (max-width: 430px) { position: fixed; top: 0; @@ -46,7 +53,7 @@ } } - > .Button { + form > .Button { margin-top: 10px; margin-bottom: 20px; min-height: 38px; diff --git a/webapp/src/pages/changePasswordPage.tsx b/webapp/src/pages/changePasswordPage.tsx index b8486e0a2..422a456b5 100644 --- a/webapp/src/pages/changePasswordPage.tsx +++ b/webapp/src/pages/changePasswordPage.tsx @@ -1,112 +1,97 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React from 'react' - -import { - withRouter, - RouteComponentProps, - Link, -} from 'react-router-dom' +import React, {useState, useContext} from 'react' +import {Link} from 'react-router-dom' import Button from '../widgets/buttons/button' import client from '../octoClient' import './changePasswordPage.scss' -import {UserContext} from '../user' +import {UserContext, IUser} from '../user' -type Props = RouteComponentProps +const ChangePasswordPage = React.memo(() => { + const [oldPassword, setOldPassword] = useState('') + const [newPassword, setNewPassword] = useState('') + const [errorMessage, setErrorMessage] = useState('') + const [succeeded, setSucceeded] = useState(false) + const user = useContext(UserContext) -type State = { - oldPassword: string - newPassword: string - errorMessage?: string - succeeded: boolean -} - -class ChangePasswordPage extends React.PureComponent { - state: State = { - oldPassword: '', - newPassword: '', - succeeded: false, - } - - private handleSubmit = async (userId: string): Promise => { - const response = await client.changePassword(userId, this.state.oldPassword, this.state.newPassword) - if (response.code === 200) { - this.setState({oldPassword: '', newPassword: '', errorMessage: undefined, succeeded: true}) - } else { - this.setState({errorMessage: `Change password failed: ${response.json?.error}`}) - } - } - - render(): React.ReactNode { + if (!user) { return (
{'Change Password'}
- - - {(user) => { - if (user) { - return (<> -
- this.setState({oldPassword: e.target.value, errorMessage: undefined})} - onKeyPress={(e) => this.onKeyPress(e, user.id)} - /> -
-
- this.setState({newPassword: e.target.value, errorMessage: undefined})} - onKeyPress={(e) => this.onKeyPress(e, user.id)} - /> -
- - {this.state.errorMessage && -
- {this.state.errorMessage} -
- } - {this.state.succeeded && - {'Password changed, click to continue.'} - } - {!this.state.succeeded && - {'Cancel'} - } - ) - } - return ( - {'Log in first'} - ) - }} -
+ {'Log in first'}
) } - private onKeyPress = (e: React.KeyboardEvent, userId: string) => { - if (!(e.metaKey || e.ctrlKey) && !e.shiftKey && e.key === 'Enter') { - this.handleSubmit(userId) - e.preventDefault() - return false + const handleSubmit = async (userId: string): Promise => { + const response = await client.changePassword(userId, oldPassword, newPassword) + if (response.code === 200) { + setOldPassword('') + setNewPassword('') + setErrorMessage('') + setSucceeded(true) + } else { + setErrorMessage(`Change password failed: ${response.json?.error}`) } - - return true } -} -export default withRouter(ChangePasswordPage) + return ( +
+
{'Change Password'}
+
{ + e.preventDefault() + handleSubmit(user.id) + }} + > +
+ { + setOldPassword(e.target.value) + setErrorMessage('') + }} + /> +
+
+ { + setNewPassword(e.target.value) + setErrorMessage('') + }} + /> +
+ +
+ {errorMessage && +
+ {errorMessage} +
+ } + {succeeded && + {'Password changed, click to continue.'} + } + {!succeeded && + {'Cancel'} + } +
+ ) +}) + +export default ChangePasswordPage