report.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from 'react';
  2. import styled from 'styled-components';
  3. import axios from 'axios';
  4. import BodyWrapper from '../components/BodyWrapper';
  5. import { authUser } from '../actions';
  6. import { REPORT_EMAIL } from '../config';
  7. import TextInput from '../components/TextInput';
  8. import Button from '../components/Button';
  9. const Wrapper = styled.div`
  10. width: 600px;
  11. max-width: 97%;
  12. display: flex;
  13. flex-direction: column;
  14. align-items: flex-start;
  15. `;
  16. const Form = styled.form`
  17. position: relative;
  18. display: flex;
  19. `;
  20. const Message = styled.p`
  21. position: absolute;
  22. left: 0;
  23. bottom: -48px;
  24. font-size: 14px;
  25. color: ${props => (props.type === 'error' ? 'red' : 'green')};
  26. @media only screen and (max-width: 448px) {
  27. bottom: -40px;
  28. font-size: 12px;
  29. }
  30. `;
  31. class ReportPage extends Component {
  32. constructor(props) {
  33. super(props);
  34. this.state = {
  35. loading: false,
  36. message: {
  37. text: '',
  38. type: '',
  39. },
  40. url: '',
  41. };
  42. this.onChange = this.onChange.bind(this);
  43. this.onSubmit = this.onSubmit.bind(this);
  44. }
  45. onChange(e) {
  46. const url = e.target.value;
  47. this.setState({ url });
  48. }
  49. async onSubmit(e) {
  50. e.preventDefault();
  51. this.setState({ loading: true });
  52. try {
  53. await axios.post('/api/url/report', { url: this.state.url });
  54. this.setState({
  55. loading: false,
  56. message: {
  57. type: 'success',
  58. text: "Thanks for the report, we'll take actions shortly.",
  59. },
  60. });
  61. } catch (error) {
  62. this.setState({
  63. loading: false,
  64. message: {
  65. type: 'error',
  66. text: error.response.data.error,
  67. },
  68. });
  69. }
  70. setTimeout(() => {
  71. this.setState({
  72. message: {
  73. type: '',
  74. text: '',
  75. },
  76. });
  77. }, 2000);
  78. }
  79. render() {
  80. const { loading, message, url } = this.state;
  81. return (
  82. <BodyWrapper>
  83. <Wrapper>
  84. <h3>Report abuse</h3>
  85. <p>
  86. Report abuses, malware and phishing links to the below email address. We will take
  87. actions shortly.
  88. </p>
  89. <p>{REPORT_EMAIL}</p>
  90. <p>Or use the form below.</p>
  91. <Form onSubmit={this.onSubmit}>
  92. <TextInput
  93. type="text"
  94. placeholder="URL"
  95. value={url}
  96. onChange={this.onChange}
  97. height={44}
  98. small
  99. />
  100. <Button type="submit" icon={loading ? 'loader' : ''}>
  101. Send report
  102. </Button>
  103. <Message type={message.type}>{message.text}</Message>
  104. </Form>
  105. </Wrapper>
  106. </BodyWrapper>
  107. );
  108. }
  109. }
  110. ReportPage.getInitialProps = ({ req, reduxStore }) => {
  111. const token = req && req.cookies && req.cookies.token;
  112. if (token && reduxStore) reduxStore.dispatch(authUser(token));
  113. return {};
  114. };
  115. export default ReportPage;