url-password.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. const { protectedLink } = this.props.query;
  58. if (!password) {
  59. return this.setState({
  60. error: 'Password must not be empty',
  61. });
  62. }
  63. this.setState({ error: '' });
  64. this.setState({ loading: true });
  65. return axios
  66. .post('/api/url/requesturl', { id: protectedLink, password })
  67. .then(({ data }) => window.location.replace(data.target))
  68. .catch(({ response }) =>
  69. this.setState({
  70. loading: false,
  71. error: response.data.error,
  72. })
  73. );
  74. }
  75. render() {
  76. if (!this.props.query.protectedLink) {
  77. return (
  78. <BodyWrapper>
  79. <Title>404 | Not found.</Title>
  80. </BodyWrapper>
  81. );
  82. }
  83. return (
  84. <BodyWrapper>
  85. <Title>Enter the password to access the URL.</Title>
  86. <Form onSubmit={this.requestUrl}>
  87. <TextInput type="password" placeholder="Password" onChange={this.updatePassword} small />
  88. <Button type="submit" icon={this.state.loading ? 'loader' : ''}>
  89. Go
  90. </Button>
  91. <Error>{this.state.error}</Error>
  92. </Form>
  93. </BodyWrapper>
  94. );
  95. }
  96. }
  97. UrlPasswordPage.propTypes = {
  98. query: PropTypes.shape({
  99. protectedLink: PropTypes.string,
  100. }),
  101. };
  102. UrlPasswordPage.defaultProps = {
  103. query: {},
  104. };
  105. export default UrlPasswordPage;