frontend/smart-hut/src/views/Forgot-password.js

155 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
2020-03-23 20:24:17 +00:00
import {
Button,
Form,
Grid,
Header,
Image,
Icon,
Message,
2020-05-12 13:18:33 +00:00
} from 'semantic-ui-react';
import { Redirect } from 'react-router-dom';
import { Forms } from '../remote';
2020-03-05 10:03:44 +00:00
2020-03-23 20:24:17 +00:00
export default class ForgotPass extends Component {
constructor(props) {
super(props);
this.state = {
2020-05-12 13:18:33 +00:00
user: '',
2020-03-23 20:24:17 +00:00
error: {
state: false,
2020-03-25 21:15:52 +00:00
message: [],
2020-03-23 20:24:17 +00:00
},
success: false,
2020-03-05 10:03:44 +00:00
};
2020-05-10 21:50:59 +00:00
this.handleChangePassword = this.handleChangePassword.bind(this);
2020-03-23 20:24:17 +00:00
}
2020-03-05 10:03:44 +00:00
2020-03-23 20:24:17 +00:00
onChangeHandler = (event) => {
2020-05-12 13:18:33 +00:00
const nam = event.target.name;
const val = event.target.value;
2020-03-23 20:24:17 +00:00
this.setState({ [nam]: val });
};
2020-03-05 10:03:44 +00:00
2020-03-23 20:24:17 +00:00
handleSendEmail = (e) => {
e.preventDefault();
2020-03-05 10:03:44 +00:00
Forms.submitInitResetPassword(this.state.user)
.then(() => this.setState({ success: true }))
2020-05-12 13:18:33 +00:00
.catch((err) => this.setState({ error: { state: true, message: err.messages } }));
2020-03-23 20:24:17 +00:00
};
2020-05-10 21:50:59 +00:00
handleChangePassword = (e) => {
if (this.state.confirmPassword !== this.state.password) {
this.setState({
error: {
state: true,
2020-05-12 13:18:33 +00:00
message: 'Passwords do not match.',
2020-05-10 21:50:59 +00:00
},
});
}
Forms.submitResetPassword(this.props.query.token, this.state.password)
.then(() => this.setState({ success: true }))
2020-05-12 13:18:33 +00:00
.catch((err) => this.setState({
error: { state: true, message: err.messages.join(' - ') },
}));
2020-05-10 21:50:59 +00:00
};
2020-03-23 20:24:17 +00:00
render() {
2020-05-10 21:50:59 +00:00
console.log(this.props);
2020-03-23 20:24:17 +00:00
if (this.state.success) {
return <Redirect to="sent-email" />;
}
return (
2020-05-12 13:18:33 +00:00
<>
<Button circular style={{ margin: '2em' }} href="/">
2020-03-23 20:24:17 +00:00
<Icon name="arrow alternate circle left" />
2020-05-12 13:18:33 +00:00
Go Home
{' '}
2020-03-23 20:24:17 +00:00
</Button>
<Grid
textAlign="center"
2020-05-12 13:18:33 +00:00
style={{ height: '70vh' }}
2020-03-23 20:24:17 +00:00
verticalAlign="middle"
>
<Grid.Column style={{ maxWidth: 450 }}>
<Header as="h2" color="blue" textAlign="center">
2020-05-12 13:18:33 +00:00
<Image src="img/logo.png" />
{' '}
Reset Password
2020-03-23 20:24:17 +00:00
</Header>
<Form
size="large"
2020-05-12 13:18:33 +00:00
style={{ marginTop: '2em' }}
2020-03-23 20:24:17 +00:00
error={this.state.error.state}
2020-03-05 10:03:44 +00:00
>
2020-03-25 21:15:52 +00:00
<Message error>
<Message.Header>Reset Password Error</Message.Header>
{this.state.error.message.map((e, i) => (
<span key={i}>
{e}
<br />
</span>
))}
</Message>
2020-05-12 13:18:33 +00:00
{this.props.type === 'FPassword1' ? (
<>
2020-05-10 21:50:59 +00:00
<Form.Input
icon="address card outline"
iconPosition="left"
placeholder="Enter your E-mail"
name="user"
type="text"
onChange={this.onChangeHandler}
required
/>
<Button
color="blue"
fluid
size="large"
onClick={this.handleSendEmail}
>
Send E-mail
</Button>
2020-05-12 13:18:33 +00:00
</>
2020-05-10 21:50:59 +00:00
) : (
2020-05-12 13:18:33 +00:00
<>
2020-05-10 21:50:59 +00:00
<Form.Input
icon="address card outline"
iconPosition="left"
placeholder="Reset your password"
name="password"
type="password"
onChange={this.onChangeHandler}
required
/>
<Form.Input
icon="address card outline"
iconPosition="left"
placeholder="Confirm Password"
name="confirmPassword"
type="password"
onChange={this.onChangeHandler}
required
/>
<Button
color="blue"
fluid
size="large"
onClick={this.handleChangePassword}
>
Confirm password
</Button>
2020-05-12 13:18:33 +00:00
</>
2020-05-10 21:50:59 +00:00
)}
2020-03-23 20:24:17 +00:00
</Form>
</Grid.Column>
</Grid>
2020-05-12 13:18:33 +00:00
</>
2020-03-23 20:24:17 +00:00
);
}
}