ShortenerOptions.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import Checkbox from '../Checkbox';
  5. import TextInput from '../TextInput';
  6. import { fadeIn } from '../../helpers/animations';
  7. const Wrapper = styled.div`
  8. position: absolute;
  9. top: 74px;
  10. left: 0;
  11. display: flex;
  12. flex-direction: column;
  13. align-self: flex-start;
  14. justify-content: flex-start;
  15. z-index: 2;
  16. @media only screen and (max-width: 448px) {
  17. width: 100%;
  18. top: 56px;
  19. }
  20. `;
  21. const CheckboxWrapper = styled.div`
  22. display: flex;
  23. @media only screen and (max-width: 448px) {
  24. justify-content: center;
  25. }
  26. `;
  27. const InputWrapper = styled.div`
  28. display: flex;
  29. align-items: center;
  30. @media only screen and (max-width: 448px) {
  31. flex-direction: column;
  32. align-items: flex-start;
  33. > * {
  34. margin-bottom: 16px;
  35. }
  36. }
  37. `;
  38. const Label = styled.label`
  39. font-size: 18px;
  40. margin-right: 16px;
  41. animation: ${fadeIn} 0.5s ease-out;
  42. @media only screen and (max-width: 448px) {
  43. font-size: 14px;
  44. margin-right: 8px;
  45. }
  46. `;
  47. class ShortenerOptions extends Component {
  48. constructor() {
  49. super();
  50. this.state = {
  51. customurlCheckbox: false,
  52. passwordCheckbox: false,
  53. };
  54. this.handleCheckbox = this.handleCheckbox.bind(this);
  55. }
  56. shouldComponentUpdate(nextProps, nextState) {
  57. const { customurlCheckbox, passwordCheckbox } = this.state;
  58. return (
  59. this.props.isAuthenticated !== nextProps.isAuthenticated ||
  60. customurlCheckbox !== nextState.customurlCheckbox ||
  61. passwordCheckbox !== nextState.passwordCheckbox
  62. );
  63. }
  64. handleCheckbox(e) {
  65. e.preventDefault();
  66. if (!this.props.isAuthenticated) {
  67. return this.props.setShortenerFormError('Please login or sign up to use this feature.');
  68. }
  69. const type = e.target.id;
  70. return this.setState({ [type]: !this.state[type] });
  71. }
  72. render() {
  73. const { customurlCheckbox, passwordCheckbox } = this.state;
  74. const { isAuthenticated } = this.props;
  75. const customUrlInput = customurlCheckbox && (
  76. <div>
  77. <Label htmlFor="customurl">{window.location.hostname}/</Label>
  78. <TextInput id="customurl" type="text" placeholder="custom name" small />
  79. </div>
  80. );
  81. const passwordInput = passwordCheckbox && (
  82. <div>
  83. <Label htmlFor="customurl">password:</Label>
  84. <TextInput id="password" type="password" placeholder="password" small />
  85. </div>
  86. );
  87. return (
  88. <Wrapper isAuthenticated={isAuthenticated}>
  89. <CheckboxWrapper>
  90. <Checkbox
  91. id="customurlCheckbox"
  92. name="customurlCheckbox"
  93. label="Set custom URL"
  94. checked={this.state.customurlCheckbox}
  95. onClick={this.handleCheckbox}
  96. />
  97. <Checkbox
  98. id="passwordCheckbox"
  99. name="passwordCheckbox"
  100. label="Set password"
  101. checked={this.state.passwordCheckbox}
  102. onClick={this.handleCheckbox}
  103. />
  104. </CheckboxWrapper>
  105. <InputWrapper>
  106. {customUrlInput}
  107. {passwordInput}
  108. </InputWrapper>
  109. </Wrapper>
  110. );
  111. }
  112. }
  113. ShortenerOptions.propTypes = {
  114. isAuthenticated: PropTypes.bool.isRequired,
  115. setShortenerFormError: PropTypes.func.isRequired,
  116. };
  117. export default ShortenerOptions;