url-password.js 2.6 KB

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