changePasswordPage.tsx
TLDR
This file, changePasswordPage.tsx
, contains a React functional component called ChangePasswordPage
. It renders a form that allows users to change their password. The form takes inputs for the current and new password, and it includes a submit button. The component handles form submission and displays error messages or success messages based on the response from the API.
Methods
This file does not contain any separate methods.
Classes
This file does not contain any separate classes.
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useState} from 'react'
import {Link} from 'react-router-dom'
import Button from '../widgets/buttons/button'
import client from '../octoClient'
import './changePasswordPage.scss'
import {IUser} from '../user'
import {useAppSelector} from '../store/hooks'
import {getMe} from '../store/users'
const ChangePasswordPage = () => {
const [oldPassword, setOldPassword] = useState('')
const [newPassword, setNewPassword] = useState('')
const [errorMessage, setErrorMessage] = useState('')
const [succeeded, setSucceeded] = useState(false)
const user = useAppSelector<IUser|null>(getMe)
if (!user) {
return (
<div className='ChangePasswordPage'>
<div className='title'>{'Change Password'}</div>
<Link to='/login'>{'Log in first'}</Link>
</div>
)
}
const handleSubmit = async (userId: string): Promise<void> => {
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 (
<div className='ChangePasswordPage'>
<div className='title'>{'Change Password'}</div>
<form
onSubmit={(e: React.FormEvent) => {
e.preventDefault()
handleSubmit(user.id)
}}
>
<div className='oldPassword'>
<input
id='login-oldpassword'
type='password'
placeholder={'Enter current password'}
value={oldPassword}
onChange={(e) => {
setOldPassword(e.target.value)
setErrorMessage('')
}}
/>
</div>
<div className='newPassword'>
<input
id='login-newpassword'
type='password'
placeholder={'Enter new password'}
value={newPassword}
onChange={(e) => {
setNewPassword(e.target.value)
setErrorMessage('')
}}
/>
</div>
<Button
filled={true}
submit={true}
>
{'Change password'}
</Button>
</form>
{errorMessage &&
<div className='error'>
{errorMessage}
</div>
}
{succeeded &&
<Link
className='succeeded'
to='/'
>{'Password changed, click to continue.'}</Link>
}
{!succeeded &&
<Link to='/'>{'Cancel'}</Link>
}
</div>
)
}
export default React.memo(ChangePasswordPage)