react/src/pages/ResetPassword.js
import React from 'react';
import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';
import CssBaseline from '@material-ui/core/CssBaseline';
import TextField from '@material-ui/core/TextField';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';
import { Password } from '../actions'
import { useDispatch } from 'react-redux'
import logo from '../logo.svg';
import routes from '../routes';
import { reverse as url } from 'named-urls'
import { get } from 'lodash'
const useStyles = makeStyles((theme) => ({
paper: {
marginTop: theme.spacing(8),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
},
avatar: {
margin: theme.spacing(1),
backgroundColor: theme.palette.secondary.main,
},
form: {
width: '100%', // Fix IE 11 issue.
marginTop: theme.spacing(1),
},
submit: {
margin: theme.spacing(3, 0, 2),
},
}));
const useFormField = (initialValue: string = "") => {
const [value, setValue] = React.useState(initialValue);
const onChange = React.useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => setValue(e.target.value),
[]
);
return { value, onChange };
};
export default function ResetPassword(props) {
const classes = useStyles();
const reset_password_token = get(props, "match.params.reset_password_token", "")
const passwordField = useFormField();
const passwordConfirmationField = useFormField();
const dispatch = useDispatch()
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
dispatch(Password.update(reset_password_token, passwordField.value, passwordConfirmationField.value));
}
return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<img src={logo} alt="Logo"/>
<Typography component="h1" variant="h2">
Archivist
</Typography>
<Typography component="h2" variant="h5">
Reset your password
</Typography>
<form className={classes.form} noValidate onSubmit={handleSubmit}>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
{...passwordField}
/>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password_confirmation"
label="Password Confirmation"
type="password"
id="password"
{...passwordConfirmationField}
/>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
>
Reset Password
</Button>
</form>
</div>
or <Link to={url(routes.login)}>Login</Link> here
</Container>
);
}