ShortenerOptions.js 3.2 KB

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