url-password.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import axios from 'axios';
  5. import BodyWrapper from '../components/BodyWrapper';
  6. import TextInput from '../components/TextInput';
  7. import Button from '../components/Button';
  8. const Title = styled.h3`
  9. font-size: 24px;
  10. font-weight: 300;
  11. text-align: center;
  12. @media only screen and (max-width: 448px) {
  13. font-size: 18px;
  14. }
  15. `;
  16. const Form = styled.form`
  17. position: relative;
  18. display: flex;
  19. align-items: center;
  20. `;
  21. const Error = styled.p`
  22. position: absolute;
  23. left: 0;
  24. bottom: -48px;
  25. font-size: 14px;
  26. color: red;
  27. @media only screen and (max-width: 448px) {
  28. bottom: -40px;
  29. font-size: 12px;
  30. }
  31. `;
  32. class UrlPasswordPage extends Component {
  33. static getInitialProps({ query }) {
  34. return { query };
  35. }
  36. constructor() {
  37. super();
  38. this.state = {
  39. error: '',
  40. loading: false,
  41. password: '',
  42. };
  43. this.updatePassword = this.updatePassword.bind(this);
  44. this.requestUrl = this.requestUrl.bind(this);
  45. }
  46. shouldComponentUpdate() {
  47. return true;
  48. }
  49. updatePassword(e) {
  50. this.setState({
  51. password: e.currentTarget.value,
  52. });
  53. }
  54. requestUrl(e) {
  55. e.preventDefault();
  56. const { password } = this.state;
  57. if (!password) {
  58. return this.setState({
  59. error: 'Password must not be empty',
  60. });
  61. }
  62. this.setState({ error: '' });
  63. this.setState({ loading: true });
  64. return axios
  65. .post('/api/url/requesturl', { id: this.props.query, password })
  66. .then(({ data }) => window.location.replace(data.target))
  67. .catch(({ response }) =>
  68. this.setState({
  69. loading: false,
  70. error: response.data.error,
  71. })
  72. );
  73. }
  74. render() {
  75. if (!this.props.query) {
  76. return (
  77. <BodyWrapper>
  78. <Title>404 | Not found.</Title>
  79. </BodyWrapper>
  80. );
  81. }
  82. return (
  83. <BodyWrapper>
  84. <Title>Enter the password to access the URL.</Title>
  85. <Form onSubmit={this.requestUrl}>
  86. <TextInput type="password" placeholder="Password" onChange={this.updatePassword} small />
  87. <Button type="submit" icon={this.state.loading ? 'loader' : ''}>
  88. Go
  89. </Button>
  90. <Error>{this.state.error}</Error>
  91. </Form>
  92. </BodyWrapper>
  93. );
  94. }
  95. }
  96. UrlPasswordPage.propTypes = {
  97. query: PropTypes.shape({
  98. id: PropTypes.string,
  99. }),
  100. };
  101. UrlPasswordPage.defaultProps = {
  102. query: null,
  103. };
  104. export default UrlPasswordPage;