ShortenerOptions.js 3.4 KB

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