| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import React, { Component } from 'react';
- import PropTypes from 'prop-types';
- import withRedux from 'next-redux-wrapper';
- import styled from 'styled-components';
- import axios from 'axios';
- import initialState from '../store';
- import BodyWrapper from '../components/BodyWrapper';
- import TextInput from '../components/TextInput';
- import Button from '../components/Button';
- const Title = styled.h3`
- font-size: 24px;
- font-weight: 300;
- text-align: center;
- @media only screen and (max-width: 448px) {
- font-size: 18px;
- }
- `;
- const Form = styled.form`
- position: relative;
- display: flex;
- align-items: center;
- `;
- const Error = styled.p`
- position: absolute;
- left: 0;
- bottom: -48px;
- font-size: 14px;
- color: red;
- @media only screen and (max-width: 448px) {
- bottom: -40px;
- font-size: 12px;
- }
- `;
- class UrlPasswordPage extends Component {
- static getInitialProps({ query }) {
- return { query };
- }
- constructor() {
- super();
- this.state = {
- error: '',
- loading: false,
- password: '',
- };
- this.updatePassword = this.updatePassword.bind(this);
- this.requestUrl = this.requestUrl.bind(this);
- }
- shouldComponentUpdate() {
- return true;
- }
- updatePassword(e) {
- this.setState({
- password: e.currentTarget.value,
- });
- }
- requestUrl(e) {
- e.preventDefault();
- const { password } = this.state;
- if (!password) {
- return this.setState({
- error: 'Password must not be empty',
- });
- }
- this.setState({ error: '' });
- this.setState({ loading: true });
- return axios
- .post('/api/url/requesturl', { id: this.props.query, password })
- .then(({ data }) => window.location.replace(data.target))
- .catch(({ response }) =>
- this.setState({
- loading: false,
- error: response.data.error,
- })
- );
- }
- render() {
- if (!this.props.query) {
- return (
- <BodyWrapper>
- <Title>404 | Not found.</Title>
- </BodyWrapper>
- );
- }
- return (
- <BodyWrapper>
- <Title>Enter the password to access the URL.</Title>
- <Form onSubmit={this.requestUrl}>
- <TextInput placeholder="Password" onChange={this.updatePassword} small />
- <Button type="submit" icon={this.state.loading ? 'loader' : ''}>
- Go
- </Button>
- <Error>{this.state.error}</Error>
- </Form>
- </BodyWrapper>
- );
- }
- }
- UrlPasswordPage.propTypes = {
- query: PropTypes.shape({
- id: PropTypes.string,
- }),
- };
- UrlPasswordPage.defaultProps = {
- query: null,
- };
- export default withRedux(initialState)(UrlPasswordPage);
|